-2

My main activity:

public class DiaryActivity extends AppCompatActivity {

private ArrayList<String> allURL = new ArrayList<>();
public void setList(ArrayList<String> list) {
        this.allURL = list;
    }

    private void fireYourAsyncTask() {
        new shitson(this).execute();
    }

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary);
        fireYourAsyncTask();
}

My other .java:

public class shitson extends AsyncTask<Void, Void, ArrayList<String>> {

ArrayList<String> tmp;
private DiaryActivity activity;

public shitson(DiaryActivity diaryActivity) {
}

protected ArrayList<String> doInBackground(Void... arg0) {
    tmp = new ArrayList<>();
    HttpHandler sh = new HttpHandler();
    String url2 = "https://................";
    String jsonStr = sh.makeServiceCall(url2);

    if (jsonStr != null) {
        try {

            JSONArray urlOfURLs = new JSONArray(jsonStr);
            for (int i = 0; i < urlOfURLs.length(); i++) {
                JSONObject JSONURL = urlOfURLs.getJSONObject(i);
                String url = JSONURL.getString("url");
                tmp.add(url);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    writeout();
    return null;
}

@Override
protected void onPostExecute(ArrayList<String> strings) {
    super.onPostExecute(strings);
    activity.setList(tmp);
}

public void writeout() {
        for (String i : tmp) {
            Log.e("-0-0-0-0-0-0-0-", i);
        }
    }
}

What I want to have the list I made in shitson.java in my DiaryActivity. In the shitson.java the writeout() function writes the right informations in Logcat but at activity.setList(tmp); I get error messages like these:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.petkovics.mango.DiaryActivity.setList(java.util.ArrayList)' on a null object reference
    at com.example.petkovics.mango.shitson.onPostExecute(shitson.java:57)
at com.example.petkovics.mango.shitson.onPostExecute(shitson.java:20)

What is non-sense since the writeout() function after I filled up the "tmp" ArrayList, it tells me every single data it has to do. But still in onPostExecute it gets crazy...

Laklam
  • 19
  • 3

1 Answers1

1

You have

private DiaryActivity activity;

which defines an uninitialized DiaryActivity object named activity.

And you have the constructor

public shitson(DiaryActivity diaryActivity) {
}

which takes a DiaryActivity object as argument but does nothing with it, and more importantly the constructor is leaving activity uninitialized (and therefore null).

Then you use activity even though it's a null object!

Your constructor should probably set the activity member to the passed object:

public shitson(DiaryActivity diaryActivity) {
    activity = diaryActivity;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Seems legit but sadly this did not fix the problem that I do not get back the list in my DiaryActivity. The allURL list is empty... But atleast I do not get any errors... – Laklam Sep 19 '18 at 21:31
  • 2
    @Laklam That's a problem for another question. One question per question please (unless they are *very* related, which your two problems aren't). – Some programmer dude Sep 19 '18 at 21:32
  • So it would be lovely if I could make another question but I can not. – Laklam Sep 19 '18 at 21:43