U
U
ukoHka2015-10-28 09:39:08
Android
ukoHka, 2015-10-28 09:39:08

What can cause NullPointerException when settingAdapter in different versions of Android?

The app crashes on some recent versions of Android, displaying

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
            at android.widget.AbsListView.obtainView(AbsListView.java:2347)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
            at android.widget.ListView.onMeasure(ListView.java:1182)
            at android.view.View.measure(View.java:17547)
            at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at 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:698)

The function that throws the exception:
private void loadEvents(){
        ArrayAdapter<String> adap = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, enames);
        ListView event_list = (ListView) findViewById(R.id.eventslist);
        event_list.setAdapter(adap);
        event_list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                event = eids[position];
                eType = etypes[position];
                group();
            }
        });
    }

If you comment out the line "event_list.setAdapter(adap);" the application does not crash, and if left, it crashes at the very end of the loadEvents() function; The data in adap is correct, and the ListView is present in the current ContentView.
In versions from 4.0 to 4.4.1 the application works fine.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LeEnot, 2015-10-28
@ukoHka

Check names. What is this variable?
UPD: I explain for the stubborn and secretive:
What is going on with you?
In the ArrayAdapter.createViewFromResource() method, the toString() method is called on a null object.
Specifically at this location:

List<T> mObjects;
...
T item = getItem(position);
if (item instanceof CharSequence) {
    text.setText((CharSequence)item);
} else {
    text.setText(item.toString());
}

Where T is String.
Accordingly, if item == null, then (null instanceof CharSequence) gives false and you have a NullPointer.
Is that clearer? Watch what you are passing to the ArrayAdapter constructor.

T
Tiberal, 2015-10-28
@Tiberal

show the adapter's getView

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question