0

I was recently working on a todo list app.

The code for the app's OnCreate method is shown below:

public class MainActivity extends AppCompatActivity {

    ArrayList<String> items;
    ArrayAdapter<String> itemsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        ListView todo = (ListView) findViewById(R.id.list);
        items = new ArrayList<>();
        itemsAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, items);
        todo.setAdapter(itemsAdapter);

        ActionBar ab = getSupportActionBar();
        assert ab != null;
        ab.hide();



    }

But when I run the app, it keeps crashing.

I checked the logcat to see what the error was and I got the following piece.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dobleu.hotlists/com.dobleu.hotlists.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

I have no idea why I'm getting a NPE.

I am using fragments and I am utilizing a BottomNavigationView to transition between them.

Here is the xml for the fragment with the listview:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="232dp"
        android:background="@drawable/header1"
        android:scaleType="fitStart"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/imageView"
        android:orientation="vertical"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="50dp"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:background="#e95555"
        android:id="@+id/linearLayout"
        android:orientation="vertical">

    </LinearLayout>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        tools:layout_editor_absoluteX="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="13dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:background="#e95555"
        app:fabSize="normal"
        android:onClick="AddItem"/>

</android.support.constraint.ConstraintLayout>

And here's the xml for the main activity:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dobleu.hotlists.MainActivity">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"
        android:animateLayoutChanges="true">


    </FrameLayout>


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#2b2b2b"
        app:itemIconTint="#fff"
        app:itemTextColor="#fff"
        app:menu="@menu/menu"/>
</RelativeLayout>

I have tried a lot to get it working but I have not been able to find the solution.

I've read that post on what a NPE is and why, but I still don't understand.

And now I'm sick and tired that people keep marking my questions as duplicates when I am very well aware that there is an answer that can apply to this question but I have no idea how to apply it.

Please help me.

  • Your `ListView` which is denoted here by `todo` is null, hence throwing exception at `todo.setAdapter(itemsAdapter);`. I think this is because there is no view with id list. – Neeraj Jain Nov 21 '17 at 20:29
  • Do you have any idea why it is null, because I've given the XML value an id. –  Nov 21 '17 at 20:34
  • Well, where is your xml then? You don't have proper [mcve] – Dalija Prasnikar Nov 21 '17 at 20:42
  • I'll post it in now –  Nov 21 '17 at 20:44
  • Also there is difference between asking "Why am I getting NPE" and "Why is my ListView null". Answer to the first question is because your ListView is null and that question is indeed duplicate. Answer to the second question is most likely also a duplicate but probably more helpful one. – Dalija Prasnikar Nov 21 '17 at 20:49
  • I know it has some element of duplication but, I have no idea how to apply the answer from the "original" question to my own question. –  Nov 21 '17 at 20:51
  • I can not see the listview layout. Have you posted this? – Samuel Agbede Nov 21 '17 at 20:55
  • Check the code again, it should be there –  Nov 21 '17 at 21:00
  • could it be that your listview isn't in your activity_main.xml? – Peppermint Paddy Nov 21 '17 at 21:38
  • If the `ListView` is in a `Fragment`, you should be finding and initializing it in the `Fragment`, not in the `Activity`. – Mike M. Nov 21 '17 at 21:58

0 Answers0