Shruti
Shruti

Reputation: 5591

Nullpointer exception in listview adapter

I am using arrayadapter to show data on listview.What i have coded is shown below bt i am getting null pointer exception .Please tell me what i have missed ?

ArrayAdapter<String> adapter;
setContentView(R.layout.menu_home_activity);
        OptionsList = (ListView) findViewById(R.id.OptionsLists);
String[] array = getResources().getStringArray(R.array.personal_settings);
            adapter = new ArrayAdapter<String>(MenuHomeActivity.this,
                    R.layout.menu_home_activity, R.id.text, array);
            OptionsList.setAdapter(adapter);

menu_home_activity.xml

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

    <ListView
        android:name="@+id/OptionsLists"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Logcat says :

03-14 10:54:59.242: E/AndroidRuntime(267): Uncaught handler: thread main exiting due to uncaught exception
03-14 10:54:59.252: E/AndroidRuntime(267): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.biz.elife/com.biz.elife.MenuHomeActivity}: java.lang.NullPointerException
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.os.Looper.loop(Looper.java:123)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread.main(ActivityThread.java:4363)
03-14 10:54:59.252: E/AndroidRuntime(267):  at java.lang.reflect.Method.invokeNative(Native Method)
03-14 10:54:59.252: E/AndroidRuntime(267):  at java.lang.reflect.Method.invoke(Method.java:521)
03-14 10:54:59.252: E/AndroidRuntime(267):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-14 10:54:59.252: E/AndroidRuntime(267):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-14 10:54:59.252: E/AndroidRuntime(267):  at dalvik.system.NativeStart.main(Native Method)
03-14 10:54:59.252: E/AndroidRuntime(267): Caused by: java.lang.NullPointerException
03-14 10:54:59.252: E/AndroidRuntime(267):  at com.biz.elife.MenuHomeActivity.onCreate(MenuHomeActivity.java:33)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-14 10:54:59.252: E/AndroidRuntime(267):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-14 10:54:59.252: E/AndroidRuntime(267):  ... 11 more

Edit :

33rd line is

OptionsList.setAdapter(adapter);

when i try setting adapter to list it gives error if i remove this line no error is displayed and also no components on screen are displayed only a black screen appears

Thanks everybody for precious time and replys my mistake was in the menu_home_activity i added android:name="@+id/OptionsList" that i changed to

android:id="@+id/OptionsList"

Upvotes: 0

Views: 904

Answers (5)

Shubhayu
Shubhayu

Reputation: 13552

your optionsList is not getting created properly, which is probably because of some problem with ure xml file. The ListView is not getting inflated properly;

try replacing match_parent with fill_parent in android:layout_width="match_parent" and android:layout_height="match_parent"

Hope this helps.

Upvotes: 0

Khan
Khan

Reputation: 7605

Just replace this

      adapter = new ArrayAdapter<String>(MenuHomeActivity.this,
                R.layout.menu_home_activity, R.id.text, array);

in your code by

   adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array);

Upvotes: 1

Anuj Balan
Anuj Balan

Reputation: 7729

Try to put the code in try-catch block I think

OptionsList = (ListView) findViewById(R.id.OptionsLists);

is causing a null pointer. Better handle the code for possible exceptions(null or others).

Upvotes: 1

Prasad
Prasad

Reputation: 1375

I think the below code will help you to solve your problem:

public class ArrayAdapterDemo extends ListActivity {
   TextView selection;
   String[] items = { "this", "is", "a", "really", 
   "silly", "list" };
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(
      this,
      android.R.layout.simple_expandable_list_item_1,
      items));
selection=(TextView)findViewById(R.id.selection); 
   }

Upvotes: 1

Relsell
Relsell

Reputation: 771

Problem is in 33rd Line of your java file for the concerned Activity. I guess your list view is not getting initialized properly .

Upvotes: 0

Related Questions