title: Read and Write from serial port in java without listener event
body: <p>I want to read bytes from the serial port and write to it sequentially.Is there any way i can read and write from/to the serial port by creating two separate functions for read and write in java?.I wrote a program but here,serialPort.getInputStream() is giving a null pointer exception even after opening the desired COM port.</p>
  
  <p>This is my code </p>
  
  <pre><code>        package PhyCom;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.Enumeration;
  import java.util.TooManyListenersException;
  import javax.comm.CommPortIdentifier;
  import javax.comm.PortInUseException;
  import javax.comm.SerialPort;
  import javax.comm.SerialPortEvent;
  import javax.comm.SerialPortEventListener;
  import javax.comm.UnsupportedCommOperationException;
  import javax.xml.bind.DatatypeConverter;
  
  
  
  
  public class COMConnect_DLMS
  { 
     public static InputStream inputStream;
     public static OutputStream outputStream;  
     SerialPort serialPort;  
     public long baudRate=19200;
     public int dataBits=SerialPort.DATABITS_8;
     public int stopBits=SerialPort.STOPBITS_1;
     public int parity=SerialPort.PARITY_NONE;
     boolean portFound = false;  
     String defaultPort="COM1";  
     static Enumeration portList ;     
     public static  CommPortIdentifier portId;
  
     public  COMConnect_DLMS()
    {
     initialize();
    }
  
  private void initialize()
  {
      System.out.println("Establishing connection to UART");
      portList = CommPortIdentifier.getPortIdentifiers();  
      while (portList.hasMoreElements()) 
      {  
          portId = (CommPortIdentifier) portList.nextElement();  
          if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
          {  
              if (portId.getName().equals(defaultPort)) 
              {  
                  System.out.println("Found port: "+defaultPort);  
                  portFound = true; 
              }
          }
      }
       try 
      {  
       serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);  
      } 
      catch (PortInUseException e) {} 
      System.out.println(portId.getName());
  
       try
      {
          inputStream=serialPort.getInputStream();
      }
      catch (IOException e) {e.printStackTrace();}
      /*try
      {
          serialPort.addEventListener((SerialPortEventListener) this);
      }
      catch(TooManyListenersException e){}
      serialPort.notifyOnDataAvailable(true);*/
      try 
      {  
       // set port parameters  
       serialPort.setSerialPortParams((int) baudRate, dataBits,stopBits,parity);  
      }
      catch (UnsupportedCommOperationException e) {}
  
  
      try
      {
          outputStream=serialPort.getOutputStream();
      }
      catch (IOException e) {}
  }
  
  
   public void serialEvent(SerialPortEvent event)
   {
  
      if(event.getEventType()==SerialPortEvent.DATA_AVAILABLE)
      {
          byte[] readBuffer = new byte[20];
  
          try 
          {
              while (inputStream.available() &gt; 0)
              {
                  int numBytes = inputStream.read(readBuffer);
              }
              System.out.print(new String(readBuffer));
          } 
          catch (IOException e) {System.out.println(e);}
  
      }
  }
  public static void writePacket(byte[] msg)
  {
     System.out.println("Writing \""+DatatypeConverter.printHexBinary(messageString));  
    try 
    {  
      // write string to serial port  
       outputStream.write(msg);  
      //  System.out.println("written");
    } 
    catch (IOException e) {}
  
  }
  
  }
  </code></pre>
  
  <p>And also I want to more about NullPointerException in case of serial ports.Thank you.Your suggestions will be appreciated.</p>