-3

bit of a newbie and trying to avoid a null pointer exception in a statement, so I want to set the value to an empty string. I am using the following code but it doesn't seem to be working, the value is still null.

row[2]==null? "" : row[2].toString()

Any help would be apreciated

Hi there, I tried with the 4 spaces, I am still left with a null value.

steve
  • 5
  • 2

1 Answers1

2

I'm guessing you want to assign a defaultValue to row[2] which can be achieved like below:

row[2] = row[2] == null ? defaultValue : row[2];

but a normal if would make more sense and be even clearer:

if(row[2] == null){
     row[2] = defaultValue;
} 
Lino
  • 19,604
  • 6
  • 47
  • 65
  • while (iter.hasNext()) – steve Dec 28 '17 at 19:35
  • while (iter.hasNext()) { Object[] row = (Object[]) iter.next(); vt = new TestObject(row[0].toString() row[1].toString(), row[2]==null ? "" : row[2].toString(), row[3].toString()); pendingList.add(vt); } row 2 is null and I get an error when I try to save data. I want to set the value to blank so I do not get the null error. – steve Dec 28 '17 at 19:39