Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JList - Null Pointer Exception

 
Karen Davis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any help with this would be greatly appreciated!!
I have a Jlist that displays a list of accounts in an application.Its working fine - i have a list selection item on the list that brings up an account when its name is selected in the list. The problem happens when I click on a menu item to create a new account after I've been using the JList I get a nullPointerException in the "String selectedAccount = accountJList.getSelectedValue().toString();" line below. Am i supposed to deselect the value? Really dont have a clue whats going on - here is my code for the listSelector
private class AccountSelector implements ListSelectionListener {
public void valueChanged (ListSelectionEvent event) {
//if (!event.getValueIsAdjusting()) {
String selectedAccount = accountJList.getSelectedValue().toString();
AccountList instance = AccountList.getAccountList();
ArrayList accounts = instance.returnList();
Iterator accountsIterator = accounts.iterator();
while(accountsIterator.hasNext()){
Account account = (Account)accountsIterator.next();
if(account.getName().equals(selectedAccount)){
Account accountToDisplay = account;
Point point = new Point(1,1);
mainFrame.displayAccount(accountToDisplay, point, mainFrame.getDesktopPaneBounds());
//String[] accountNames = instance.getAccountNames();
//setJList(accountNames);
}//end if
}// end while
}//end if
}//end valueChanged
}//end inner class AccountSelector
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the line number that "String selectedAccount = accountJList.getSelectedValue().toString();" is on is the one that is reporting a null pointer exception, there are only two things that are causing it: either "accountJList" is null, or "getSelectedValue()" is returning null. You should try and test for both of these problems, but I'm guessing that it's the second. In fact, in the API for this method it says, "Returns the first selected value, or null if the selection is empty." I'm betting that the menu item that adds a new account is (either on purpose, or not) causing this.
 
Karen Davis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,
that definitely seems to be the problem, when I add a new account using the menu item the Jlist also gets updated, if an item has already been selected it gives the null pointer exception - any ideas on how to ensure that the getValueSelected doesnt equal null between selecting an item and adding a new account/updating the list.
thanks for your help
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code for the JMenuItem action listener that adds a new item? I think that's actually where the problem is...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic