Why can one line of code work, but then the same line throws a null pointer exception later?

182 Views Asked by At

I am creating an android app that involves creating a to-do list, when the onstart method is called I can call a context.findviewbyid() line succesfully but later that does not work, this only happens when the app has been rotated or reset in some way

onStart Method

@Override
public void onStart(){
super.onStart();
this.context = getActivity();
ViewGroup vg = (ViewGroup) context.findViewById(R.id.MainLayout);
databaseHandler = new DatabaseHandler(context, (ViewGroup)
context.findViewById(R.id.MainLayout));
vg.removeAllViews();
databaseHandler.reinstantiateAllWishes();
WishlistCounter = databaseHandler.getWishCount();
databaseHandler.close();
Log.v("onStart WishlistCounter: ", String.valueOf(WishlistCounter));
}

createNew Method

public void createNew(){
    Log.v("Creating new:","confirmed");
    //Get the message from the Edit Text
    EditText editText = (EditText) context.findViewById(R.id.WishListAddText);
    String message= editText.getText().toString();
    //If it is acceptable, continue
    if(!message.equals("") && WishlistCounter < SLOTS_LIMIT && !message.equals(null)){
        //Create new Wish
        Wish c = new Wish((ViewGroup) context.findViewById(R.id.MainLayout), getActivity(), this.databaseHandler);
        c.newBuilder(message);
        WishlistCounter++;
    //Explain unacceptability
    } else if(WishlistCounter >= SLOTS_LIMIT){
        Toast.makeText(context, "You cannot have over" +  String.valueOf(SLOTS_LIMIT) + "entries" ,Toast.LENGTH_SHORT).show();
    }
}

Error Log

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3039)
at android.view.View.performCLick(View.java:3480)
at android.view.View$PerformClick.run(View,java:13983)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.osLooper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.intenal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(NativeMethod)
Caused by: java.lang.InvocationTargetException
at java.lang.reflect.Method.invokeNative(NativeMethod)
at java.lang.reflect.Method.invoke(Method.java:3034)
... 11 more
Caused by: java.lang.NullPointerException
at com.chesterbane.inspiriappaplpha.Frgmant4.createNew(Fragment4.java:61)
at com.chesterbane.inspiriappalpha.MainActivity.wishlistCreateNew(MainActivity.java:315)
... 14 more

It is the line EditText editText = (EditText) context.findViewById(R.id.WishListAddText); that is throwing the exception and I think that it is the context that is null. I dont understand what could be causing it to be null as it works fine in the onStart method. Any help would be greatly appreciated

1

There are 1 best solutions below

0
On

On the third line of your onStart method you are setting the context to the activity. Try setting it to the context instead.

this.context = getActivity().getApplicationContext();