0
 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? Commented Jan 15, 2017 at 3:21

1 Answer 1

0

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.

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.