0

I have this code to bring up a message if my list is empty. The first time it works and I get my JOptionPane. However, if I add an item to the list and then remove it and hit remove if the list is empty again, I get NullPointerException error. Is there a reason for this?

KnowledgeGeek
  • 197
  • 1
  • 1
  • 11
  • interesting read: http://stackoverflow.com/questions/4716353/if-catching-null-pointer-exception-is-not-a-good-practice-is-catching-exception – donfuxx Feb 25 '14 at 23:13

1 Answers1

3

The culprit is :

 String selectListValue = selectionList.getSelectedValue().toString();

and also

  if(selectListData.size() > 0) 
  // Null pointer exception will be thrown is selctionData is Null

In this you are not checking if selectionList is null. Ideally you should check if a object is null before performing any operation on it.

Correct Way :

if(selectionList != null)
{
    String selectListValue = selectionList.getSelectedValue().toString();
   // perform yoour operations
}

Also change :

  if(selectListData != null && selectListData.size() > 0)
Kakarot
  • 4,252
  • 2
  • 16
  • 18