Results 1 to 8 of 8
  1. #1
    Luinithil is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Help with fixing NullPointerException error

    I'm a beginner with ~2 month's classes, now trying to write a basic interpreter for an assignment. I have several reserved words which I implemented classes for (one reserved word, one class), and mapped using a HashMap. A line of code in this language always starts with a reserved word. Code from a file is read into an ArrayList line by line and stored, then parsed and executed.

    So I'm testing one of these classes and get this NullPointerException. I did some debugging and isolated the problem to line 22: the thread of execution is jumping from break 0 to break1 to break3 and bypassing break2. I'm not sure why the code at break2 isn't working though. Is there something I missed with regards to String.indexOf() and its use? I'd use String.split() but am not sure how to work with the array that method produces-- I actually need to parse in two steps: one for the reserved word, and then some way to pass the rest of the String to the relevant class which will parse the rest of the String for data needed and execute. Thanks for the help!

    Java Code:
    	public static void main (String[]args)
    	{
    		List <String> myString= new ArrayList<String>();
    		myString.add(new String("# A TPL HELLO WORLD PROGRAM"));
    		myString.add(new String("STRING myString"));
    		myString.add(new String("LET myString= \"HELLO WORLD\""));
    		myString.add(new String("PRINTLN myString"));
    		myString.add(new String("PRINTLN HELLO WORLD"));
    		myString.add(new String("END"));
    
    	
    		for (String listString: myString)//iterate across arraylist
    		{	
    			
    			int firstSpace= listString.indexOf("END");
    			System.out.println("break0");
    			String command= listString;
    			System.out.println("break1");
    					
    			if(firstSpace >0)
    				{
    					command=listString.substring(0, firstSpace);// line causing NullPointerException
    					System.out.println("break2");
    				}
    					
    			Directive directive= commandHash.get(command);
    			System.out.println("break3");
    				if (commandHash ==null)
    					{
    						System.out.println("break4");
    						System.out.println("No mapped command given");
    					}
    					else if(commandHash!=null)
    					{
    						directive.execute(listString);
    					}				
    		}
    		
    	}
    Last edited by Luinithil; 01-12-2012 at 09:27 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with fixing NullPointerException error

    I don't see how that line could produce a NullPointerException since listString is not null at that point. Could you post the stack trace?

  3. #3
    Luinithil is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with fixing NullPointerException error

    Ok, being new to everything in general-- what's the stack trace, and how do I get one and post it? Also, do you need all the code?

    Full text of error message from my manual debugging comes up with:
    break0
    break1
    break3
    Exception in thread "main" java.lang.NullPointerException at StringSplit.main(StringSplit.java:71)
    Last edited by Luinithil; 01-12-2012 at 09:44 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with fixing NullPointerException error

    The stack trace is the thing that reports the NullPointerException. It begins "NullPointerException on line nnn of Foo.java".

    To be honest I don't think you have a NullPointerException here. "break2" is never going to print. That's because firstSpace is never >0. The closest it gets is zero when the last string of the list is being processed. Other than that it is -1 because "END" does not appear in any of the strings before the last one. Perhaps you meant if(firstSpace==0) {

  5. #5
    Luinithil is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with fixing NullPointerException error

    Hmm, ok. Ithink you're right and I might've been mistaken on the error with too many compilations: but anyway, this is my current code which now doesn't get past break1 and is throwing StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1958) at StringSplit.main(StringSplit.java:59)--

    Java Code:
    import java.util.*;
    
    public class StringSplit
    {
    
    
    public interface Directive //Map keywords to an interface
    {
    	
    	
    	public void execute (String listString);
    }
    
    
    	public class EndStatement implements Directive
    	{
    		public void execute(String listString)
    		{	
    			
    			System.out.print("TPL finished OK [" + " x lines processed]");
    			System.exit(0);
    		}
    	}
    	
    	
    	
    	private static Map<String, Directive> commandHash= new HashMap<String, Directive>();
    	{
    		commandHash.put("END", new EndStatement());
    		
    	}
    	
    
    	
    	
    	public static void main (String[]args)
    	{
    		List <String> myString= new ArrayList<String>();
    		myString.add(new String("# A TPL HELLO WORLD PROGRAM"));
    		myString.add(new String("STRING myString"));
    		myString.add(new String("LET myString= \"HELLO WORLD\""));
    		myString.add(new String("PRINTLN myString"));
    		myString.add(new String("PRINTLN HELLO WORLD"));
    		myString.add(new String("END"));
    	
    	
    	
    		for (String listString: myString)//iterate across arraylist
    		{	
    			
    			int firstSpace= listString.indexOf("END");
    			System.out.println("break0");
    			String command= listString;
    			System.out.println("break1");
    					
    			if(firstSpace<0)
    				{
    					
    					command=listString.substring(0, firstSpace);
    					System.out.println("break2");
    				}
    					
    			Directive directive= commandHash.get(command);
    			System.out.println("break3");
    				if (commandHash ==null)
    					{
    						System.out.println("break4");
    						System.out.println("No mapped command given");
    					}
    					else if(commandHash!=null)
    					{
    						directive.execute(listString);
    					}				
    		}
    		
    	}
    }

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with fixing NullPointerException error

    Now you are entering the if block with firstSpace being <0. (in particular it's -1). But you can't call substring() with a negative second argument: you're saying "I want the substring from position zero to just less than position negative one". That's what the runtime exception is complaining about.

    What is firstSpace supposed to be anyway? At the moment you are setting it to be the index of the substring "END" but that doesn't seem to have anything to do with a space.

  7. #7
    Luinithil is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with fixing NullPointerException error

    Ech, my bad... I copypasta-ed from a previous piece of code; renamed it to firstToken now as a better fit. Basically-- I need to get one by one the first word from all the Strings, which is contained in the ArrayList. I'm then going to map that to my HashMap so the class can take over processing the string. Does that make sense?
    Last edited by Luinithil; 01-12-2012 at 10:31 AM.

  8. #8
    Luinithil is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help with fixing NullPointerException error

    Just to mention that the problem is solved-- it was a logical error basically checking null on the wrong variable, among other sundry issues. Thanks for the help!

Similar Threads

  1. help fixing error in constructor
    By katiebear128 in forum New To Java
    Replies: 3
    Last Post: 10-13-2011, 03:40 AM
  2. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  3. [SOLVED] I need help fixing an error
    By leonardjr in forum New To Java
    Replies: 8
    Last Post: 02-21-2009, 08:33 PM
  4. help with fixing my assignment.please...error
    By Jin-enigma-PL in forum New To Java
    Replies: 4
    Last Post: 02-05-2009, 12:12 AM
  5. Exception Error need help fixing
    By skinnybones in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 07:14 PM

Tags for this Thread

Posting Permissions

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