Java Null Pointer Exception
I'm getting a null pointer exception concerning lines 123 and 206, about the getUARTLine method. I had this code working earlier in that it was taking in the strings, splitting them up, removing the x and y characters and turning the remaining numbers into ints. I can't figure out wwhat's causing the null pointer error, and any help would be greatly appreciated.
Thanks!!
Code :
import java.io.*;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;
import java.util.Scanner;
public class finalproject implements SerialPortEventListener {
SerialPort serialPort;
/** The port we're normally going to use. You need to change these to match your system*/
private static final String PORT_NAMES[] = {
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyUSB0", // Linux
"COM60", // Windows
"COM3", // Windows
};
/** Buffered input stream from the port */
private InputStream input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
// iterate through, looking for the port
while (portEnum.hasMoreElements()) { // Looking through all available ports for one that matches the one we want.
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); // Make a new port
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) { // Is the current port name the same as one of the names in our list?
portId = currPortId; //if so, use this port.
break;
}
}
}
if (portId == null) { // We never found the port!
System.out.println("Could not find COM port.");
return;
}
try { // We think we know the port so lets TRY to open it. It might not work though so we are only TRYing it.
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
output = serialPort.getOutputStream();
input = serialPort.getInputStream();
} catch (Exception e) {
System.err.println(e.toString());
}
System.out.println("Port opened successfully.");
}
public void initialize(String portname) {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
// iterate through, looking for the port
while (portEnum.hasMoreElements()) { // Looking through all available ports for one that matches the one we want.
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); // Make a new port
if (currPortId.getName().equals(portname)) { // Is the current port name the same as the one we want?
try { // We think we know the port so lets TRY to open it. It might not work though so we are only TRYing it.
// open serial port, and use class name for the appName.
serialPort = (SerialPort) currPortId.open(this.getClass().getName(), TIME_OUT);
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
output = serialPort.getOutputStream();
input = serialPort.getInputStream();
} catch (Exception e) {
System.err.println(e.toString());
}
System.out.println("Port " + this.getClass().getName() + " opened successfully.");
return;
}
}
System.out.println("Port " + portname + " was not opened!");
System.exit(0);
}
/**
* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
*/
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
public String getUARTLine(){
String inputLine = "steve";
System.out.println("inside getUART method" );
try{
BufferedReader input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
inputLine = input.readLine();
System.out.println("UART method " + inputLine);
if (inputLine.length() == 0)
return null;
} catch (IOException e){
//System.out.println("IOException: " + e);
return null;
}
return inputLine;
}
public char readUART(){
int temp = '.';
try{
if (input.available() > 0)
temp = input.read();
if (temp == -1)
return 0;
} catch (IOException e){
//System.out.println("IOException: " + e);
return 0;
}
return (char)(byte)temp;
}
public int writeUART(char b){
try{
output.write((char)(byte)b);
} catch (IOException e){
//System.out.println("IOException: " + e);
return -1;
}
return 1;
}
/**
* Handle an event on the serial port. Read the data and print it.
*/
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
int available = input.available();
byte chunk[] = new byte[available];
input.read(chunk, 0, available);
// Displayed results are codepage dependent
System.out.print(new String(chunk));
} catch (Exception e) {
System.err.println(e.toString());
}
}
// Ignore all the other eventTypes, but you should consider the other ones.
}
public static void main(String[] args) throws Exception {
//BufferedReader is = new BufferedReader(new InputStreamReader(serialPort.getInputStream));
System.out.println("Started");
finalproject main = new finalproject();
main.initialize();
/** Your code should go here **/
/*while (true){
String temp = main.getUARTLine();
if (temp != null)
System.out.println(temp);
if (is.ready()){
String inputLine = is.readLine();
System.out.println(inputLine);
break;
}
}
*/
String xstring = "herp derp";
String ystring = "durka durka";
boolean ready = false;
while(true){
String string = main.getUARTLine();
System.out.println("test = " + string);
while(string != null){
System.out.println("inside the while loop");
//String inputLine = is.readLine();
//System.out.println("input line = " + inputLine);
if(string.charAt(0) == 'X')
xstring = string;
if(string.charAt(4) == 'Y')
ystring = string;
System.out.println("xstring = " + xstring);
System.out.println("ystring = " + ystring);
xstring = xstring.substring(1,4);
ystring = ystring.substring(5);
System.out.println("modified xstring = " + xstring);
System.out.println("modified ystring = " + ystring);
}
int xnum = Integer.parseInt(xstring);
int ynum = Integer.parseInt(ystring);
//int xnum = xstring.ConvertStringToInt(xstring);
//int ynum = ystring.ConvertStringToInt(ystring);
//stop
if((186 < xnum && xnum < 190) && (191 < ynum && ynum< 194))
main.writeUART('s');
//left
if((186 < xnum && xnum < 190) && (170 < ynum && ynum< 190))
main.writeUART('l');
//right
if((186 < xnum && xnum < 190) && (195 < ynum && ynum< 210))
main.writeUART('r');
//forward
if((170< xnum && xnum < 185) && (191 < ynum && ynum< 194))
main.writeUART('f');
//Backward
if((191 < xnum && xnum < 210) && (191 < ynum && ynum< 194))
main.writeUART('b');
break;
}
Thread.sleep(500);
System.out.println("outside of the while loop");
}
/*
while (true){
String temp = main.getUARTLine();
if (temp != null)
System.out.println(temp);
}*/
}
Re: Java Null Pointer Exception
Can you paste the exception message here?
Re: Java Null Pointer Exception
Sure. Here's the command window output.
Started
Port Opened Successfully.
Inside getUART method
Exception in thread "main" java.lang.NullPointerException
at finalproject.getUARTLine(finalproject.java:123)
at finalproject.main(finalproject.java:206)
Re: Java Null Pointer Exception
Quote:
main" java.lang.NullPointerException
at finalproject.getUARTLine(finalproject.java:123)
Look at line 123 in the finalproject source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
If you can not tell, add a println just before line 123 and print out the values of all the varibles on that line.
Re: Java Null Pointer Exception
Line 123 is my buffered reader input creation line. That shouldn't have any value should it, since it's an object?
Re: Java Null Pointer Exception
Are there any variables on line 123 that can be null?
Did you print out the values of all the variables used on line 123? The JVM sees one with a null value.