web-dev-qa-db-fra.com

Tentative d'appel de la méthode d'interface 'booléen Java.util.List.add (Java.lang.Object)' sur une référence d'objet null

MainActivity.Java a le code suivant:

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

J'ai créé deux classes de modèles.

Tout d'abord, 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;
}
}

Le second, 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;
}
}

Lorsque j'exécute mon application, une erreur se produit:

Java.lang.NullPointerException: tentative d'appel de la méthode d'interface 'boolean Java.util.List.add (Java.lang.Object)' sur une référence d'objet nul à com.softjourn.redmineclient.models.IssuesResponse. (IssuesResponse.Java:26 ) sur com.softjourn.redmineclient.activities.MainActivity $ Login.onPostExecute (MainActivity.Java:125) sur com.softjourn.redmineclient.activities.MainActivity $ Login.onPostExecute (MainActivity.Java:79) sur Android.os.AsyncTask. terminer (AsyncTask.Java:636) sur Android.os.AsyncTask.access 500 $ (AsyncTask.Java:177) sur Android.os.AsyncTask $ InternalHandler.handleMessage (AsyncTask.Java:653) sur Android.os.Handler.dispatchMessage ( Handler.Java:102) sur Android.os.Looper.loop (Looper.Java:135) sur Android.app.ActivityThread.main (ActivityThread.Java:5254) sur 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:69 8)

J'attends vos suggestions pour savoir comment le réparer.

16
Vitaly Romaniv

Vous n'avez pas initialisé List<Issue> dans la classe IssuesResponse. Essayer

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