Results 1 to 2 of 2
  1. #1
    conandor is offline Member
    Join Date
    Aug 2007
    Posts
    3
    Rep Power
    0

    Default nullPointerException problem

    I have this RS232 class that need to read by the main class.
    Whenever i use this readData method. it throws nullPointerException message.
    but if i implement it d RS232() main method, it works well with output interger value -1 or hex value 255.
    why izzit like that?


    Java Code:
    /**
    * @(#)RS232.java
    *
    * Serial Port Object
    */
    
    import javax.comm.*;
    import java.io.*;
    import java.net.*;
    
    public class RS232 {
    
       static InputStream in = null;
       static OutputStream out = null;
    
       private static RS232 instance = new RS232();
       public static RS232 getInstance() { return instance; }
    
       private RS232() {
     try {
      System.out.println("Connecting to serial0 at 115200bps");
    
      CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("serial0");
      SerialPort sp = (SerialPort)portId.open("DeviceController", 0);
    
             sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
               sp.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
    
      sp.enableReceiveThreshold(1024);
      sp.enableReceiveTimeout(1000);
    
      // Get output and input stream for the serial port
      InputStream in = sp.getInputStream();
      OutputStream out = sp.getOutputStream();
    
    /*
    int data;
    data = 0;
    try {
      data = in.read();
      System.out.println("data");
    }
    catch (IOException ioe) { System.err.println("readData FAIL"); }
    */
    
     }
           catch (IOException ioe) {
            System.err.println("Unable to open an InputStream " + "for the serial port!");
            System.exit(1);
           }
           catch (NoSuchPortException nspe) {
            System.err.println("No Such Port");
            System.exit(2);
           }
           catch (UnsupportedCommOperationException ucoe) {
            System.err.println("Unable to set the paramets of the serial port!");
            System.exit(3);
           }
           catch (PortInUseException piue) {
            System.err.println("The serial port is in use!");
            System.exit(4);
           }
           
    public int readData() {
     int data;
     data = 0;
     try {
      data = in.read();
      System.out.println("data");
     }
     catch (IOException ioe) { System.err.println("readData FAIL"); }
     return data;
    }
    
       
      public void sendData(int b) {
           try {
      out.write((byte)b);
     }
     catch (IOException ioe) {}
       }
    }

  2. #2
    conandor is offline Member
    Join Date
    Aug 2007
    Posts
    3
    Rep Power
    0

Similar Threads

  1. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 04:20 PM
  2. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 07:51 PM
  3. NullPointerException problem
    By warship in forum AWT / Swing
    Replies: 5
    Last Post: 08-10-2007, 04:43 PM
  4. ERROR: nullPointerException
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:54 AM
  5. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •