Results 1 to 5 of 5
- 10-12-2010, 11:36 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
Exception in thread "main" java.lang.NullPointerException
I'm having a problem. could someone please tell me what to do.
thanks.
When I pressed ESC it comes out like this
Exception in thread "main" java.lang.NullPointerException at exercise3.London
import javax.swing.*; // import the swing library for I/O
class exercise3
{
public static void main (String[] param)
{
LondonUnderground();
System.exit(0);
} // END main
/* ************************************************** *
Show how the service on different lines on the London Underground
*/
public static void LondonUnderground()
{
String ans = JOptionPane.showInputDialog("What line would you like to know about?");
if (ans.equalsIgnoreCase ("Central"))
{
JOptionPane.showMessageDialog(null, "The" + ans + " line will be SUSPENDED");
}
else if (ans.equalsIgnoreCase ("Victoria"))
{
JOptionPane.showMessageDialog(null, "The" + ans + " line will have a GOOD SERVICE");
}
else if (ans.equalsIgnoreCase ("District"))
{
JOptionPane.showMessageDialog(null, "The" + ans + " line will have DELAYS");
}
else
{
JOptionPane.showMessageDialog(null, "The line is not recognised by our system");
}
} // END LondonUnderground
} // END class exercise3
-
So now you've learned what JOptionPane.showInputDialog returns if the user presses escape -- null. So have your program test for null before you start trying to call methods on a null object. An if test right after accepting the user input should work nicely here.]
Oh, and welcome to the java-forums.org, and thanks for re-asking your question. The previous thread was completely unrelated to your current question and was also a hijack.Last edited by Fubarable; 10-12-2010 at 11:42 PM.
- 10-12-2010, 11:42 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
read the API doc
Returns:
user's input, or null meaning the user canceled the input
your String ans is null!
thats the reason why you should better use
if ("Central".equalsIgnoreCase(ans)) - the condition will be only false and dont throw an exception
- 10-12-2010, 11:51 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 51
- Rep Power
- 0
I'm really confused here.
I'm totally new to programming and I have no background. This my third exercise. I'm really sorry for this confusion.
I was told to modify my program so that it won't appear this
Exception in thread "main" java.lang.NullPointerException at exercise3.London
I was not given what to do, but I was told to add something before the first ''if''
I might sound like a clueless person here (which I am really), but I'm really trying to get it step by step
Thanks.
-
Please understand that we all start out clueless, so it's no biggie, and the first thing you need to do is obvious -- get a clue, and fortunately the resources are available for this. First you should read the tutorials so you understand what an if block is for, otherwise you won't understand the help we try to give.
You can find the Java tutorials here: Java Tutorials
You can find the if tutorial here: If Tutorials
So study them, try to change your program, and if it still doesn't work come on back.
Best of luck!
Similar Threads
-
Exception in thread "main" java.lang.NullPointerException
By Sudde-J in forum EclipseReplies: 7Last Post: 04-18-2011, 02:24 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 -
Exception in thread "main" java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 0Last Post: 02-24-2009, 01:19 PM -
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