Solved

Using javax.comm in JSP-Variable outputStream may not have been initialized

Posted on 2004-09-02
5
669 Views
Last Modified: 2008-02-01
Hi
I have made a program in JSP for open port through browser. I am using javax.comm

But I found error whenever use outputStream and inputStream to read and write data through serial port.

The error is:
Variable outputStream may not have been initialized

below is my code:
****************
String drivername = "com.sun.comm.Win32Driver";
try {
      CommDriver driver = (CommDriver) Class.forName(drivername).newInstance();
      driver.initialize();  //error occured                              
}
catch (Exception e)
{}
                  
//port opening
                  
portList = CommPortIdentifier.getPortIdentifiers();
                                              
while (portList.hasMoreElements())
{
     portId = (CommPortIdentifier) portList.nextElement();                                 
     if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
    {
          try {
                 serialPort = (SerialPort) portId.open("", 2000);                       
                                                                     
                 try {
                       outputStream = serialPort.getOutputStream();
                inputStream  = serialPort.getInputStream();                                                  }
          catch (IOException e)
          { }                                                                  
 
                 try {
                       serialPort.setSerialPortParams(1200,
                SerialPort.DATABITS_7,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_EVEN);                                                                                                
                }
          catch (UnsupportedCommOperationException e)
                 {}                                                                          
           }  
           catch (PortInUseException e)
           {
           out.println("port in use:"+e.toString());            
            }                                                
     }   //end if
}  //while

outputStream.write(0x02);
.
.
.
0
Comment
Question by:VRGultom
5 Comments
 
LVL 4

Author Comment

by:VRGultom
ID: 11961481
Anybody can help me how to fix this problem, please?

Regards
Veronika
0
 
LVL 11

Accepted Solution

by:
sompol_kiatkamolchai earned 75 total points
ID: 11961525
in variable declaration initial it to null

OutputStream outputStream = null;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11961680
And before you do:

    outputStream.write(0x02);

check if "outputStream == null"
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 11965813
What is the story behind this:
try {
     CommDriver driver = (CommDriver) Class.forName(drivername).newInstance();
     driver.initialize();  //error occured                        
}
catch (Exception e)
{}


if '//error occured' is expected, then just trap the EXPECTED error, you might be getting an unexpected error.  The code should be more like this:
try {
     CommDriver driver = (CommDriver) Class.forName(drivername).newInstance();
     driver.initialize();  
}
catch (SomeErrorIExpect ignore)
{
   //ignore this error it means nothing
   //..You could possible log this error if you are using a logging framework
   //E.G.  log.error("This error is ignored, ignore);
}
catch (SomeErrorThatNEVERhappens e)
{
  //Crash and burn because something went wrong
  //log.fatal("WHAT HAPPENED!!!!!", e);
  e.printStackTrace();
  throw ServletException("An unexpected error occured, call the police!!!", e);
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 11971073
Sorry, I got on my soapbox, I did not read the post carefully enough!

The Java compiler has analysed the program flow and determined that there is a possibility that the line
                       outputStream = serialPort.getOutputStream();
may NOT get executed, be cause of the IF and the nested TRY/CATCH blocks.  So,  sompol_kiatkamolchai
 and Tim Yates are both right..  You need to initialize all of the inputStream AND outputStream to NULL.  Then before they are used, check that they are NOT null, otherwise the page will generate a NullPointerException.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to access elements of included jsp  from parent jsp's save button 9 24
library class in java 1 93
bean mapper frameworks 5 63
maven webservice projet error 6 80
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question