Results 1 to 8 of 8
  1. #1
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default NULLPointer Exception

    I have a Null Pointer exception in the following code but I can't figure out why. The variable "line" has been initialized and returns a value before and after the exception.

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class chatBot
    {
    	static Scanner sc = new Scanner(System.in);
    	static String uInput = "";
    		
    	public static void main(String[] args)
    	{
    		while(true)
    			{
    				System.out.println("Input text");
    				uInput = sc.nextLine();
    				uInput = uInput.toUpperCase();
    				removePunctuation();
    				System.out.println(response(uInput));				
    			}		
    	}
    	
    	static String response = "";
    	
    	public static String response(String uInput)
    	{
    			try
    			{
    				response = findPattern("data/" +uInput.substring(0,1) + ".txt");
    			}
    			catch(IOException e)
    			{
    				response = "I don't know what you're talking about.";
    			}
    		
    		return response;
    		
    	}
    	
    	public static String findPattern(String filename) throws IOException
    	{
    		FileReader fileReader = new FileReader(filename);
    		
    		BufferedReader bufferedReader = new BufferedReader(fileReader);
    		String line = "";
    		String line2 = "";
    		
    		while((line = bufferedReader.readLine()) != null)
    		{
    			if(line.startsWith("<pattern>"))
    			{ 
    				int i = line.indexOf(">");
    				line = line.substring(i+1);
    				Random rand = new Random();
    				
    				List<String> responses = new ArrayList<String>();
    				String[] responseHolder = {};
    				
    				if(uInput.equals(line))
    				{
    					line = bufferedReader.readLine();
    					while(line.startsWith("<response>"))
    					{
    						i = line.indexOf(">");
    						responses.add(line.substring(i+1));
    						line = bufferedReader.readLine();
    					}
    					
    					responseHolder = responses.toArray(new String
    							[responses.size()]);
    					line = "Rogue: "+ responseHolder[rand.nextInt
    					                    (responseHolder.length)];
    					break;
    				
    				}
    			}
    		}
    		
    		bufferedReader.close();
    		
    		FileReader fileReader2 = new FileReader(filename);
    		BufferedReader bufferedReader2 = new BufferedReader(fileReader2);
    		String star1 = "";
    		
    		if(line == "NULL" || line == null)
    		{//open file a second time to search for * option
    			while((line = bufferedReader2.readLine()) != null)
    			{
    				
    				if(line.startsWith("<pattern>"))
    				{
    					int i = line.indexOf(">");
    					if(line.contains("*"))
    					{
    						int j = line.indexOf("*");
    						String divLine1 = line.substring(i+1,j);
    						
    						if(uInput.length() >= divLine1.length())
    						{
    							star1 = uInput.substring(divLine1.length());
    						}
    						else
    							star1 = "";
    						
    						if(line.substring(j+1)!=null)
    						{
    							String divLine2 = line.substring(j+1);
    							
    							if(divLine2=="" || divLine2 == null)
    							{
    								line = divLine1.substring(i+1,j);
    							}
    							else
    								line = divLine1;
    								line2 = divLine2;
    							
    							List<String> responses = new ArrayList<String>();
    							String[] responseHolder = {};
    							Random rand = new Random();
    							
    							if(uInput.startsWith(line) && (uInput.contains(line2) 
    									|| (divLine2 == "" || divLine2 == null)))
    							{
    								
    								line = bufferedReader2.readLine();
    								try
    								{
    								while(line.startsWith("<response>"))
    								{
    										i = line.indexOf(">");
    										responses.add(line.substring(i+1));
    										line = bufferedReader2.readLine();
    								}
    								}
    								catch(NullPointerException e)
    								{
    									e.printStackTrace();
    								}
    								
    								responseHolder = responses.toArray(new String
    										[responses.size()]);
    								
    								for(int a = 0; a<responseHolder.length; a++)
    								{
    									if(responseHolder[a].contains("<star1>"))
    									{
    										responseHolder[a] =
    												responseHolder[a].replace
    												("<star1>", star1.toLowerCase());
    									}
    									
    								}
    								line = "Rogue: "+ responseHolder[rand.nextInt
    								                    (responseHolder.length)];
    								break;
    							}
    							else
    							{
    								line = "I don't know what you're talking about.";
    								break;
    							}
    							
    						}
    					}
    				}
    
    			}
    			
    		}
    		bufferedReader2.close();
    		return line;
    	}
    
    	public static void removePunctuation()
    	{
    		uInput = uInput.replaceAll("\\?", "");
    		uInput = uInput.replaceAll("\\.", "");
    		uInput = uInput.replaceAll("\\!", "");
    		uInput = uInput.replaceAll("\\'", "");
    		uInput = uInput.replaceAll("\\,", "");
    	}
    
    }
    This is the stack trace print out:

    java.lang.NullPointerException
    at chatBot.findPattern(chatBot.java:126)
    at chatBot.response(chatBot.java:27)
    at chatBot.main(chatBot.java:17)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: NULLPointer Exception

    What line causes the exception? This information is always critically important and *always* should be included in questions like these. Else we are forced to guess which isn't fair.

  3. #3
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: NULLPointer Exception

    I apologize I meant to put line 126.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: NULLPointer Exception

    We know it's this line number based on the exception text, but again, which line is line 126?

  5. #5
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: NULLPointer Exception

    while(line.startsWith("<response>"))

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: NULLPointer Exception

    Now we can answer the question, thank you. You'll want to read the bufferedReader API to see what it returns when it reaches the end of input, and this will tell you why you're having your current problem.

  7. #7
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    0

    Default Re: NULLPointer Exception

    According to some documentation I read bufferedReader returns null at the end of a file. I've tried to add a while((line = bufferedReader2.readLine()) != null) wrapping the error line and it still gives me the error. Not sure what is wrong.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: NULLPointer Exception

    If you've made a significant change, please show the new code. Please show the new code's error message, and again indicate which line throws the exception.

Similar Threads

  1. Nullpointer exception
    By Games2Design in forum New To Java
    Replies: 4
    Last Post: 10-20-2012, 01:15 AM
  2. Nullpointer exception
    By naveen516 in forum New To Java
    Replies: 2
    Last Post: 12-19-2011, 05:36 AM
  3. Nullpointer Exception???
    By kipcorn91 in forum AWT / Swing
    Replies: 5
    Last Post: 10-29-2010, 12:19 AM
  4. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 05:44 AM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 04:40 PM

Posting Permissions

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