Friday, 29 September 2017

android - Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference




MainActivity.java has following code:



package com.softjourn.redmineclient.activities;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.softjourn.redmineclient.R;
import com.softjourn.redmineclient.models.Issue;
import com.softjourn.redmineclient.models.IssuesResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

private final static String URI="https://redmineng.softjourn.if.ua/issues.json?assigned_to_id=me";
@Bind(R.id.list_issues) ListView mListIssues;
Login li;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);

li = new Login();
li.execute(URI);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

class Login extends AsyncTask {

@Override
protected String doInBackground(String... strings) {
HttpURLConnection c = null;
try {
c = (HttpURLConnection) new URL(strings[0]).openConnection();
} catch (IOException e) {
e.printStackTrace();
}
c.setUseCaches(false);
try {
c.setRequestMethod("GET");
} catch (ProtocolException e) {
e.printStackTrace();
}
c.setRequestProperty("Accept", "application/json");
c.setRequestProperty("Authorization", "basic " + Base64.encodeToString("osavchak:mhgT7322".getBytes(), 0));
try {
c.connect();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader((c.getInputStream())));
} catch (IOException e) {
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
String output;
try {
while ((output = br.readLine()) != null) {
sb.append(output);
}
} catch (IOException e) {
e.printStackTrace();
}

return sb.toString();
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
JsonArray ja = new JsonParser().parse(result).getAsJsonObject().getAsJsonArray("issues");
IssuesResponse ir = new IssuesResponse(ja);

ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, ir.getIssues());
mListIssues.setAdapter(adapter);
}
}
}


I've created two model classes.



First, IssuesResponse.java:



package com.softjourn   .redmineclient.models;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.annotations.SerializedName;
import java.util.List;

public class IssuesResponse {
@SerializedName("issues")
private List issues;

public IssuesResponse(JsonArray ja) {
if (issues == null) {

}
for(JsonElement je : ja) {
Issue issue = new Issue(je);
issues.add(issue);
}
}

public List getIssues() {
return issues;
}
}


The second one, Issue.java:



package com.softjourn.redmineclient.models;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.annotations.SerializedName;

public class Issue {
@SerializedName("id")
private int id;

@SerializedName("description")
private String description;

public Issue(JsonElement je) {
Issue issue = new Gson().fromJson(je,Issue.class);
this.id = issue.id;
this.description = issue.description;
}
}


When I run my application an error occurs:




java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.softjourn.redmineclient.models.IssuesResponse.(IssuesResponse.java:26)
at com.softjourn.redmineclient.activities.MainActivity$Login.onPostExecute(MainActivity.java:125)
at com.softjourn.redmineclient.activities.MainActivity$Login.onPostExecute(MainActivity.java:79)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)




I wait for your suggestions how it could be fixed.


Answer



You haven't initialised List in the IssuesResponse class. Try



private List issues = new ArrayList<>();

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...