Results 1 to 5 of 5
  1. #1
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    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

  4. #4
    whateverme is offline Member
    Join Date
    Oct 2010
    Posts
    51
    Rep Power
    0

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by whateverme View Post
    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

  1. Replies: 7
    Last Post: 04-18-2011, 02:24 PM
  2. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  3. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  4. Exception in thread "main" java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 0
    Last Post: 02-24-2009, 01:19 PM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •