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.
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);
}
}
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.
Re: Exception in thread "main" java.lang.NullPointerException
Quote:
what do I have to do to get rid of this error message?
Could you post the error and its associated stack trace?
Re: Exception in thread "main" java.lang.NullPointerException
Sorry about that. Here it is.
Quote:
----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.
That would mean the problem lies in one of these three lines.
Code:
if (N.word.compareTo(S) == 0) {
In this line I don't think either N or S have a null value
Code:
return insertR(root, newWord);
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.
Code:
tree.insert(nextWord, true); //inserts word into BSTree
nextWord shouldn't be null here since nextWord = getNextWord(lineIn) and lineIn = input.nextLine();
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.
Re: Exception in thread "main" java.lang.NullPointerException
Quote:
the default value for root is null. I think the problem is here.
You can check that:
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);
(Or something similar put into the insertR() method.)
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.
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.
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);
}