-1

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.

2 Answers2

0

Since I didn't see your error log but still there are few observations which might cause the Null pointer Exception.

You are casting your views each time in your button click method with your button view. like here

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);

and here

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);

Its better to make these views global and remove these lines from your update and reset methods as you are already casting them in you onCreateView method. lemme know if problems sloved or else post your error log as well. :)

here is sample

public class CalculatorFragment extends Fragment implements View.OnClickListener {

    double score = 0;
    double man = 2;
    double manPl = 0;

    CheckBox cbGeslacht;

    TextView punt;
    TextView perc;
    TextView advies;

    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);

        cbGeslacht = (CheckBox)layout.findViewById(R.id.cbGeslacht);
        cbGeslacht.setOnClickListener(this);

        Button reset = (Button) layout.findViewById(R.id.button);
        reset.setOnClickListener(this);

        punt = (TextView) layout.findViewById(R.id.tvPunt);
        perc = (TextView) layout.findViewById(R.id.tvPercentage);
        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) {

        cbGeslacht.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) {

        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) {


        switch (view.getId()) {
            case R.id.cbGeslacht:
                if (cbGeslacht.isChecked()) {
                    manPl = man;
                } else {
                    manPl = 0;
                }
                update(view);
                break;
            case R.id.button:
                reset(view);
                break;
        }

    }

}
Junaid Hafeez
  • 1,618
  • 1
  • 16
  • 25
0

You can use layout instead of getting button views for getting other items.

Try this

public class CalculatorFragment extends Fragment implements View.OnClickListener {

    double score = 0;
    double man = 2;
    double manPl = 0;
    View layout;
    public CalculatorFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

         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) layout.findViewById(R.id.cbGeslacht);

        TextView punt = (TextView) layout.findViewById(R.id.tvPunt);
        TextView perc = (TextView) layout.findViewById(R.id.tvPercentage);
        TextView advies = (TextView) layout.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) layout.findViewById(R.id.tvPunt);
        TextView perc = (TextView) layout.findViewById(R.id.tvPercentage);
        TextView advies = (TextView) layout.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) layout.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;
        }

    }

}
santosh kumar
  • 2,952
  • 1
  • 15
  • 27