-2

I tried to load Json Object to Listview with simple adapter, but i return the following error.

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

i've check the JSON object is valid.

here is the json object

{
    "result": [
        {
            "nomor": "18",
            "id_ortu": "020202",
            "alasan_perizinan": "ada aja deh pokoknya",
            "tgl_izin": "2019-07-10",
            "lama_izin": "10",
            "tgl_kembali": "2019-07-20"
        },
        {
            "nomor": "17",
            "id_ortu": "020202",
            "alasan_perizinan": "ada aja",
            "tgl_izin": "2019-07-10",
            "lama_izin": "10",
            "tgl_kembali": "2019-07-20"
        },
        {
            "nomor": "16",
            "id_ortu": "020202",
            "alasan_perizinan": "ada dehhhhhhh",
            "tgl_izin": "2019-07-10",
            "lama_izin": "10",
            "tgl_kembali": "2019-07-20"
        },
        {
            "nomor": "14",
            "id_ortu": "020202",
            "alasan_perizinan": "hs",
            "tgl_izin": "2019-07-10",
            "lama_izin": "10",
            "tgl_kembali": "2019-07-20"
        }
    ]
}

here is my java

package com.budiluhur.almusyarrofahdigital;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.JsonReader;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DaftarPermintaanIzin extends AppCompatActivity {

    private String JSON_STRING;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listView = findViewById(R.id.ListPermintaan);

        getJSON();
    }

    private void showIzin() {
        JSONObject jsonObject = null;
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray("result");


            for (int i = 0; i < result.length(); i++) {
                JSONObject jsonObject1 = result.getJSONObject(i);
                String nomor = jsonObject1.getString("nomor");
                String id_ortu = jsonObject1.getString("id_ortu");
                String alasanPerizinan = jsonObject1.getString("alasan_perizinan");
                String tglIzin = jsonObject1.getString("tgl_izin");
                String lamaIzin = jsonObject1.getString("lama_izin");
                String tglKembali = jsonObject1.getString("tgl_kembali");
                HashMap<String, String> perizinan = new HashMap<>();
                perizinan.put("nomor", nomor);
                perizinan.put("id_ortu", id_ortu);
                perizinan.put("alasan_perizinan", alasanPerizinan);
                perizinan.put("tglIzin", tglIzin);
                perizinan.put("lamaIzin", lamaIzin);
                perizinan.put("tgl_kembali", tglKembali);
                list.add(perizinan);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(this, list, R.layout.list_izin, new String[]{"nomor", "id_ortu","alasan_perizinan","tglIzin","lamaIzin","tgl_kembali"}, new int[]{R.id.nomor1, R.id.id_ortu1,R.id.AlasanIzin1,R.id.TglIzin1,R.id.LamaIzin1,R.id.TglKembali1});

        listView.setAdapter(adapter);

    }


    private void getJSON() {
        class getJSON extends AsyncTask<Void, Void, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);

                Toast.makeText(DaftarPermintaanIzin.this,result, Toast.LENGTH_LONG).show();
                JSON_STRING = result;
                showIzin();
            }


            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequest("https://ohmybags.id/almus/getIzin.php");
                return s;
            }
        }
        getJSON getJSON = new getJSON();
        getJSON.execute();
    }



}


here is the xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"

    >

    <TextView
        android:id="@+id/nomor1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"/>

    <TextView
        android:id="@+id/id_ortu1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />



    <TextView
        android:id="@+id/AlasanIzin1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />

    <TextView
        android:id="@+id/TglIzin1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />


    <TextView
        android:id="@+id/LamaIzin1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />

    <TextView
        android:id="@+id/TglKembali1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />

</LinearLayout>


where did i go wrong?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • And where did you set layout? – Selvin Jul 04 '19 at 13:57
  • i already set the layout on another xml, here is the layout – Alldi Aghlan Jul 04 '19 at 14:04
  • *i already set the layout on another xml,* no, you didn't `protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); listView = findViewById(R.id.ListPermintaan); getJSON(); }` <= point where – Selvin Jul 04 '19 at 14:05
  • i don't understand sir, what do you mean by saying i didn't set it? please advise :) – Alldi Aghlan Jul 04 '19 at 14:08
  • *please advise :)* I advise some basic tutorials – Selvin Jul 04 '19 at 14:09
  • but i've done it here `ListAdapter adapter = new SimpleAdapter(this, list, R.layout.list_izin, new String[]{"nomor", "id_ortu","alasan_perizinan","tglIzin","lamaIzin","tgl_kembali"}, new int[]{R.id.nomor1, R.id.id_ortu1,R.id.AlasanIzin1,R.id.TglIzin1,R.id.LamaIzin1,R.id.TglKembali1}); listView.setAdapter(adapter);` – Alldi Aghlan Jul 04 '19 at 14:10
  • hehe I give up ... you don't understand basics ... your layout file is element of ListView layout **not the layout which contains ListView itself** - which need to be set to activity with right method so findViewById would not return what it returns now – Selvin Jul 04 '19 at 14:12
  • you're really good at motivating sir. it works now. thanks a lot :D – Alldi Aghlan Jul 04 '19 at 14:16

1 Answers1

0

You have to call setContentView(int) in your onCreate() after the super call and provide the layout resource as parameter. Read more in the docs here.

Raimo
  • 1,494
  • 1
  • 10
  • 19