title: Testing for null JSON Objects in Java/Android
body: <p>I am getting this LogCat:</p>
  
  <pre><code>06-22 15:30:53.731: E/AndroidRuntime(2389): java.lang.NumberFormatException: Invalid float: "null"
  06-22 15:30:53.731: E/AndroidRuntime(2389):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
  06-22 15:30:53.731: E/AndroidRuntime(2389):     at java.lang.StringToReal.parseFloat(StringToReal.java:310)
  06-22 15:30:53.731: E/AndroidRuntime(2389):     at java.lang.Float.parseFloat(Float.java:300)
  06-22 15:30:53.731: E/AndroidRuntime(2389):     at java.lang.Float.valueOf(Float.java:337)
  </code></pre>
  
  <p>Here is code:</p>
  
  <pre><code>try
  {
      jObject = new JSONObject(result);
      starAvg = jObject.getString("AverageRating"); 
  }
  ratingsBar = (RatingBar) findViewById(R.id.theRatingBar);
  ratingsBar.setRating(Float.valueOf(starAvg));
  </code></pre>
  
  <p>Here is the context:</p>
  
  <p>In PHP, I am averaging total numbers in a column in a MySQL table.  When there are ANY rows, it will send back an average of the data in it and it encodes it, and Java picks it up as a JSON object.  But sometimes, there are cases where a table may have 0 rows, so I get this JSON Object:</p>
  
  <pre><code> {"AverageRating":null}
  </code></pre>
  
  <p>My app then crashes and the <code>LogCat</code> is as seen above.</p>
  
  <p>The <code>String</code> doesn't seem to care if it picks up a Null JSON Object but the app crashes when I do <code>Float.valueOf(theString)</code>.</p>
  
  <p>Another side note, I have tried to test this way:</p>
  
  <pre><code>if String is Null, Float = 0 
  if String is not null, Float.valueOF(String)
  </code></pre>
  
  <p>But it doesn't ever seem to read the String as null.  Is it actually NOT <code>null</code> in this case?</p>