how to assign values to instances of a method created via an array
i have created an array of the PlayerRecord instance called currentPlayers and im trying to use another array of Strings to give names to the players (currentPlayers[instance].name) but it is failing horribly
when i try to name them one at a time as seen in the code here it seems to compile fine, but when i test it with a System.out.println(currentPlayers[0].name); it tells me "Exception in thread "main" java.lang.NullPointerException"
Im assuming this happens because the instances of PlayerRecord were created using an array, therefore i could not give the PlayerRecord a name value to begin with, but shouldnt currentPlayers[0].name = namesarray[0]; make up for that and give the value in namesarray[0] to currentPlayers[0].name? therefore allowing me to display the value held in currentPlayers[0].name without the error?
publicclass Round
{
publicstatic PlayerRecord[] currentPlayers =new PlayerRecord[15];
public Round(String[] namesarray)
{
currentPlayers[0].name = namesarray[0];
System.out.println(currentPlayers[0].name);
}
}
publicclass test
{
publicstaticvoid main (String[] args)
{
String[] namelist={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"};
Round one =new Round(namelist);
}
}
publicclass PlayerRecord
{
public String name;
public PlayerRecord(String playername)
{
name = playername;
}
}
[2820 byte] By [
s34nsm411a] at [2007-10-2 19:30:59]

Call 'new' on each element in the array before attempting to use one of the array element's methods. It *seems* like Java will create 15 new instances of the array (because of the 'new' keyword). However, it only allocates references for 15 elements. To actually have instances, you need to assign each reference to an instance, via the 'new' operator.
- Saish
Saisha at 2007-7-13 21:18:18 >

unfortunately the rest of the program relies on being able to change the number of elements in the array, so in the end a variable would take the place of the 15 in
public static PlayerRecord[] currentPlayers = new PlayerRecord[15];
so that would not work
im surprised java isnt up there in the top reasons for suicide
actually now that i think about it, i could make it work by having a definite amount of elements in the array, but the array of names cannot be read since they are connected to the Round method
so basically the creation of the array of currentPlayers has to be before the Round method, yet the naming of the players has to happen in the Round method, is there any way this can be done?
Message was edited by:
s34nsm411
> unfortunately the rest of the program relies on being
> able to change the number of elements in the array,
> so in the end a variable would take the place of the
> 15 in
> public static PlayerRecord[] currentPlayers =
> new PlayerRecord[15];
>
> so that would not work
>
It 'works', just not the way you supposed it would. :^)
It initializes an array of 15 elements with references to type Round. You need to assign those references to instances. The only way to create instances is via the 'new' keyword.
> im surprised java isnt up there in the top reasons
> for suicide
>
It's too early in the week for me to start drinking.....
>
>
> actually now that i think about it, i could make it
> work by having a definite amount of elements in the
> array, but the array of names cannot be read since
> they are connected to the Round method
>
Correct me if I am wrong, but you do already have a definite number of elements in your array. 15.
> so basically the creation of the array of
> currentPlayers has to be before the Round method, yet
> the naming of the players has to happen in the Round
> method, is there any way this can be done?
>
Round is not a method. It is your class's constructor (I think). Right before calling the setter for 'name', call 'new' and assign the created Round instance to that array element (in your case '0').
Then call 'name'.
> Message was edited by:
> s34nsm411
- Saish
Saisha at 2007-7-13 21:18:18 >

Think of it this way: the ' new' element for your array created an array. You then need to call 'new' to create each element in the array.- Saish
Saisha at 2007-7-13 21:18:18 >

ohhh i see what you mean now, and yeah i meant constructor not method, sorrythanks!
Glad I could help. Best of luck.- Saish
Saisha at 2007-7-13 21:18:18 >

gah, now all of the names i just assigned arent visible to a method i created just below the Round constructor. the constructor, method, and all the instances are public. anyone know what im doing wrong here?
public class Round
{
public static int playerCount;
public static PlayerRecord[] currentPlayers = new PlayerRecord[15];
public Round(int numberofplayers, String[] namesarray)
{
playerCount = numberofplayers;
for(int index = 0; index < 15; index++)
currentPlayers[index] = new PlayerRecord(namesarray[index]);
//here i can view the currentplayers[whatever].name fine
}
public static void NotPlayed()
{
//buthere all hell breaks loose
System.out.println(currentPlayers[0].name);
}
}
Okay. First things first. No one here can help you solve 'all hell is breaking loose'. What is the actual problem? Do you get a stack trace? Compilation error? Etc. Try to explain what you have done and show the exact error you are getting.
Second, even without seeing the above, how are you calling your new method? Why is it static? My guess is that you are invoking it from main(), but that is only a guess.
- Saish
Saisha at 2007-7-13 21:18:18 >

sorry about the vagueness. when you asked how it was called i realized that might have had something to do with it, and the problem infact was that i hadnt called the constructor yet, only the method. i assumedthat java could see all the other parts of the program reguardless if it had been called before which dosent really make any sense now that i think about it
so im good now, thanks for all your help once again
This may sound random, but I think you are well on your way! Most posters here require an explicit solution. You seem to be able to find your own way after just a few questions. Pretty soon, you'll ask yourself these questions on your own. So, keep it up!:^)- Saish
Saisha at 2007-7-13 21:18:18 >
