Edit: I have already gone through that question about null pointer exception. This question is completely different. Here I don't know why slot3 was termed null pointer, even though I declared it in oncrrateview. Another point to note, is that when I try to access the variables from the oncrrateview itself, there is no error. The error is coming only when I do fragment communication. That question did not have any answers to my question. So please remove the duplicate Mark.
This is my code. This is my first question in stackoverflow. Please bear my way of asking.
public class HeaderScrollFragment extends Fragmen {
OnModeSelectedListener modeCallBack;
public View myFragmentView;
Button slot1,slot2,slot3,slot4,slot5;
int selectedMode;
public interface OnModeSelectedListener{
public void OnModeChangedCallBack(int x);
public void inputDataCallback(int y);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
modeCallBack = (HeaderScrollFragment.OnModeSelectedListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.activity_header_scroll,container,false);
manyScreens();
slot1 = (Button) myFragmentView.findViewById(R.id.header_slot1);
slot2 = (Button) myFragmentView.findViewById(R.id.header_slot2);
slot3 = (Button) myFragmentView.findViewById(R.id.header_slot3);
slot4 = (Button) myFragmentView.findViewById(R.id.header_slot4);
slot5 = (Button) myFragmentView.findViewById(R.id.header_slot5);
slot1.setOnClickListener(this);
slot2.setOnClickListener(this);
slot3.setOnClickListener(this);
slot4.setOnClickListener(this);
slot5.setOnClickListener(this);
selectedMode = 3;
return myFragmentView;
}
private void manyScreens() {
}
public void onTopicChanged(int x){
switch (x){
case 1:
slot3.setText(R.string.next_m);
slot5.setText(R.string.mg);
break;
case 2:
slot3.setText(R.string.next_e);
slot5.setText(R.string.tg);
break;
case 3:
slot3.setText(R.string.next_v);
slot5.setText(R.string.y_g);
break;
}
onModeChanged(3);
}
public void onModeChanged(int x){
slot1.setSelected(false);
slot2.setSelected(false);
slot3.setSelected(false);
slot4.setSelected(false);
slot5.setSelected(false);
switch (x){
case 1:
slot1.setSelected(true);
break;
case 2:
slot2.setSelected(true);
break;
case 3:
slot3.setSelected(true);
break;
case 4:
slot4.setSelected(true);
break;
case 5:
slot5.setSelected(true);
break;
}
modeCallBack.OnModeChangedCallBack(x);
}
}
There is another fragment in my code which is BottomMenuFragment. When I try to to communicate with this fragment from Bottom menu fragment, it is showing null pointer exception. Don't look at the line numbers in the log. Basically it is showing the line slot3.setText(R.string.next_episode); with a null pointer exception
FATAL EXCEPTION: main
Process: com.batmansves.www.nextepisode, PID: 18087
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.batmansves.www.nextepisode/com.batmansves.www.nextepisode.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference
at com.batmansves.www.nextepisode.HeaderScrollFragment.onTopicChanged(HeaderScrollFragment.java:138)
at com.batmansves.www.nextepisode.MainActivity.TopicChangeCallback(MainActivity.java:69)
at com.batmansves.www.nextepisode.BottomMenuFragment.onTopicChanged(BottomMenuFragment.java:99)
at com.batmansves.www.nextepisode.BottomMenuFragment.onCreateView(BottomMenuFragment.java:74)
at android.app.Fragment.performCreateView(Fragment.java:2220)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1537)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6275)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
What I think is happening is that it is taking slot3 as a null pointer. But I have already defined slot3 in onCreateView(). So I don't actually understandwhere the problem is?