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

时间:2015-07-12 14:26:03

标签: java android list android-studio stack-trace

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

我创建了两个模型类。

首先, 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<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

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:尝试调用接口方法&#39; boolean java.util.List.add(java.lang.Object)&#39;在null对象引用上               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)               在android.os.AsyncTask.finish(AsyncTask.java:636)               在android.os.AsyncTask.access $ 500(AsyncTask.java:177)               在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:653)               在android.os.Handler.dispatchMessage(Handler.java:102)               在android.os.Looper.loop(Looper.java:135)               在android.app.ActivityThread.main(ActivityThread.java:5254)               at java.lang.reflect.Method.invoke(Native Method)               在java.lang.reflect.Method.invoke(Method.java:372)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我等待你的建议如何修复。

1 个答案:

答案 0 :(得分:74)

您尚未在IssuesResponse类中初始化List<Issue>。尝试

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