-1

I need to retrieve a double value from a database, which could potentially be stored as null, so I need to detect whether or not it is null before or after retrieving it. However, the method getDouble("columnName") actually returns 0 if the value in the database is null.

I have tried the solutions provided here to no avail (Android Studio says it's unable to resolve wasNull() and getObject():

How do I in JDBC read a possibly null double value from resultSet?

Here is what I've tried so far:

beer.setRating(beerObj.getDouble("myRating"));
if(beerObj.wasNull()){ //'Cannot resolve method wasNull'
}

And this:

Double doubleVal = (Double)beerObj.getObject("myRating"); //'Cannot resolve method getObject()' 

I have other properties that I am retrieving using getString("otherColumnName") and that is retrieved properly so I assumed that the beerObj was retrieved properly as well.

Is there another way to check if the double value is null in the database before retrieving it?

Community
  • 1
  • 1
VIN
  • 6,385
  • 7
  • 38
  • 77
  • 1
    Both `wasNull()` and checking `getObject("columnName") == null` are valid solutions. Could you please share the errors you're getting? – Mureinik Aug 25 '15 at 20:22
  • Hi @Mureinik, when I do the following: beer.setRating(beerObj.getDouble("myRating")); if(beerObj.wasNull()){ } I get "Cannot resolve method wasNull()". Also, when I try the getObject() method, I get "Cannot resolve method "getObject()" – VIN Aug 25 '15 at 20:25
  • but what error are you getting? Can you please edit your question with the code snippet and the exact error? – Mureinik Aug 25 '15 at 20:26
  • @Mureinik, I'm not sure I understand what you're asking for me to post. I did add the lines that I'm having issues with in my original post. As I said in my comment, Android Studio is unable to resolve those methods, so I cannot compile it without first resolving those methods. In that case, there won't be compile errors. – VIN Aug 25 '15 at 20:35
  • Why are you using the JDBC tag and citing JDBC questions when your question is about JSON? – user207421 Aug 25 '15 at 21:23

2 Answers2

0

Yes put it in your where clause: "WHERE columnname is not null". If there is a record returned then it isn't null if there isn't a record returned then it is null.

brso05
  • 13,142
  • 2
  • 21
  • 40
  • Thanks @brso05, this solution worked. I ended up doing what you suggested, and then using the beerObj.has("myRating") to check if that property exists. I have to admit though, I am a little disappointed that I cannot check for a null value from the front end. Thanks again! – VIN Aug 25 '15 at 20:48
  • @Koercion no problem glad I could help! – brso05 Aug 25 '15 at 20:48
0

Try if (resultset.getObject("columnName") == null). It's hard to tell from your original post if you were trying to use getObject() without passing in a value.

MHardwick
  • 659
  • 3
  • 9
  • Hi @MHardwick. I entered if(beerObj.getObject("myRating")==null) but I still get the 'Cannot resolve method getObject()' message. – VIN Aug 25 '15 at 20:41
  • Can you add some more context? Specifically, from the declaration of beerObj on? – MHardwick Aug 25 '15 at 20:46
  • I retrieve a response from the database as a JSON object. JSONObject beerObj = beersJSONArr.getJSONObject(i); (the i is because it is in a for loop). The rest of the properties are retrieved properly, and the code compiles and the data is retrieved properly when I comment out the getDouble line. – VIN Aug 25 '15 at 20:48
  • Ah, ok! You're using a JSONObject instead of a ResultSet. JSONObjects don't have wasNull or getObject methods. Try `if (beerObj.isNull("myRating"))` instead. – MHardwick Aug 25 '15 at 20:57