web-dev-qa-db-ja.com

nullオブジェクト参照でインターフェイスメソッド 'boolean Java.util.List.add(Java.lang.Object)'を呼び出そうとしました

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

2つのモデルクラスを作成しました。

まず、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;
}
}

2番目の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:com.softjourn.redmineclient.models.IssuesResponse。(IssuesResponse.Java:26のnullオブジェクト参照でインターフェイスメソッド 'boolean Java.util.List.add(Java.lang.Object)'を呼び出そうとしました。 )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) 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:69 8)

修正方法についてのご提案をお待ちしております。

16
Vitaly Romaniv

IssuesResponseクラスでList<Issue>を初期化していません。試して

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