Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Caused by: java.lang.NullPointerException TableView in getSelectedItem() method

 
Ertan Hasani
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get an ID from SelectedItem() in a TableView from another class, but it gives me a NullPointerException. Can someone please help me solve this.

p.s I set selectFirst() method , because I thought that the error was because when the program starts none of them is selected, but still the same.

Here's the code:

KonsumatoretWindowController.java:


ShikoKonsumatorinController.java:


OUTPUT:
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So... either table is null, or table.getSelectionModel() is null, or table.getSelectionModel().getSelectedItem() is null. But it seems that you've already homed in on the reason:

Ertan Hasani wrote:I thought that the error was because when the program starts none of them is selected


In other words, table.getSelectionModel().getSelectedItem() is null because no item in the table is selected. You're going to have to decide what that "merrId()" method should do if that is the case. It seems like you tried to force an item to be selected, but I wouldn't have done that because it could happen that the table is empty and there are no items to be selected.

You could have the method return null if no item is selected, for example. Or if that's a serious situation which should not happen then you could throw an exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic