Results 1 to 8 of 8
Thread: NULLPointer Exception
- 11-08-2012, 04:05 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
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.
This is the stack trace print out: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("\\,", ""); } }
java.lang.NullPointerException
at chatBot.findPattern(chatBot.java:126)
at chatBot.response(chatBot.java:27)
at chatBot.main(chatBot.java:17)
-
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.
- 11-08-2012, 04:13 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
Re: NULLPointer Exception
I apologize I meant to put line 126.
-
Re: NULLPointer Exception
We know it's this line number based on the exception text, but again, which line is line 126?
- 11-08-2012, 04:25 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
Re: NULLPointer Exception
while(line.startsWith("<response>"))
-
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.
- 11-08-2012, 04:48 AM #7
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
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.
-
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
-
Nullpointer exception
By Games2Design in forum New To JavaReplies: 4Last Post: 10-20-2012, 12:15 AM -
Nullpointer exception
By naveen516 in forum New To JavaReplies: 2Last Post: 12-19-2011, 04:36 AM -
Nullpointer Exception???
By kipcorn91 in forum AWT / SwingReplies: 5Last Post: 10-28-2010, 11:19 PM -
NullPointer exception
By bdario1 in forum New To JavaReplies: 15Last Post: 03-17-2010, 04:44 AM -
NullPointer Exception
By Preethi in forum New To JavaReplies: 8Last Post: 02-06-2008, 03:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks