Note that there are some explanatory texts on larger screens.

plurals
  1. POJson parse from URL
    primarykey
    data
    text
    <p>I am getting java.lang.NullPointerException when I run this. Does anyone know why this is happening and how I can fix it?</p> <p>Please take a look at my code and let me know if you have any suggestions.</p> <pre><code>{"begin":[{"id":1,"name":"Andy","size":1}],"open":[{"id":1,"name":"Tom","size":2}]} </code></pre> <p><strong>Fragment</strong></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><strong>JSONParser</strong></p> <pre><code>import android.util.Log; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url) { // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } } </code></pre> <p><strong>Error:</strong></p> <pre><code>E/JSON Parser﹕ Error parsing data org.json.JSONException: Value &lt;!DOCTYPE of type java.lang.String cannot be converted to JSONObject 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> <p><strong>EDIT (Answer)</strong></p> <p><strong>JSONParser</strong></p> <p>Inside of JSONParser I changed my code to this:</p> <pre><code>public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url) { // Making HTTP request try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpUriRequest request = new HttpGet(url); request.setHeader("Accept", "application/json"); HttpResponse response = httpClient.execute(request); HttpEntity httpEntity = response.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } } </code></pre> <p><strong>Inside of onPostExecute</strong></p> <pre><code>JSONArray begin = json.getJSONArray(TAG_BEGIN); for (int i = 0; i &lt; begin(); i++) { try { JSONObject b = = begin(i); String id = b.getString(TAG_ID); String name = b.getString(TAG_NAME); String size = b.getString(TAG_SIZE); } catch (JSONException e) { e.printStackTrace(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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