0

I have a a populated JTable with various data. Now I'm trying to convert it into a String but in my DB they can be a null value so when I call this line:

String s = model.getValueAt(i,j).toString();

I get an exception, it's because it tries to convert a null value into string right?

How do I make it work?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mimi87
  • 53
  • 1
  • 9
  • 5
    `Object val = model.getValueAt(i,j); String s = val == null ? "" : val.toString();` – alex2410 Jan 21 '15 at 15:33
  • 3
    1) Always copy/paste error and exception output! 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 3) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Jan 21 '15 at 15:35

2 Answers2

1

You are getting a NullPointerException because when you retrieve the information from your data storage, you do not check whether or not it is null. So when you call model.getValueAt(i,j), this is most likely returning a null value at some point. You do not check this, so you are calling toString() on a null value (null.toString()), which will cause your code to crash with a NullPointerException. I would suggest you use a turnary operator. This will check whether or not the value returned is null first, and then create the String s depending on whether or not it was null.

String s = ((model.getValueAt(i,j) == null) ? null : model.getValueAt(i,j).toString());

This will set your String to null if the record is null, otherwise set it to the record's value. OR, if you would like it to be an empty String ("") when the record does not exist, you can use:

String s = ((model.getValueAt(i,j) == null) ? "" : model.getValueAt(i,j).toString());

Mike Elofson
  • 2,017
  • 1
  • 10
  • 16
  • he has exception when null... so it must transform `null` into `""` `== null) ? "" : model`.... – Jordi Castilla Jan 21 '15 at 15:44
  • 1
    My assumption is that the NPE was because the record (`model.getValueAt(i,j)`) was `null`, and if you call `.getString()` on a `null` object, it will cause a NPE. The poster did not state whether or not they wanted the value of `String s` to be `null` or `empty` when the record returned from `model.getValueAt(i,j)` is `null`. I've updated my answer to include both the empty and `null` variations, although my first answer correctly supports the original question. @JordiCastilla – Mike Elofson Jan 21 '15 at 15:47
  • 1
    @JordiCastilla, he probably gets an exception because `null.toString()` produces a null pointer exception. This would happen if the cell he retrieves from the table returns `null`. No where did the author of the question say he wants to convert a null into an empty string. – Amr Jan 21 '15 at 15:49
  • 2
    I've updated my answer again to explain why the NPE is happening. @GigaStore you are correct. Whoever downvoted, did it far too hastily without correctly reading the question that was asked. – Mike Elofson Jan 21 '15 at 15:51
0
Object cellObj = model.getValueAt(i,j);
String s;
if (cellObj == null) {
    s = null;
} else {
    s = cellObj.toString();
}
Amr
  • 2,420
  • 2
  • 17
  • 26
  • 1
    @Tom, was typo :).. fixed – Amr Jan 21 '15 at 15:38
  • JTable (valid for Java too) hasn't any issue with null, then set the String doesn't make me any sence (even can generete desribed NPE), not convert to the String, Object can do that ... – mKorbel Jan 21 '15 at 20:34