?
Solved

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

Posted on 2004-09-02
5
Medium Priority
?
692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 225 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

With its various features, Office 365 can not only help you with your day-to-day business tasks, it can also do wonders for your marketing campaign.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 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