Help with Exception in thread "main" java.lang.NullPointerException
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Will_Collani
Posted On:   Wednesday, June 14, 2006 12:26 PM

I'm trying to run a program inside netbeans but I get the following error message when I run it Exception in thread "main" java.lang.NullPointerException at serialport.read(serialport.java:62) at ListOpenPorts.main(ListOpenPorts.java:50) This only occurs inside netbeans, if I run the program from the windows XP command prompt it runs perfectly. I'm running Netbeans 5.0 btw. Any help would be greatly appreciated. The following is ListOpenPorts.java and serialport.java import javax.comm.CommPortIdentifier; import javax.comm.PortInUseException; import javax.comm.UnsupportedCommOperationException; import j   More>>

I'm trying to run a program inside netbeans but I get the following error message when I run it


Exception in thread "main" java.lang.NullPointerException

at serialport.read(serialport.java:62)

at ListOpenPorts.main(ListOpenPorts.java:50)



This only occurs inside netbeans, if I run the program from the windows XP command prompt it runs perfectly. I'm running Netbeans 5.0 btw.



Any help would be greatly appreciated.
The following is ListOpenPorts.java and serialport.java




			
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.UnsupportedCommOperationException;
import java.io.IOException;

public class ListOpenPorts{

static String sentence,UTC,status,latitude,lathem,longitude;
static String longhem,speed,course,date,magvar,magdirec,hour,minute,second;

public ListOpenPorts() {
}

public void list() throws InterruptedException{

}

public static void sepgps(String gpssignal)
{
//gpssignal = "$GPRMC,000000,V,3900.0020,N,09500.0150,W,000.0,000.0,070606,003.8,E";
sentence = gpssignal.substring(0,6);
UTC = gpssignal.substring(7,13);
hour = UTC.substring(0,2);
minute = UTC.substring(2,4);
second = UTC.substring(4,6);
status = gpssignal.substring(14,15);
latitude = gpssignal.substring(16,25);
lathem = gpssignal.substring(26,27);
longitude = gpssignal.substring(28,38);
longhem = gpssignal.substring(39,40);
speed = gpssignal.substring(41,46);
course = gpssignal.substring(47,52);
date = gpssignal.substring(53,59);
magvar = gpssignal.substring(60,65);
magdirec = gpssignal.substring(66,67);
}
public static void main(String[] args)throws PortInUseException,
UnsupportedCommOperationException,
IOException{

serialport gogo = new serialport("COM1","IMAPS", 4800);
sepgps(gogo.read('*'));
System.out.println("Time is " + hour + ":" + minute + ":" + second);
System.out.println("Longitude " + longitude + " " + longhem);
System.out.println("Latitude " + latitude + " " + lathem);

}
}

			
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.UnsupportedCommOperationException;
import javax.comm.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class serialport {

private SerialPort serialPort;
private InputStream inputStream;
private OutputStream outputStream;

public serialport(String portname,String owner, int baud) throws PortInUseException,
UnsupportedCommOperationException,
IOException
{
//Baud rate of serial port (sent in) &
//portname is OS's name of the serial port to be used
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
while(ports.hasMoreElements()){
CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
if(portname.equals(port.getName())){
//Open the Serial Port and pass parameters
serialPort = (SerialPort) port.open(owner, baud);
serialPort.setSerialPortParams(baud,SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
System.out.print(port.getName() + ": " + port.getCurrentOwner());
System.out.println(" running at " + serialPort.getBaudRate() + " bps");
//Open up the Input and Output streams for the port
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
}//end if
}
//catch(java.io.IOException e) {} //not sure to add or not
//catch(javax.comm.PortInUseException e) { }
//catch(javax.comm.UnsupportedCommOperationException e) { }
}//end serialport()

public String read() throws IOException{
int i = 0;
String input = "";
while(i != -1){
i = inputStream.read();
input += (char) i;
//System.out.println(input); //For Debug
}//end while
return input;
}//end read()
public String read(char end) throws IOException{
//end is the the last character to be read in
int i = 0;
String input = "";
while(i != end){
i = inputStream.read();
input += (char) i;
//System.out.println(input); //For Debug
}//end while
return input;
}//end read()
}//end class
   <<Less

Re: Help with Exception in thread "main" java.lang.NullPointerException

Posted By:   Robert_Lybarger  
Posted On:   Wednesday, June 14, 2006 06:44 PM

As seems to happen with every person who has ever used Eclipse, it sounds like the classpath for your netbeans project doesn't have a jar listed in it. I don't know how to configure netbeans, but the symptoms clearly point in that direction, IMO.
About | Sitemap | Contact