0

I am trying to populate a simple list view with the values in a string array using the following code ,it throws a null pointer exception.The list is fetched from the previous ctivity that stats this and the list is valid i have checked it containns string values

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras(); 
        final ListView listview = (ListView) findViewById(R.id.listview);
        List<Map<String, String>> deptList = new ArrayList<Map<String,String>>();

        if (extras != null) {

            deplist = extras.getStringArray("deparments");
            Log.w("Got","List");

            }
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(Home.this,
                  android.R.layout.simple_list_item_1, android.R.id.text1, deplist);
         listview.setAdapter(adapter); 


        setContentView(R.layout.home);

here is the stack trace

10-06 02:39:33.002: D/dalvikvm(1409): GC_FOR_ALLOC freed 59K, 7% free 2541K/2720K, paused 139ms, total 141ms
10-06 02:39:33.102: I/dalvikvm-heap(1409): Grow heap (frag case) to 4.058MB for 1536016-byte allocation
10-06 02:39:33.152: D/dalvikvm(1409): GC_FOR_ALLOC freed <1K, 5% free 4041K/4224K, paused 48ms, total 48ms
10-06 02:39:33.642: D/gralloc_goldfish(1409): Emulator without GPU emulation detected.
10-06 02:39:35.952: W/Got(1409): List
10-06 02:39:36.092: D/AndroidRuntime(1409): Shutting down VM
10-06 02:39:36.092: W/dalvikvm(1409): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-06 02:39:36.372: E/AndroidRuntime(1409): FATAL EXCEPTION: main
10-06 02:39:36.372: E/AndroidRuntime(1409): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usa/com.example.usa.Home}: java.lang.NullPointerException
10-06 02:39:36.372: E/AndroidRuntime(1409):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at android.os.Looper.loop(Looper.java:137)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at java.lang.reflect.Method.invokeNative(Native Method)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at java.lang.reflect.Method.invoke(Method.java:525)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at dalvik.system.NativeStart.main(Native Method)
10-06 02:39:36.372: E/AndroidRuntime(1409): Caused by: java.lang.NullPointerException
10-06 02:39:36.372: E/AndroidRuntime(1409):     at com.example.usa.Home.onCreate(Home.java:51)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at android.app.Activity.performCreate(Activity.java:5133)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-06 02:39:36.372: E/AndroidRuntime(1409):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-06 02:39:36.372: E/AndroidRuntime(1409):     ... 11 more
0

1 Answer 1

2

You are calling

final ListView listview = (ListView) findViewById(R.id.listview);

before

setContentView(R.layout.home);

Which doesn't make sense. findViewById() finds the view in the currently inflated view. But you haven't inflated a view until 10 lines later with setContentView(). Put setContentView() on the beginning of onCreate().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.