aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JList - Null Pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JList - Null Pointer Exception" Watch "JList - Null Pointer Exception" New topic

JList - Null Pointer Exception

Karen Davis

Joined: Mar 04, 2004
Posts: 8
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();
Account account = (Account);
Account accountToDisplay = account;
Point point = new Point(1,1);
mainFrame.displayAccount(accountToDisplay, point, mainFrame.getDesktopPaneBounds());
//String[] accountNames = instance.getAccountNames();
}//end if
}// end while
}//end if
}//end valueChanged
}//end inner class AccountSelector
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

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.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Karen Davis

Joined: Mar 04, 2004
Posts: 8
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

Joined: Oct 18, 2000
Posts: 4121

Can you post the code for the JMenuItem action listener that adds a new item? I think that's actually where the problem is...
I agree. Here's the link:
subject: JList - Null Pointer Exception
Similar Threads
valueChanged can't throw SQLException??
about swing--please give me an oracle!!
JList Deselection
Populating JList in Parent window from Child window's save button
JList deleting multiple items