Solved

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

Posted on 2004-09-02
5
659 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
When we talk about DevOps toolchains, I sometimes wonder how many people really get what we’re talking about. I don’t know if it’s just semantics or tone or something else, but sometimes I think it just sounds like buzzword sausage. So it’s always …
A short film showing how OnPage and Connectwise integration works.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now