尝试在空对象引用上调用接口方法 'boolean java.util.List.add(java.lang.Object)'

主要活动.java具有以下代码:

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);
    }
}
}

我创建了两个模型类。

首先,问题响应.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<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;
}
}

第二个问题,问题.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;
}
}

当我运行我的应用程序时,发生错误:

java.lang.NullPointerException:尝试在 null object reference 上调用接口方法 'boolean java.util.List.add(java.lang.Object)' 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) atandroid.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)

我等待您的建议,如何解决它。


答案 1

您尚未在问题响应类中初始化。尝试List<Issue>

private List<Issue> issues = new ArrayList<>();

答案 2