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.
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
};
}
Your variable mCallback
is defined but not initialized. You have to initialize it before you can use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With