Results 1 to 7 of 7
4Likes Thread: Exception in thread "main" java.lang.NullPointerException
- 04-25-2012, 04:24 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
Exception in thread "main" java.lang.NullPointerException
When I run my code I get the following message: Exception in thread "main" java.lang.NullPointerException
The program is supposed to extract words from a file and then place them in a Binary Search Tree. This is a past HW assignment that has already been graded. According to the grader, the only thing wrong with my program is the display() method. I will not be able to fix the display() method until I can get rid of this "Exception in thread "main" java.lang.NullPointerException" message. My question is, what do I have to do to get rid of this error message? I know my code is ridiculously long and I apologize for that. Thanks.
Java Code:package assignment8; import java.io.*; import java.util.*; import java.lang.String; public class Assignment8 { public static void main(String[] args) throws FileNotFoundException { String lineIn; //Line of text in the file String nextWord; //Next word in the line of text File readFile = new File("Speech.txt"); //To reduce confusion Speech.txt.txt has been renamed Speech.txt Scanner input = new Scanner(readFile); //read from file BSTree tree = new BSTree(); while (input.hasNext()) { lineIn = input.nextLine(); while (lineIn.length() > 0) { nextWord = getNextWord(lineIn); //get word from front of line tree.insert(nextWord, true); //inserts word into BSTree lineIn = removeNextWord(lineIn); //update the line w/o word } } tree.display(); //displays BST input.close(); } public static boolean isLetter(char ch) { //checks if character is a letter return ((ch >= 'A') && (ch <= 'Z') || (ch >= 'a') && (ch <= 'z') || (ch == '-') || (ch == '\'')); //Allow dash and apostrophe } public static String removeNextWord(String s) { //Returns the string with the first 'word' removed //First, pull all non-letters off front while ((s.length() > 0) && (isLetter(s.charAt(0)) == false)) { s = s.substring(1); } while ((s.length() > 0) && (isLetter(s.charAt(0)) == true)) { s = s.substring(1); } return s; //Return the resulting string } public static String getNextWord(String s) { //Returns first 'word' of the string //First, pull all non-letters off front String word = ""; // while ((s.length() > 0) && (isLetter(s.charAt(0)) == false)) { s = s.substring(1); } while ((s.length() > 0) && (isLetter(s.charAt(0)) == true)) { word = word + s.charAt(0); //build up the word s = s.substring(1); //remove letters from string input } return word; //Return the resulting word string } } class BTNode { //define variables String word; //Data item int count; //Count of data items input BTNode left; //Node's left child BTNode right; //Node's right child //constructor with specific String, right BTNode and left BTNode BTNode(String S, BTNode L, BTNode R) { word = S; count = 1; left = L; right = R; } //methods public void increment() { //adds 1 to the counter count++; } } class BSTree { //define variables BTNode root; //first node of tree //no args constructor BSTree() { root = null; } //methods public BTNode insert(String newWord, boolean lowerCase) { //adds new node into the BST if newWord is not there //increments its node's counter if it is already there if (lowerCase == true) { newWord = newWord.toLowerCase(); //converts to lowercase } return insertR(root, newWord); } private BTNode insertR(BTNode N, String S) { //recursive method called by insert method if (N.word.compareTo(S) == 0) { N.increment(); } else { if (N.word.compareTo(S) > 0) { if (N.left == null) { N.left = new BTNode(S, null, null); } else { insertR(N.left, S); } } else { if (N.right == null) { N.right = new BTNode(S, null, null); } else { insertR(N.right, S); } } } return root; } public int wordCount() { //returns sum of all occurence counters in all the nodes of BST return wCountR(root); } public int wCountR(BTNode N) { //recursive method called by wordCount() if (N == null) { return 0; } else { return N.count + wCountR(N.left) + wCountR(N.right); } } public void display() { //displays the entire BST in a nicely formatted ouput inorder(root); //call recursive inorder method wordCount(); //call wordCount method System.out.println("|" + wordCount()); } private void inorder(BTNode N) { //performs an inorder traversal of BST if (N != null) { inorder(N.left); visit(N); inorder(N.right); } } public void visit(BTNode N) { //visit method System.out.println(N.word); } }
- 04-25-2012, 04:58 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Exception in thread "main" java.lang.NullPointerException
You should check the error stack trace and find out which line of your code that start the exception. And then look closely to those line a find out which object that might have a null value.
Website: Learn Java by Examples
- 04-25-2012, 04:58 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Exception in thread "main" java.lang.NullPointerException
Could you post the error and its associated stack trace?what do I have to do to get rid of this error message?
- 04-25-2012, 06:17 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
Re: Exception in thread "main" java.lang.NullPointerException
Sorry about that. Here it is.
That would mean the problem lies in one of these three lines.----jGRASP exec: java assignment8.Assignment8
Exception in thread "main" java.lang.NullPointerException
at assignment8.BSTree.insertR(Assignment8.java:108)
at assignment8.BSTree.insert(Assignment8.java:102)
at assignment8.Assignment8.main(Assignment8.java:20)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
In this line I don't think either N or S have a null valueJava Code:if (N.word.compareTo(S) == 0) {
In this line I know the default value for root is null. I think the problem is here. I'm not sure how to fix it.Java Code:return insertR(root, newWord);
nextWord shouldn't be null here since nextWord = getNextWord(lineIn) and lineIn = input.nextLine();Java Code:tree.insert(nextWord, true); //inserts word into BSTree
- 04-25-2012, 06:26 AM #5
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Exception in thread "main" java.lang.NullPointerException
Yes, the root will be null as you explicitly assign null to it in the BSTree constructor. You need to create an instance of the root type before you can use it.
Website: Learn Java by Examples
- 04-25-2012, 06:28 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Exception in thread "main" java.lang.NullPointerException
You can check that:the default value for root is null. I think the problem is here.
(Or something similar put into the insertR() method.)Java Code:if (lowerCase == true) { // btw, if (lowerCase) looks nicer and does the same thing... newWord = newWord.toLowerCase(); //converts to lowercase } System.out.println("About to insertR() with root=" + root); return insertR(root, newWord);
If it turns out to be the culprit you should ask yourself "What is insertR() supposed to do when the root is null?". I haven't read the code all that closely, but the intent of that method may be to create a new node based on the string and make that new node the tree's root.
- 04-25-2012, 09:25 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
Re: Exception in thread "main" java.lang.NullPointerException
@wsaryada Thanks, I've created an instance of the root type and added to my insert method.
@pbrockway2 Thanks, it was the culprit. I changed my insert method so that it creates a new BTNode when root is null.
I got the code to finally stop displaying the error message. Here is the updated insert method.
Java Code:public BTNode insert(String newWord, boolean lowerCase) { //adds new node into the BST if newWord is not there //increments its node's counter if it is already there if (lowerCase == true) { newWord = newWord.toLowerCase(); //converts to lowercase } if(root == null) { //if root is null root = new BTNode(newWord, null, null); //make new node } System.out.println("About to insertR() with root = " + root); return insertR(root, newWord); }
Similar Threads
-
Another Exception in thread "main" java.lang.NullPointerException problem
By wdh321 in forum New To JavaReplies: 6Last Post: 04-19-2012, 07:10 PM -
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks