spurra
spurra

Reputation: 1025

NullPointer exception when using ListView

I've got following code, which gets launched as a second activity:

public class SensorActivity extends Activity implements SensorEventListener{

List<Sensor> sensors;
Sensor selectedSens;
SensorManager SensMng;
ArrayAdapter<String> adapter;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sensor);

    final ListView listSensors = (ListView) findViewById(R.id.listvalues);


    SensMng = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensors = SensMng.getSensorList(Sensor.TYPE_ALL);
    ArrayAdapter<Sensor> adapter = new ArrayAdapter<Sensor>(this, android.R.layout.simple_list_item_1, sensors);
    listSensors.setAdapter(adapter);



}

Here's my XML file:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listvalues"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

I keep on getting a nullpointer exception:

10-03 21:46:57.475: E/AndroidRuntime(9893): FATAL EXCEPTION: main
10-03 21:46:57.475: E/AndroidRuntime(9893): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ch.ethz.inf.vs.android.spurra.sensors/ch.ethz.inf.vs.android.spurra.sensors.SensorActivity}: java.lang.NullPointerException
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.os.Looper.loop(Looper.java:137)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread.main(ActivityThread.java:4898)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at java.lang.reflect.Method.invokeNative(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at java.lang.reflect.Method.invoke(Method.java:511)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at dalvik.system.NativeStart.main(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893): **Caused by**: java.lang.NullPointerException
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.Activity.findViewById(Activity.java:1882)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at ch.ethz.inf.vs.android.spurra.sensors.SensorActivity.<init>(SensorActivity.java:24)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at java.lang.Class.newInstanceImpl(Native Method)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at java.lang.Class.newInstance(Class.java:1319)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
10-03 21:46:57.475: E/AndroidRuntime(9893):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
10-03 21:46:57.475: E/AndroidRuntime(9893):     ... 11 more

All I did was really just copy past from my main activity, plus adjusting the values. The main activity works without any problems, this one however doesn't. I've tried cleaning, rebuilding the project, to no avail.

Can anyone please tell me whats wrong?

Thanks, Regards

Upvotes: 0

Views: 93

Answers (2)

codeMagic
codeMagic

Reputation: 44571

I've declared " final ListView listVals = (ListView) findViewById(R.id.listvalues);" as a field, so before running setContentView(). Once I've moved it into the actual function, it worked. So I assume that was causing it to return null?

Yes. If you call findViewById(R.id.someId); before calling setContentView(R.layout.someLayout); it will return null because you haven't yet inflated your layout which contains the View id you are trying to find.

Basically, your Views exist inside of your layout so if you try to use findViwById() before you inflate your layout (usually with setContentView()) then there is nothing to find. So you will get NPE when you try to use like when you call a function on it.

Upvotes: 1

muratgurbuzdal
muratgurbuzdal

Reputation: 106

If your project has external jar files, go to Project->properties->order and export and select all external jar files. That should do the work.

Upvotes: 0

Related Questions