user3399567
user3399567

Reputation: 67

Android. NullPointerException when I add adapter to ListView?

I can't find the problem. I checked a lot of question in this case, but I can't solve the problem. I pasted the code from another source code and there works fine. Here is the code and the XML file, maybe somebody can tell what is the problem. Any suggestion?

public class MainActivity extends Activity {
MySQLiteHelper db = new MySQLiteHelper(this);
public String address = new String();

private com.example.fdfd.DiscussArrayAdapter adapter;
public List firstMessages;




public String messageBody = new String();
public String messageDate = new String();
public String messageBoolean = new String();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_discuss);

    final ListView listview = (ListView) findViewById(R.id.listView);
    listview.setClickable(true);

    final ArrayAdapter newAdapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, firstMessages);

    listview.setAdapter(newAdapter);
.
.
.

My XML:

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

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</RelativeLayout>

My LogCat:

03-25 16:01:03.890: E/Trace(7819): error opening trace file: No such file or directory (2)
03-25 16:01:04.480: W/dalvikvm(7819): threadid=1: thread exiting with uncaught exception (group=0x413f5450)
03-25 16:01:04.490: E/AndroidRuntime(7819): FATAL EXCEPTION: main
03-25 16:01:04.490: E/AndroidRuntime(7819): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fdfd/com.example.fdfd.MainActivity}: java.lang.NullPointerException
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2090)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.app.ActivityThread.access$600(ActivityThread.java:136)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.os.Looper.loop(Looper.java:137)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.app.ActivityThread.main(ActivityThread.java:4802)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at java.lang.reflect.Method.invoke(Method.java:511)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at dalvik.system.NativeStart.main(Native Method)
03-25 16:01:04.490: E/AndroidRuntime(7819): Caused by: java.lang.NullPointerException
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.widget.ListView.setAdapter(ListView.java:460)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at com.example.fdfd.MainActivity.onCreate(MainActivity.java:64)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.app.Activity.performCreate(Activity.java:5013)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-25 16:01:04.490: E/AndroidRuntime(7819):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
03-25 16:01:04.490: E/AndroidRuntime(7819):     ... 11 more

Upvotes: 0

Views: 273

Answers (3)

Garytech
Garytech

Reputation: 338

The role of an ArrayAdapter is to associate each item of a ListView with a layout and data.

There is something wrong with the data of your ArrayAdapter, they have never been initialized. Then, you can't associate the items of your listview with data or display the items on a layout .

Upvotes: 0

nfusion
nfusion

Reputation: 599

You're never initializing firstMessages, therefore it is null. Try changing the following part

public List firstMessages;

to

public List firstMessages = new ArrayList();

Upvotes: 1

Robby Pond
Robby Pond

Reputation: 73494

Your List, firstMessages, is null and that is generating the NullPointerException. When attached to a ListView, getCount() is called which in turn calls size() on the Collection.

Upvotes: 0

Related Questions