-2

While populating ListView app crashes.

Error: null point Exception

public class Recycler extends AppCompatActivity
{
    private ListView mListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);

        mListView = (ListView) findViewById(android.R.id.list);

        DatabaseReference ref = FirebaseDatabase.getInstance().getReferenceFromUrl("https://afirebaseproject-eab4d.firebaseio.com/data/a");

        FirebaseListAdapter<String> adapter = new FirebaseListAdapter<String>(
                this,
                String.class,
                android.R.layout.simple_list_item_1,
                ref
        )
        {
            @Override
            protected void populateView(View v, String model, int position) {

               TextView text = (TextView) findViewById(android.R.id.list);

                text.setText(model);
            }
        };
        mListView.setAdapter(adapter);
    }
}

XML ListView id- list

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    ....
    tools:context="shah.parth.temp2.Recycler">

    <ListView

        android:id="@+id/list"
        style="@android:style/Widget.ListView" />
</android.support.constraint.ConstraintLayout>

This is the error which I am getting

FATAL EXCEPTION: main
    Process: shah.parth.temp2, PID: 2845
    java.lang.RuntimeException: Unable to start activity ComponentInfo{shah.parth.temp2/shah.parth.temp2.Recycler}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
        at android.app.ActivityThread.access$900(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5441)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at shah.parth.temp2.Recycler.onCreate(Recycler.java:41)
        at android.app.Activity.performCreate(Activity.java:6303)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) 
        at android.app.ActivityThread.access$900(ActivityThread.java:153) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5441) 
        at java.lang.reflect.Method.invoke(Native Method) 
Pang
  • 9,564
  • 146
  • 81
  • 122

2 Answers2

0

For your FirebaseListAdapter, you're using android.R.layout.simple_list_item_1 as your layout for each ListItem.

In the populateView method, you are looking for the view android.R.id.list. The populateView method is the method where you assign the values in the views of your layout and here are the views in the layout, android.R.layout.simple_list_item_1: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/simple_list_item_1.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@android:id/text1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:textAppearance="?android:attr/textAppearanceListItemSmall"
   android:gravity="center_vertical"
   android:paddingStart="?android:attr/listPreferredItemPaddingStart"
   android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
   android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

If you look in the code, there is no view with an id of list but rather a view with an id of text1. With that, when the findViewById method will start to find the view with an ID of list, it would get nothing and when you setText to nothing, it would invoke a NullPointerException

To fix this, just change the id in your populateView method to text1 so that it would get the appropriate view.

You should also change your code on line 12:

mListView = (ListView) findViewById(android.R.id.list);

to:

mListView = (ListView) findViewById(R.id.list);

Using android.R would refer to resources that are built-in with the Android SDK and R would refer to resources your provide or create in your application.

See this post for more info: Difference between R.layout and android.R.layout

Community
  • 1
  • 1
Kurt Acosta
  • 2,407
  • 2
  • 14
  • 29
  • id for ListView is list only – parth. shah Mar 27 '17 at 08:05
  • protected void populateView(View v, String model, int position) { ListView text = (ListView) findViewById(R.id.list); text.text(model); } – parth. shah Mar 27 '17 at 08:06
  • For your List Adapter, you are using android.R.layout.simple_list_item_1 as your layout for the List Item and android.R.layout.simple_list_item_1 has android.R.id.text1 as the container for the value. Your R.id.list would refer to your whole list, not to the container for the values which would be placed in the list – Kurt Acosta Mar 27 '17 at 08:57
0

change your code

           TextView text = (TextView) findViewById(android.R.id.list);

to

         TextView textView = (TextView) v.findViewById(android.R.id.text1);