Java help

Soldato
Joined
28 Oct 2006
Posts
12,457
Location
Sufferlandria
i have this code
Code:
int marker = 0;
for(int x = 0; x < arraylength; x++)
{
	if(name==players[x].getPlayerName() && surname == players[x].getPlayerSurname())
{
	marker = x;
}
}
this is how i set up my array:
Player[] players = new Player[arraylength];
but im getting a null pointer error when i run it :(
Anybody have any ideas?
 
Need more information to diagnose, check that arraylength is a number > 0 and that the players array is in scope for the posted code.
 
As above, NullPointerException is thrown whenever the program tries to call a method or property of a null object.
 
if you're comparing strings you'll find some funkyness with "literal" == stringObject, you're better off doing stringObject.equals(new String("literal"));

For the null pointer i'd be guessing that the array hasn't been initialised, remember String[] toast = new String[5] creates a new array with 5 nulls in.

To solve this just initialize each block before usage with some magic like
String[] toast = new String[5];
for(int i = 0; i < toast.length; i++){
toast = new String();
}
 
x < players.length; Would be better really. I think he would be getting an ArrayOutOfBounds Exception if he was going off the end of the array not a null pointer exception which is because he's calling a method on null object.
 
Una said:
x < players.length; Would be better really. I think he would be getting an ArrayOutOfBounds Exception if he was going off the end of the array not a null pointer exception which is because he's calling a method on null object.
echo!
 
Back
Top Bottom