Null pointer error in async task

I get a null pointer exception when processing JSON data, I checked that the JSON that I get is correct. further I want to save this data in a hash map and pull it out one by one in accordance with the requirements.

public ProgressDialog pDialognew; static JSONParser jParser = new JSONParser(); ArrayList<HashMap<String, String>> allValueList; class SearchData extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); pDialognew = new ProgressDialog(Activity.this); pDialognew.setMessage("Loading. Please wait..."); pDialognew.setIndeterminate(false); pDialognew.setCancelable(true); pDialognew.show(); } @Override protected String doInBackground(String... params) { runOnUiThread(new Runnable() { @Override public void run() { int success; try { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("match",pName)); JSONObject json = jParser.makeHttpRequest(GlobalServerLocation.url_pol, "GET", params); success = json.getInt("success"); if (success == 1) { JSONArray productObj = json.getJSONArray("object"); // JSON Array Log.i("val", productObj.toString()); // Correct JSON total = productObj.length(); // Correct number of rows Log.i("Len", String.valueOf(total)); for (int j = 0; j < productObj.length(); j++) { JSONObject productnew = productObj.getJSONObject(j); String LS = productnew.getString(TAG_LS); String CN = productnew.getString(TAG_CN); String SN = productnew.getString(TAG_SN); String PN = productnew.getString(TAG_PN); HashMap<String, String> map = new HashMap<String, String>(); map.put(TAG_LS, LS); map.put(TAG_CN, CN); map.put(TAG_SN, SN); map.put(TAG_PN, PN); allValueList.add(map); } } else { Log.i("Error", " Some Error from PHP "); } } catch (JSONException e) { Log.i("Error", e.toString()); } } }); return null; } @Override protected void onPostExecute(String file_url) { pDialognew.dismiss(); // Line 113 } } 

 08-23 17:17:33.449: E/AndroidRuntime(31930): FATAL EXCEPTION: main 08-23 17:17:33.449: E/AndroidRuntime(31930): java.lang.NullPointerException 08-23 17:17:33.449: E/AndroidRuntime(31930): at my.india.our.app$SearchData$1.run(app.java:113) 08-23 17:17:33.449: E/AndroidRuntime(31930): at android.os.Handler.handleCallback(Handler.java:587) 08-23 17:17:33.449: E/AndroidRuntime(31930): at android.os.Handler.dispatchMessage(Handler.java:92) 08-23 17:17:33.449: E/AndroidRuntime(31930): at android.os.Looper.loop(Looper.java:123) 08-23 17:17:33.449: E/AndroidRuntime(31930): at android.app.ActivityThread.main(ActivityThread.java:3729) 08-23 17:17:33.449: E/AndroidRuntime(31930): at java.lang.reflect.Method.invokeNative(Native Method) 08-23 17:17:33.449: E/AndroidRuntime(31930): at java.lang.reflect.Method.invoke(Method.java:507) 08-23 17:17:33.449: E/AndroidRuntime(31930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874) 08-23 17:17:33.449: E/AndroidRuntime(31930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632) 08-23 17:17:33.449: E/AndroidRuntime(31930): at dalvik.system.NativeStart.main(Native Method) 

If any other piece of code is required, let me know.

+4
source share
3 answers

Typically, AsyncTask is used as a new thread to perform heavy operations without blocking the main UI thread. But you used the code that I mentioned below inside doInBackground() AsyncTask, which again will make the process in the main thread (especially in higher-end mobile phones), blocking it. therefore, it is better to delete the following lines and try to run it.

 runOnUiThread(new Runnable() { @Override public void run() { } }; 
+1
source

There are several parts to AsyncTask: the doInBackground method, which actually works in a separate thread, and the onPostExecute method, which works in the user interface thread. The purpose of onPostExecute is to publish the results (for example, updating the hierarchy of views or setting text in text form) that must be executed in the user interface thread. It can also post updates. For all this to work correctly, you must create an AsyncTask and call the execution method in the user interface thread.

You should not invoke user interface actions from doInBackground - this will cause your application to crash.

0
source

First of all, move the pDialognew to your SearchData class.

Secondly, as Hari noted, getting rid of runOnUiThread things. Your doInBackground() method should look something like this:

 protected String doInBackground(String... params) { try { List<NameValuePair> params = new ArrayList<NameValuePair>(); // ... rest of your code } catch (JSONException e) { Log.i("Error", e.toString()); } return null; } 

Third, in your onPostExecute() method, make sure pDialognew is not null (although I don’t know how it can be null here):

 if (pDialognew != null) pDialognew.dismiss(); 
0
source

Source: https://habr.com/ru/post/1498588/


All Articles