i have a android fragment with a checkbox and a reset button. inside the fragment there is an update and a reset methode. Whenever i call these methodes i get a NullPointerException. i can't figure out why (which makes me a novice)
package info.doktershuis.android.jichtcalculator;
public class CalculatorFragment extends Fragment implements View.OnClickListener {
double score = 0;
double man = 2;
double manPl = 0;
public CalculatorFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_calculator, container, false);
CheckBox cbGeslacht = (CheckBox)layout.findViewById(R.id.cbGeslacht);
cbGeslacht.setOnClickListener(this);
Button reset = (Button) layout.findViewById(R.id.button);
reset.setOnClickListener(this);
TextView punt = (TextView) layout.findViewById(R.id.tvPunt);
TextView perc = (TextView) layout.findViewById(R.id.tvPercentage);
TextView advies = (TextView) layout.findViewById(R.id.tvToelicht);
punt.setText(Double.toString(score));
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
return layout;
}
public void reset(View view) {
CheckBox geslacht = (CheckBox) view.findViewById(R.id.cbGeslacht);
TextView punt = (TextView) view.findViewById(R.id.tvPunt);
TextView perc = (TextView) view.findViewById(R.id.tvPercentage);
TextView advies = (TextView) view.findViewById(R.id.tvToelicht);
geslacht.setChecked(false);
double score = 0;
manPl = 0;
punt.setText(Double.toString(score));
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
}
public void update(View view) {
TextView punt = (TextView) view.findViewById(R.id.tvPunt);
TextView perc = (TextView) view.findViewById(R.id.tvPercentage);
TextView advies = (TextView) view.findViewById(R.id.tvToelicht);
score = manPl;
punt.setText(Double.toString(score));
if (score <= 4) {
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
} else if (score < 8) {
perc.setText(R.string.pt48);
perc.setBackgroundColor(getResources().getColor(holo_orange_light));
advies.setText(R.string.pt48a);
} else {
perc.setText(R.string.pt8);
perc.setBackgroundColor(getResources().getColor(holo_red_light));
advies.setText(R.string.pt8a);
}
}
@Override
public void onClick(View view) {
CheckBox geslacht = (CheckBox) view.findViewById(R.id.cbGeslacht);
switch (view.getId()) {
case R.id.cbGeslacht:
if (geslacht.isChecked()) {
manPl = man;
} else {
manPl = 0;
}
update(view);
break;
case R.id.button:
reset(view);
break;
}
}
}
The Exception is thrown when i use geslacht.setChecked(false);
or punt.setText(Double.toString(score));
can anybody help me with this one, many thanks in advance
logcat:
01-21 08:43:25.233 23876-23876/info.doktershuis.android.jichtcalculator E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at info.doktershuis.android.jichtcalculator.CalculatorFragment.reset(CalculatorFragment.java:111)
at info.doktershuis.android.jichtcalculator.CalculatorFragment.onClick(CalculatorFragment.java:278)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Hi K Neeraj Lal (SO-not-ready to help), i have read all the items about NullPointerException. Can not find my answer. Please point me out where to find it. Remember i'm a novice. I might not understand it all.