title: Json exception causing android device to crash
body: <p>I have a jsonarray that is checked for a value worksheetID which is then stored and used in a variable later on. The problem is that when this value doesn't exist the android device crashes and even though I run it through a try catch block it won't stop it from crashing. </p>
  
  <p>Here is my code: </p>
  
  <pre><code>try {
      try
      {
          jsonArray = jsonObject.getJSONArray("WorksheetID");
          Log.v("Worked", "WorksheetID is in ");
  
      } catch (JSONException e)
      {
  
         Log.v("failed", "No WorksheetID");
          Log.v("failed", WorksheetID[0]+"");
          e.printStackTrace();
  
  
      }
  
      if(WorksheetID[0] != 0)
      {
          WorksheetID = new int[jsonArray.length()];
          for (int i = 0; i &lt; jsonArray.length(); i++) {
  
              try {
                  WorksheetID[i] = jsonArray.getInt(i);
              } catch (JSONException e)
              {
                  e.printStackTrace();
                  Log.v("failed", "cant do loop 2");
                  WorksheetID[0] = 0;
  
              }
          }
      }
      else
      {
          return;
      }
  }catch (Exception e)
  {
      WorksheetID[0] = 0;
  
  }
  </code></pre>
  
  <p>Here is the logcat error that I'm getting. for some reason the machine seems to shut down after the first catch without even performing all the lines in the catch itself! </p>
  
  <pre><code>    03-21 08:55:42.494  21224-21224/net.azurewebsites.cosy W/System.err﹕ org.json.JSONException: No value for WorksheetID
  03-21 08:55:42.494  21224-21224/net.azurewebsites.cosy W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:355)
  03-21 08:55:42.494  21224-21224/net.azurewebsites.cosy W/System.err﹕ at org.json.JSONObject.getJSONArray(JSONObject.java:549)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at net.azurewebsites.cosy.Classroom.onCreate(Classroom.java:127)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5231)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:135)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
  03-21 08:55:42.498  21224-21224/net.azurewebsites.cosy V/failed﹕ No WorksheetID
  03-21 08:55:42.506  21224-21224/net.azurewebsites.cosy D/AndroidRuntime﹕ Shutting down VM
  03-21 08:55:42.506  21224-21224/net.azurewebsites.cosy W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4d59b20)
  03-21 08:55:42.510  21224-21224/net.azurewebsites.cosy E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: net.azurewebsites.cosy, PID: 21224
      java.lang.RuntimeException: Unable to start activity ComponentInfo{net.azurewebsites.cosy/net.azurewebsites.cosy.Classroom}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
              at android.app.ActivityThread.access$800(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5001)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
              at net.azurewebsites.cosy.Classroom.onCreate(Classroom.java:163)
              at android.app.Activity.performCreate(Activity.java:5231)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
              at android.app.ActivityThread.access$800(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5001)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
  </code></pre>