0

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());
}
4
  • 5
    at which line the NullPointerException occur? You are declaring playerArray twice Commented Jan 21, 2016 at 5:55
  • 3
    show your main() method code.Also, why are you declaring playerArray twice? Commented Jan 21, 2016 at 5:56
  • how you are calling setPlayer1()? Add proper code not just snippets which can't be glued together Commented Jan 21, 2016 at 6:15
  • 1
    Where these objects are created p1WPM, p1Mis, p1Time Commented Jan 21, 2016 at 6:26

6 Answers 6

2

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;
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

Comments

0

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

Comments

0

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.

Comments

0

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;

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.