Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
MainActivity.java有以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 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<String, Void, String> { @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<Issue> adapter = new ArrayAdapter<Issue>(MainActivity.this, android.R.layout.simple_list_item_1, ir.getIssues()); mListIssues.setAdapter(adapter); } } } |
我创建了两个模型类。
一,IssuesResponse.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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<Issue> issues; public IssuesResponse(JsonArray ja) { if (issues == null) { } for(JsonElement je : ja) { Issue issue = new Issue(je); issues.add(issue); } } public List<Issue> getIssues() { return issues; } } |
第二个,Issue.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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; } } |
当我运行我的应用程序时出现错误:
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)
我等待你的建议如何解决。
您尚未在IssuesResponse类中初始化
1 | private List<Issue> issues = new ArrayList<>(); |