This question has already been answered:
I receive json from the server in order to transfer it to the custom adapter in the future. When starting the application crashes
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add (java.lang.Object)' on a null object reference
How to fix?
public class JSONTask extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { HttpURLConnection connection = null; BufferedReader reader = null; while (true) { try { URL url = new URL(params[0]); connection = (HttpURLConnection) url.openConnection(); connection.connect(); InputStream stream = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(stream)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = reader.readLine()) != null) { buffer.append(line); } return buffer.toString(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } try { if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } } @Override protected void onPostExecute(String result) { super.onPostExecute(result); if (result != null) { JSONObject jsonObject = null; try { jsonObject = new JSONObject(result); JSONArray jsonArray = jsonObject.getJSONArray("wall"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject productObject = jsonArray.getJSONObject(i); UList.add(new CSD( productObject.getString("name"), productObject.getString("text") )); } } catch (JSONException e) { e.printStackTrace(); } } else { } } }