Steakschen
Steakschen

Reputation: 163

Android NumberPicker NullPointer why?

i am try to implement a numberPicker to select minute values.

But i am getting a NullPointer Exception at this line:

minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);

Following Code:

public class MainActivity extends Activity {


NumberPicker minutePicker;

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

    // Auswahl Minuten zum starten / Stoppen aller
    minutePicker = new NumberPicker(MainActivity.this);
    minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
    minutePicker.setMaxValue(30);


    minutePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            abschaltzeit = minutePicker.getValue();
        }
    });

    minutePicker.setValue(0);
    minutePicker.setWrapSelectorWheel(false);

}
}

XML:

<NumberPicker
    android:id="@+id/minuten_picker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_row="6"
    android:layout_column="0"
    android:paddingLeft="20dp" />

Log:

09-25 11:00:09.749  10687-10687/de.carsten.awesome.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: de.carsten.awesome.app, PID: 10687
    java.lang.RuntimeException: Unable to start activity ComponentInfo{de.carsten.awesome.app/de.carsten.awesome.app.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at de.carsten.awesome.app.MainActivity.onCreate(MainActivity.java:83)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Upvotes: 3

Views: 1092

Answers (2)

Steakschen
Steakschen

Reputation: 163

I apologize.

I moved the Code from the MainActivity in the creating Fragment and it works with rootView.findView.

Sorry but i am new with the Fragement Konzept.

Thanks a lot for your help !

Upvotes: 0

laalto
laalto

Reputation: 152797

minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);

You're creating a NumberPicker programmatically and then overwriting the reference with whatever findViewById() returns. It returns null if your activity_main layout does not contain a minuten_picker.

  • Choose only the other: either create it prorgrammatically or find it from a view hierarchy you inflated.

  • If you choose the programmatic way new NumberPicker(), remember to add it to some layout in your activity view hierarchy, e.g. with setContentView()

  • If you choose the inflation way, make sure you have the view in your XML layout file.

I'm guessing the NPE you're seeing is actually on the following line where you're trying to invoke a method on the minutePicker and it's null.

Upvotes: 3

Related Questions