Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2011
    Location
    Exeter, UK
    Posts
    5
    Rep Power
    0

    Default NullPointerException error

    Hi, I'm entirely new to Java, but I have experience with C in the past.

    When trying to run the following code (in which I'm investigating classes for the first time), I get the error:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
            at test.Test.main(Test.java:33)
    Java Result: 1

    This is the line in main that says (and I've highlighted it with "=>")

    Java Code:
    playerTeam.manager.name = read.nextLine();

    My (rather simple) code:

    Java Code:
    package test;
    
    import java.util.Random;
    import java.util.Scanner;
    import static java.lang.System.out;
    
    class RaceTeam {
        String name;
        int balance;
        Person manager;
    }
    
    class Person {
        String name;
        int age;
    }
    
    public class Test {
        public static void main(String[] args) {
            RaceTeam playerTeam = new RaceTeam();
            Scanner read = new Scanner(System.in);
    
            int randomNo = new Random().nextInt(50001);
            playerTeam.balance = 50000 + randomNo;
    
            out.println("Hi there, Mr Manager, what is your name?");
    =>      playerTeam.manager.name = read.nextLine();
            out.println("Nice to meet you, " + playerTeam.manager.name + "!");
            out.println("How old are you?");
            playerTeam.manager.age = read.nextInt();
            out.println("And what would you like to call your new race team?");
            playerTeam.name = read.nextLine();
            out.println("OK, " + playerTeam.name + " it is.");
            out.println(playerTeam.name + " has " + playerTeam.balance +
                    "to begin with.");
        }
    }

    Thanks in advance for any help!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    RaceTeam.manager is null be default.

    So when you call playerTeam.manager, you get back a null.

    So when you call playerTeam.manager.name, you're actually calling (null).name.

    You can't call (null).anything.

    So you have to initialize the manager before you dereference it like that.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Apr 2011
    Location
    Exeter, UK
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    RaceTeam.manager is null be default.

    So when you call playerTeam.manager, you get back a null.

    So when you call playerTeam.manager.name, you're actually calling (null).name.

    You can't call (null).anything.

    So you have to initialize the manager before you dereference it like that.
    Oh yeah, I see. Thanks. I'd actually thought about doing that and completely forgotten.

  4. #4
    Join Date
    Apr 2011
    Location
    Exeter, UK
    Posts
    5
    Rep Power
    0

    Default

    So how do I initialise a class within a class?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by Thecoffeefiend View Post
    So how do I initialise a class within a class?
    Same way you create an instance of a class anywhere else. You could add a Constructor to RaceTeam that initializes manager, or you could just do it in the same line you declare it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Join Date
    Apr 2011
    Location
    Exeter, UK
    Posts
    5
    Rep Power
    0

    Default

    OK, thanks.

    Also, do I have to initialise a new Scanner for the second String scan at the end or is there a better way to do this?

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Quote Originally Posted by Thecoffeefiend View Post
    OK, thanks.

    Also, do I have to initialise a new Scanner for the second String scan at the end or is there a better way to do this?
    I'm not really sure what you mean. What second String scan? Why would you have to initialize a new Scanner?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Join Date
    Apr 2011
    Location
    Exeter, UK
    Posts
    5
    Rep Power
    0

    Default

    I tried running the program after initialising a Person class in the RaceTeam class and the Scanner worked fine for the first nextLine() and the nextInt() but on the last nextLine where the program asks for the team name it didn't work. This is the output:

    Java Code:
    Hi there, Mr Manager, what is your name?
    Thecoffeefiend
    Nice to meet you, Thecoffeefiend!
    How old are you?
    100
    And what would you like to call your new race team?
    OK,  it is.
     has 64122 to begin with.
    But if I initialise a new Scanner called read2 and replace the last Scanner, it works fine.
    Last edited by Thecoffeefiend; 04-14-2011 at 04:19 PM. Reason: Include more information

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    Oh, I understand now.

    The problem is that nextInt() does not read the end of line after the next int. So if you enter 100 and hit enter, what the Scanner sees is something more like this:

    100\n

    The nextInt() method reads until the end of the next int and then stops reading, so the Scanner at that point is looking at:

    \n

    So when you call nextLine(), which reads until the end of the next end of line, it reads that \n and stops, even if there is other stuff after it (your team name).

    You could either call nextLine() after calling nextInt() and just throw that value away, or you could use nextLine() and convert to an int instead of using nextInt().
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. NullPointerException Error
    By PorgrammingNoob117 in forum Java Applets
    Replies: 32
    Last Post: 04-25-2011, 11:05 PM
  2. Need Help with NullPointerException Error
    By waterisgood5 in forum New To Java
    Replies: 2
    Last Post: 11-10-2010, 07:27 PM
  3. NullPointerException error
    By Aggror in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 02:31 PM
  4. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  5. ERROR: nullPointerException
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:54 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
  •