Exclamation Exception in thread "main" java.lang.NullPointerException
Hello All,
I'm having some difficulties with this program. I keep getting an error that makes no sense to me. The code I'm trying to implement is supposed to take user input, store that into a string array, find the longest element of the string array, and print different items based on the length of the longest element. I have the code for taking the user input and storing everything into an array, but when I try to find the longest string element, I receive the following error:
"Exception in thread "main" java.lang.NullPointerException
at MyCode.main(MyCode.java:20)"
The weird thing is the program seems to work. The output is exactly what I would expect.
Here's my code:
Code:
import java.util.Scanner;
public class MyCode {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in); // scanner for the keyboard
int i=0;
System.out.println("What would you like to talk about?");
String line = keyboard.nextLine(); // get the line as a string
Scanner lineScanner = new Scanner(line); // scanner for reading words from the line
String[] myString = new String[1000];
while(lineScanner.hasNext()) { // while there is a word left in the line
myString[i] = lineScanner.next();
i++;
}
int y;
String word = myString[0];
for (y=1; y<myString.length ; y++)
{
[B] if ( (myString[y]).length() > word.length())[/B]
{
word = myString[y];
System.out.println(word);
}}
if (word.length() ==4)
{
System.out.println("Tell me more about " + word);
}
else if (word.length() == 5)
{
System.out.println("Why do you think " + word + " is important?");
}
else if (word.length() >= 6)
{
System.out.println("Now we are getting somewhere. How does " + word + " affect you the most?");
}
else
{
System.out.println("Maybe we should move on. Is there something else you would like to talk about?");
}
}
}
When i run the code, I am prompted to enter what I would like to talk about. If I were to say "which is the looooooongest word", "looooooongest" is printed out, along with the above error. Line 20 (referenced in the error) is in bold above.
Does anyone know why I am getting this error? Or why I am getting this error, yet everything appears to be working?
Thank you for your help!
Lorelai
Re: Exclamation Exception in thread "main" java.lang.NullPointerException
You will get the null pointer exception when you use a variable as if it had a value (call a method, or make an array reference etc) when it is really null.
Code:
int[] foo = null;
foo[42]; // bad, foo is null
foo.length(); // bad, foo is null
There are three candidates on that line. Any of them could be null: myString, myString[y] and word. Use System.out.println() to print out their values just before you get the exception. That way you will see which variable is null when you did not expect it to be.
Re: Exclamation Exception in thread "main" java.lang.NullPointerException
Thank you for your prompt reply pbrockway2. I did as you suggested and printed out the variables. As near as I can tell from my test, it appears that word is null. I am confused on this, however, as I thought I had initialized word:
Code:
String word = myString[0];
How is this null? Isn't word being initialized to the first element in my array?
Thank you!
Lorelai
Re: Exclamation Exception in thread "main" java.lang.NullPointerException
Are you sure it is word that is null at that point? What code are you using to see that?
Re: Exclamation Exception in thread "main" java.lang.NullPointerException
My bad, it was myString[y]. I was able to get the initial loop to work.
My next problem is giving me a similar error, however. I need the loop to repeat through the code until the user types "Thanks". For example, if the user types "I like cookies", the code sees that "cookies" is the longest string. "Cookies" contains more than 6 characters, so the code prints out "Now we are getting somewhere. How does cookies affect you the most?". The code should then take the user input and repeat the process of finding the longest string. This should continue until the user types "Thanks".
The first time the loop runs through, it works perfectly. When the user inputs another answer, however, I receive the "Exception in thread "main" java.lang.NullPointerException at MyCode.main(MyCode.java:35)" error again. This time, line 35 points to if (word.length() ==4). Why would the loop work once and then fail on the second go through?
Code:
import java.util.Scanner;
public class MyCode {
public static void main(String[] args) {
int i=0;
boolean end = false;
Scanner keyboard = new Scanner(System.in); // scanner for the keyboard
System.out.println("What would you like to talk about?");
do
{
String line = keyboard.nextLine(); // get the line as a string
Scanner lineScanner = new Scanner(line); // scanner for reading words from the line
String[] myString = new String[1000];
if(line == "Thanks")
end=true;
while(lineScanner.hasNext()) { // while there is a word left in the line
myString[i] = lineScanner.next();
i++;
}
int y=0;
String word = myString[0];
for (y=0; y<myString.length -1 ; y++)
{
if (myString[y] == null)
{break;}
if ( (myString[y]).length() > word.length())
{
word = myString[y];
System.out.println(word);
}}
if (word.length() ==4)
{
System.out.println("Tell me more about " + word);
}
else if (word.length() == 5)
{
System.out.println("Why do you think " + word + " is important?");
}
else if (word.length() >= 6)
{
System.out.println("Now we are getting somewhere. How does " + word + " affect you the most?");
}
else
{
System.out.println("Maybe we should move on. Is there something else you would like to talk about?");
}
} while (!end);
}
}
Re: Exclamation Exception in thread "main" java.lang.NullPointerException
Clearly word is null - the question is why. It might have something to do with the fact that you don't reset i each time around the loop. So, eg, the second time round you read the words and put them into the myString array but not at the start of the array. But it's the start of the array that the for loop deals with.
-----
If I were you I would clean up the code as it is a bit unreadable at the moment. Get the formatting looking nice - use whatever convention you like or are told to use, but get it consistent. Spaces are better than tabs especially when the code ends up displayed by a web browser.
Use good variable names. i and y are not descriptive. myString is not a string! (When is a string not a string? When it's array!)
Think separate methods! and Have a plan!
I only mention all this because your code is growing and becoming a tangle. It might seem like you make the fastest progress by actually writing code line by line and having a pragmatic attitude about what "working" means. But that all breaks down if the tangle becomes a knot and progress impossible.