Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java Null Pointer Exception

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!!





    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);
    		}*/
     
    	}


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Java Null Pointer Exception

    Can you paste the exception message here?

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Null Pointer Exception

    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.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

Similar Threads

  1. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 06:21 PM
  2. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM
  3. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM
  4. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM