0
votes
 EditText e = (EditText)findViewById(Edit_current);
    Spinner s = (Spinner)findViewById(R.id.edit_destination);
    navigatebut = (Button) findViewById(R.id.navigate);
    final String current = e.getText().toString();
    final String dest = s.getSelectedItem().toString();
    navigatebut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //to do your drawable
            if (current.equals("Entrance") && dest.equals("Vegetables")) {
                layout.setImageResource(R.drawable.entrance_vege);;
                //do something
            }

        }
    });
}}

How it looks like


When i run this, it has an error java.lang.nullpointerexception. May I know where its coming from?


E/AndroidRuntime: FATAL EXCEPTION: main Process: com.sp.ez_mart, PID: 17820 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sp.ez_mart/com.sp.ez_mart.NavigateImage}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 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:5017) 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:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.sp.ez_mart.NavigateImage.onCreate(NavigateImage.java:31) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)  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:5017)  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:779)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  at dalvik.system.NativeStart.main(Native Method) 

1
com.sp.ez_mart.NavigateImage.onCreate(NavigateImage.java:31) what is at line # 31?Hossain Khan

1 Answers

0
votes

The stack trace clearly tells you where the error is @ NavigateImage.java Line#31

Looking at code, it seems like you are passing wrong resource id in following line. May be you meant to use R.id.edit_destination

EditText e = (EditText)findViewById(Edit_current);

So, I suspect, it is crashing from

final String current = e.getText().toString();

Because it can't find the EditText e.

NOTE: In future as best practice, please use descriptive variable names.