Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON Array NullPointerException
    text
    copied!<p>What would be the best way to parse this in an Android app? I am getting java.lang.NullPointerException when I run the app. Please take a look at my code and let me know if you have any suggestions.</p> <pre><code>{"begin":[{"id":1,"name":"Bob","size":2}],"open":[{"id":1,"name":"Mike","size":2}]} </code></pre> <p>This is the code that I have.</p> <pre><code>public class MainFragment extends Fragment { public MainFragment() {} //URL to get JSON Array private String url = "URL..."; //JSON Node Names private static final String TAG_BEGIN = "begin"; private static final String TAG_ID = "id"; private static final String TAG_NAME = "name"; private static final String TAG_SIZE = "size"; JSONArray begin = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); new JSONParse().execute(); return rootView; } private class JSONParse extends AsyncTask&lt;String, String, JSONObject&gt; { private ProgressDialog pDialog; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(HomeActivity.this); pDialog.setMessage("Getting Data ..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override protected JSONObject doInBackground(String... args) { JSONParser jParser = new JSONParser(); // Getting JSON from URL JSONObject json = jParser.getJSONFromUrl(url); return json; } @Override protected void onPostExecute(JSONObject json) { pDialog.dismiss(); try { // Getting JSON Array begin = json.getJSONArray(TAG_BEGIN); JSONObject c = begin.getJSONObject(0); // Storing JSON item in a Variable String id = c.getString(TAG_ID); String name = c.getString(TAG_NAME); String size = c.getString(TAG_SIZE); } catch (JSONException e) { e.printStackTrace(); } } } } </code></pre> <p>When I run the activity I get this error:</p> <pre><code>FATAL EXCEPTION: main java.lang.NullPointerException at ...$MainFragment$JSONParse.onPostExecute(MainActivity.java:399) at ...$MainFragment$JSONParse.onPostExecute(MainActivity.java:373) </code></pre> <p>Which is...</p> <pre><code>begin = json.getJSONArray(TAG_BEGIN); </code></pre> <p>and...</p> <pre><code>private class JSONParse extends AsyncTask&lt;String, String, JSONObject&gt; { </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload