Solved

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

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Data breaches are on the rise, and companies are preparing by boosting their cybersecurity budgets. According to the Cybersecurity Market Report (http://www.cybersecurityventures.com/cybersecurity-market-report), worldwide spending on cybersecurity …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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