Results 1 to 9 of 9
Thread: NullPointerException error
- 04-14-2011, 01:52 PM #1
Member
- Join Date
- Apr 2011
- Location
- Exeter, UK
- Posts
- 5
- Rep Power
- 0
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!
- 04-14-2011, 01:56 PM #2
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!
- 04-14-2011, 02:00 PM #3
Member
- Join Date
- Apr 2011
- Location
- Exeter, UK
- Posts
- 5
- Rep Power
- 0
- 04-14-2011, 02:01 PM #4
Member
- Join Date
- Apr 2011
- Location
- Exeter, UK
- Posts
- 5
- Rep Power
- 0
So how do I initialise a class within a class?
- 04-14-2011, 02:04 PM #5
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-14-2011, 03:13 PM #6
Member
- Join Date
- Apr 2011
- Location
- Exeter, UK
- Posts
- 5
- Rep Power
- 0
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?
- 04-14-2011, 03:22 PM #7
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-14-2011, 04:19 PM #8
Member
- Join Date
- Apr 2011
- Location
- Exeter, UK
- Posts
- 5
- Rep Power
- 0
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:
But if I initialise a new Scanner called read2 and replace the last Scanner, it works fine.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.
Last edited by Thecoffeefiend; 04-14-2011 at 04:19 PM. Reason: Include more information
- 04-14-2011, 06:03 PM #9
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
-
NullPointerException Error
By PorgrammingNoob117 in forum Java AppletsReplies: 32Last Post: 04-25-2011, 11:05 PM -
Need Help with NullPointerException Error
By waterisgood5 in forum New To JavaReplies: 2Last Post: 11-10-2010, 07:27 PM -
NullPointerException error
By Aggror in forum New To JavaReplies: 2Last Post: 09-29-2010, 02:31 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
ERROR: nullPointerException
By mathias in forum New To JavaReplies: 1Last Post: 08-05-2007, 06:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks