File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Why am I getting NullPointerException? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why am I getting NullPointerException?" Watch "Why am I getting NullPointerException?" New topic

Why am I getting NullPointerException?

Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
Don't know why but I'm getting runtime error:
when I add the lines
players[4].gold = 0; //Dragon!!!
players[4].warriors = 0; //Dragon!!!
Anyone know why?

I changed the scope of the variables from private to public but it didn't make any difference.

Thanks in advance


public class TowerCode {

/** Creates a new instance of TowerCode */
public TowerCode() {

public void SetUpGame() {
System.out.println("Set Tower Startup Values");

//Set Array of 5 Players (Player 4 = Dragon)
Player players[];
players = new Player[5];

players[4].gold = 0; //Dragon!!!
players[4].warriors = 0; //Dragon!!!

public class Player {
private int gold = 30;
private int warriors = 10;
private int food = 25;

private int[] boughtItems = null;
private int[] foundItems = null;

private int kingdom = 0;
private int moves = 0;
private int lastLocation = 0;
private boolean cursed = false;
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
You have created an array that holds five references to player objects, but you haven't created any Player objects for them to reference. So players[4] is a reference to a player object but its value is currently null.

You need to do something like

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11175

you're making a common mistake.

when you write

Player players[];
players = new Player[5];

you have created an array that will hold players, but you have not actually created the players.

the line

players[4].gold = 0;

says "take the thing i put in slot 4 and do stuff to it" - but there is nothing there yet. you need something like

for (int x = 0; x<numPlayers; x++)
players[x] = new Player();

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183


When you create a Java array of some kind of object, what you get is an array of references to objects. All of those references are null. You can't use the objects in the array, because there are no objects in the array. You need to put objects in there first -- i.e.,

[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Synchronized posting!
jQuery in Action, 2nd edition
subject: Why am I getting NullPointerException?
Similar Threads
Incompatibe Types & Symbol Not Found
OO design question?
How should I return an array of a class type?
Referencing an array via a method.