0

I am trying to go through an object which has an array within it containing names and numbers of -

 public void showPlayerGameList(){
        playerTextView = findViewById(R.id.aPlayerBox);

        for(PlayerDetails aPlayer : playerListGame.myPlayers) {

            playerTextView.append(aPlayer.name + "\n");
        }
    }
}

But I get the following error:

Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.android.footysortit.PlayerDetails.name' on a null object reference

The way I am adding my player which is an object of PlayerDetails (which just contains a 2x String and an Int):

            if(phone.moveToFirst()) {
                PlayerDetails player = new PlayerDetails(); //TODO turn this into a loop to add each player the user picks and then display it
                player.name = phone.getString(phone.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
               // player.number =phone.getString(phone.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                playerListGame.addPlayer(player);

The idea is for the user to pick a contact and their name displays within a list but they can pick different contacts each time and each contact should be added to the list.

Any ideas?

Thank you

  • 2
    debug and check that phone.getString(phone.getColumnIndex(); is returning someting or not – Sunil Soni Mar 26 '18 at 12:47
  • According to error message it's look like user object is null so when you try to access name field of null object you get NPE. – contrapost Mar 26 '18 at 12:52
  • Can you show your PlayerDetails class? – contrapost Mar 26 '18 at 12:54
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – M. Prokhorov Mar 26 '18 at 13:00
  • @contrapost3 - public class PlayerDetails { int id; String name; String number; } –  Mar 26 '18 at 13:00
  • @SunilSoni The getString does return something. If i use the following: playerListGame.addPlayer(player); playerTextView = findViewById(R.id.aPlayerBox); playerTextView.append(player.name); It prints the name that I choose from the contacts list. –  Mar 26 '18 at 13:08
  • Therefore the only thing left we didn't see you initialise is `playerListGame.addPlayer(...)`. Where do you initialise the `playerListGame` Object? Try to log if it's value is null or not – L.Spillner Mar 26 '18 at 13:10
  • @L.Spillner I initialise is globally within the same class... –  Mar 26 '18 at 13:17

1 Answers1

0

When you are adding your players, check that the result of phone.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME) isn't null.

Currently it seems like a null value is set as the players name.

Perhaps there is another value you can try instead, or set the name to an empty string instead of null.

Sam
  • 670
  • 1
  • 6
  • 20
  • Hi, thanks for your reply. If i use the follow: playerListGame.addPlayer(player); playerTextView = findViewById(R.id.aPlayerBox); playerTextView.append(player.name); It works, so it shows that the player.name does hold a value. The name will appear in the text view –  Mar 26 '18 at 13:06
  • are you setting the value of player.name anywhere else? – Sam Mar 26 '18 at 13:09
  • Good question, I actually made player global at the start, so I moved it into my function and made it local. So every time they choose a contact it will now pass through the name and if they do it again, it will print the second name on another line. –  Mar 26 '18 at 13:17
  • Do you make and add to playerListGame.myPlayers a PlayerDetails, without setting a name on it anywhere? – Sam Mar 26 '18 at 13:21
  • Yea, so I create playerListGame object from the class PlayerList( which creates array of PlayerDetails called myPlayers) The idea is then to add a player to playerListGame with their contact details and then use playerListGame.myPlayers[i] to display their name. So while writing this I just noticed that I should be using: playerTextView.append(playerListGame.myPlayers[0].name+"\n"); and then maybe use a for loop to display the names.... –  Mar 26 '18 at 13:27
  • Glad we were able to work through it! – Sam Mar 26 '18 at 13:32
  • Same here, thanks for your help! –  Mar 26 '18 at 13:54