Reputation: 11
How do I put an object into my array? This is my array.
public static Player [] playerArray;
Player[] playerArray = new Player [2];
player
public class Player {
private String wpm;
private String mistakes;
private String time;
public Player (String nwpm,String nmistakes, String ntime){
wpm = nwpm;
mistakes = nmistakes;
time = ntime;
}
public String getWPM(){
return wpm;
}
public String getMistakes(){
return mistakes;
}
public String getTime(){
return time;
}
}
I kept getting this error
Exception in thread "main" java.lang.NullPointerException
whenever I tried to use player. Did I do something wrong? Is there anything else you need?
Edit : adding where the errors happened
public void setPlayer1(Player p){
p1WPM.setText("8");
p1Mis.setText(p.getMistakes());
p1Time.setText(p.getTime());
}
Upvotes: 0
Views: 1278
Reputation: 3697
Here is working code for you.
public class Player {
public static final int SIZE = 2;
public static Player[] playerArray = new Player[SIZE]; // Creates array of
// Player of size
// SIZE
private String wpm;
private String mistakes;
private String time;
public Player(String wpm, String mistakes, String time) {
this.wpm = wpm;// this way is more readable
this.mistakes = mistakes;
this.time = time;
}
public String getWPM() {
return wpm;
}
public String getMistakes() {
return mistakes;
}
public String getTime() {
return time;
}
public static void main(String[] args) {
/*
* If I wan to add something to the array it has to be object of the
* array type and I need to create it first
*/
// Create 2 Player objects
Player player1 = new Player("p11", "p12", "p13");
Player player2 = new Player("p21", "p22", "p23");
// arrays are indexed from 0 to size-1
playerArray[0] = player1;
playerArray[1] = player2;
}
}
Upvotes: 0
Reputation: 4135
Exception in thread "main" java.lang.NullPointerException
Player[] playerArray = new Player [2];
With this statement you are just creating an array of size 2
whose all elements are null
. But you are not creating elements here.
You have to instantiate each element.
for(int i=0; i< playerArray.length;i++)
playerArray[i] = new Player();//with appropriate constructor
If you are trying to perform any operation on playerArray
like playerArray[0].mistakes
then get NullPointerException
. You should initialize each element in the array with a Player
object.
Upvotes: 0
Reputation: 5959
You have declared playerArray
twice which is wrong. Also, you have not shown the code where you are getting the NullPointerException
.
If you are newbie then try the below code:
Player[] playerArray = new Player [2]; //declare array of size 2
Player p1 = new Player("nwpm","nmistakes","ntime"); // first player object
Player p2 = new Player("nwpm","nmistakes","ntime"); // second player object
playerArray[0] = p1; // first player added in the array
playerArray[1] = p2; // second player added in the array
Upvotes: 2
Reputation: 293
Try this:
Player a = new Player("a","b","c");
Player[] playerArray = new Player [2];
playerArray = new Player[2];
playerArray[0]=a;
System.out.println(playerArray[0].getTime()+" "+playerArray[0].getMistakes()+" "+playerArray[0].getWPM());
Result: c b a
Upvotes: 0
Reputation: 11
Player[] playerArray = new Player [2];
is creating the array, not the objects.
You should add
playerArray[0] = new Player(param1, param2, param3);
playerArray[1] = new Player(param1, param2, param3);
to have all items have their object
Upvotes: 0
Reputation: 5831
For example to add a Player object in array you can do:
Player[] playerArray = new Player [2];
//Create new Player object using the defined constructor
p = new Player("a","b","c");
// Assign p to 1st index of array
playerArray[0] = p;
Upvotes: 2