5

When compiling my code i get this error ("java.lang.NullPointerException: Attempt to invoke interface method") and I still can't figure why.

Here is my code:

    public class FragmentDetails extends Fragment {

        private TextView text1, text2, text3, text4, text5 = null;
        private Button button1 = null;


        OnDetailsDefinedListener mCallback;

        public interface OnDetailsDefinedListener {
            void executeOrder(String a, String b, String c, String d, String e);
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            rootView = inflater.inflate(R.layout.fragment_details, container, false);

            button1 = (Button) rootView.findViewById(...);

            button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                text1 = getActivity().findViewById(...);
                text2 = getActivity().findViewById(...);
                text3 = getActivity().findViewById(...);
                text4 = getActivity().findViewById(...);
                text5 = getActivity().findViewById(...);

                String a = text1.getText().toString();
                String b = text2.getText().toString();
                String c= text3.getText().toString();
                String d= text4.getText().toString();
                String e= text5.getText().toString();

                //this is where my error appears
                mCallback.executeOrder(a, b, c, d, e);
            }
            return rootView;
        }
    }

(here is my error message)

    05-26 09:39:20.868    2916-2916/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.blueHatCoder.myapplication, PID: 2916
        java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.blueHatCoder.myapplication.FragmentDetails$OnDetailsDefinedListener.executeOrder(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
        at com.example.blueHatCoder.myapplication.FragmentDetails$6.onClick(FragmentDetails.java:131)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Thank you in advance.

1
  • 1
    mCallback is never initiated... Commented May 26, 2015 at 10:23

4 Answers 4

5

mCallback is never initiated. you have to do something like that:

mCallback = new OnDetailsDefinedListenerImpl(); 
mCallback.executeOrder(a, b, c, d, e);

or

mCallback = new OnDetailsDefinedListener(){
   @Override
   void executeOrder(String a, String b, String c, String d, String e){
     //doSomething
   };
}
Sign up to request clarification or add additional context in comments.

5 Comments

the thing is...since i'm using an interface, the purpose is for the activity who implements it to have the code that "do something". Another thing is that i did exactly the same on another fragment (didn't initialized the mCallback) and it worked...
no. if something is never initiated you will get a NPE. Always!
you're right...my mistake and i just found what i was missing. Thank you for the help :)
@BlueHatCoder What were you missing? I'm having the same problem and cannot see why mCallback needs to be initialized.
@StefanBeike I cannot initialize mCallback = new OnDetailsDefinedListenerImpl(). I get the issue: OnDetailsDefindedListenerImpl is abstract(); cannot be instantiated. What could I have done wrong?
2

Your variable mCallback is defined but not initialized. You have to initialize it before you can use it.

1 Comment

how interface initialize even i want to add value in it .
1

You should add a public method to the FragmentDetails class.

public setOnDetailsDefinedListener(OnDetailsDefinedListener listener) {
    mCallback = listener;
}

And just implement it in the caller class. just like how you do when add button listener.

mFragmentDetails.setOnDetailsDefinedListener(new OnDetailsDefinedListener() {
// You code
})

Comments

0

Maybe you need to use onAttach(Activity activity) fragments method

public void onAttach(Activity activity){
    super.onAttach(activity);
    if(activity instanceof onSomeListener){
        mListener=(onSomeListener) activity;
    }
}

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.