Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hyena v12.2 is now available for downloading and is available in English, French, German and Spanish versions.
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

12 Experts available now in Live!

Get 1:1 Help Now