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:
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 "=>")
Code:
playerTeam.manager.name = read.nextLine();
My (rather simple) code:
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!