S Fitz
S Fitz

Reputation: 1094

ImageView ArrayList gives NullPointerException

I'm having some trouble with the logic behind storing an ImageView into an ArrayList.

The application I'm developing keeps track of player statuses in a game. The user first adds Player objects (which keeps track of a status string and a status image to go with it) to an ArrayList (to keep track of them all). Then, after submitting all of the players, a screen pops up inflating a TableRow for each player, containing a button (to view the Player's profile), an ImageView (an icon representing the status), and a TextView (containing the player's status string value).

I don't have a problem with the buttons and loading each player's profile. The problem occurs with loading the "select status" GUI from dialog_select_icon.xml, particularly the ImageView ArrayList. I get a NullPointerException, which doesn't make sense to me because I'm doing it essentially the same way as I did the buttons.

//this code runs when user clicks a player's status icon
public void playerStatusIconClicked(View v)
{
    //loop through buttons to determine which player's button was clicked
    for (int i = 0; i < playerList.size(); i++)
    {
        if (v.getId() == playerStatusIVList.get(i).getId())
        {
            calledPlayer = i; //instance variable
            loadStatusIconGUI();
        }//if
    }//for
}//method playerStatusIconClicked


//showStatusIconGUI inflates the "select status icon" GUI
//and handles the user selecting an icon
private void loadStatusIconGUI()
{       
    //inflate the GUI for the showStatusIcon dialog (inflater is an instance variable)
    View view = inflater.inflate(R.layout.dialog_select_icon, null);
    //if the list has something in it, start from fresh
    if (!selectStatusIVList.isEmpty())
    {
        selectStatusIVList.clear();        
    }

    //list of icons in the "select status icon" dialog
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV0));
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV1));
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV2));
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV3));
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV4));
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV5));
    selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV6));

    //create a dialog so user can select an icon
    AlertDialog.Builder selectIconDialog = new AlertDialog.Builder(this);
    selectIconDialog.setView(view); //set the Dialog's custom view
    selectIconDialog.setTitle(R.string.title_select_icon);

    selectIconDialog.setNegativeButton(R.string.close, null);
    selectIconDialog.show();
}//showStatusIconGUI


//Handle clicks in the "select status icon" dialog
//Assigns a new status to the player
public void statusIconClicked(View v)
{
    Toast message;

    for (int i = 0; i < selectStatusIVList.size(); i++)
    {
        if (v.getId() == selectStatusIVList.get(i).getId())
        {
            message = Toast.makeText(
                MafiaTracker.this, "new status: " statusID[i], Toast.LENGTH_SHORT);
            message.show();
            playerList.get(calledPlayer).setImage(imageID[i]);
            playerList.get(calledPlayer).setStatus(statusID[i]);
        }
    }

    updateViewPlayerGUI(); 
}

Note that imageID[i] and statusID[i] are referring to int arrays containing the IDs for each status string and status image.

I can post the xml file but since it's 124 lines long I'd prefer not to. Just know that each ImageView in the xml file DOES have an ID, so I can't figure out why I'm getting these NullPointerExceptions, starting with the "if (!selectStatusIVList.isEmpty())" part, and continuing on with every other call after.

Please help!

Upvotes: 1

Views: 514

Answers (2)

Akhil
Akhil

Reputation: 14038

statusIconGUI seems to be the main layout xml you used in setContenView(). Consider the line :

selectStatusIVList.add((ImageView) statusIconGUI.findViewById(R.id.statusIV0));

you are using findViewbyID on statusIconGUI. Do that instead on the view instance of R.layout.dialog_select_icon which you inflated. so, change the above line to :

selectStatusIVList.add((ImageView) view.findViewById(R.id.statusIV0));

Upvotes: 1

user936414
user936414

Reputation: 7634

Initially selectStatusIVList is null. In loadStatusIconGUI check it for null

if(selectStatusIVList != null){
    if (!selectStatusIVList.isEmpty())
    {
         selectStatusIVList.clear();        
    }
}else{

     selectStatusIVList = new ArrrayList<Integer>();
}

Upvotes: 1

Related Questions