Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Why am I getting NullPointerException?  RSS feed

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know why but I'm getting runtime error:
"java.lang.NullPointerException"
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

Paul.

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;
}
}
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
lowercase baba
Posts: 12746
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
 
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.,

 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronized posting!
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!