Question

I'm still learning the basics of Java so please bear with me. I'm creating a simple soccer simulator game, right now I'm trying to populate the game with players from different countries.

I've created a for loop to go through an array of the instantiated object for players but I keep getting a null pointer exception. Here is my code:

public static void main(String[] args) {

        SoccerPlayers[] newPlayer = new SoccerPlayers[5];
        for (int i = 0; i < newPlayer.length; i++){
            newPlayer[i].PlayerCountry();
            newPlayer[i].PlayerPosition();
            newPlayer[i].PlayerName();
            newPlayer[i].PlayerAge();
            newPlayer[i].PrintSoccerPlayer();
        }

    }

I don't know if it's the way I did my for loop or the way the other class handles the method since it's pointing to the first method used. Here is a sample of the method from the other class.

public class SoccerPlayers extends SoccerMain {

    public String playerContinent = "";
    public String playerPosition = "";
    public String playerCountry = "";
    public String playerFirstName = "";
    public String playerLastName = "";
    public String playerName = "";
    public int playerAge = 0;

    public void PlayerCountry(){

        int continent = randomNum.nextInt(6) + 1;
        int country = 0;

        if (continent == 1){
            playerContinent = "North America";
        }else if (continent == 2){
            playerContinent = "South America";
        }else if (continent == 3){
            playerContinent = "Europe";
        }else if (continent == 4){
            playerContinent = "Africa";
        }else if (continent == 5){
            playerContinent = "Asia";
        }else{
            playerContinent = "Oceanica";
        }

        if (playerContinent == "North America"){
            country = randomNum.nextInt(100) + 1;
            if (country > 0 && country < 11){
                playerCountry = "Canada";
Was it helpful?

Solution

When creating an array of objects, the elements of the array are assigned default values i.e null. (dont get confused with arrays of array of primitives. for eg: in case of primitve int the default values would be 0).

Case 1: Array of Objects

   SoccerPlayers[] newPlayer = new SoccerPlayers[5];
   // now all elements of newPlayer are null
   System.out.println(newPlayer[0]); // prints null

Case 2 : Array of primitives

   int[] intArr = new int[5];
   // now all elements are initialized to 0 (default value for primitive int)
   System.out.println(intArr[0]); // prints 0

So you have to create new objects and store them in the array as per your need.

Change this :

 for (int i = 0; i < newPlayer.length; i++){
         // create objects of SoccerPlayers and store them in array
         newPlayer[i] = new SoccerPlayers(); 
         newPlayer[i].PlayerCountry();
         .........
         .........

OTHER TIPS

Your initialization of the array just creates an array of 5 nulls. you need to populate the list before you can iterate over it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top