Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need that the each checkbox have own value like 1, 2 or 3, and u can check only 1 of first 3 checkboxes(cb1, cb2, cb3), and only 1 of second 3 checkboxes(cb4, cb5, cb6). But most imortant is to get summary of which checkboxes is cheked. App is crashing, what am I doing wrong? p.s. please excuse my bad English Here is the full source code:

public class MainActivity extends Activity {
    LinearLayout layout;
    Button bt;
    CheckBox cb1, cb2, cb3, cb4, cb5, cb6;
    TextView tv;
    String a, b, c;
    int d, e, f, g, h, i;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        cb1 = new CheckBox(this);
        cb2 = new CheckBox(this);
        cb3 = new CheckBox(this);
        cb4 = new CheckBox(this);
        cb5 = new CheckBox(this);
        cb6 = new CheckBox(this);
        bt = new Button(this);
        tv = new TextView(this);
        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                a = "";
                b = "";
                c = "";
                d = 1;
                e = 2;
                f = 3;
                if (cb1.isChecked()){
                    a = Integer.toString(d);
                }else if (cb2.isChecked()){
                    a = Integer.toString(e);
                }else if (cb3.isChecked()){
                    a = Integer.toString(f);
                }else if (cb4.isChecked()){
                    b = Integer.toString(d);
                }else if (cb5.isChecked()){
                    b = Integer.toString(e);
                }else if (cb6.isChecked()){
                    b = Integer.toString(f);
                }
                g = Integer.valueOf(a);
                h = Integer.valueOf(b);
                i = g+h;
                c = Integer.toString(i);
                tv.setText(c);
            }
        });
        layout.addView(cb1);
        layout.addView(cb2);
        layout.addView(cb3);
        layout.addView(cb4);
        layout.addView(cb5);
        layout.addView(cb6);
        layout.addView(bt);
        layout.addView(tv);
        setContentView(layout);

    }
}

10-20 00:13:15.580: W/dalvikvm(2826): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-20 00:13:15.700: E/AndroidRuntime(2826): FATAL EXCEPTION: main
10-20 00:13:15.700: E/AndroidRuntime(2826): java.lang.NullPointerException
10-20 00:13:15.700: E/AndroidRuntime(2826):     at com.example.test.MainActivity$1.onClick(MainActivity.java:58)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.view.View.performClick(View.java:4204)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.view.View$PerformClick.run(View.java:17355)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.os.Handler.handleCallback(Handler.java:725)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.os.Looper.loop(Looper.java:137)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at java.lang.reflect.Method.invoke(Method.java:511)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-20 00:13:15.700: E/AndroidRuntime(2826):     at dalvik.system.NativeStart.main(Native Method)
share|improve this question
2  
What's the error you're getting? –  H P Oct 19 '13 at 23:52
    
java.lang.NumberFormatException: Invalid int: "" –  LXcoder Oct 19 '13 at 23:57

2 Answers 2

up vote 2 down vote accepted

These lines:

g = Integer.valueOf(a);
h = Integer.valueOf(b);

will give you an error in one of them because either a or b will always be equal to "" and you can't use Integer.valueOf(String) on an empty String.

To solve this problem, use this following code instead of those lines:

if (!a.equals(""))
    g = Integer.valueOf(a);
else
    g = 0;

if (!b.equals(""))
    h = Integer.valueOf(b);
else
    h = 0;
share|improve this answer
    
You were 22 seconds faster... :-) –  cybergen Oct 20 '13 at 0:01
    
not really... ;-) –  cybergen Oct 20 '13 at 0:05
    
Still not working. Now crashing java.lang.NullPointerException –  LXcoder Oct 20 '13 at 0:17
    
@LXcoder at which line? It could be when you write i = g + h; because if you don't assign h or g to something, like 0, (like I did in my code), it will give you NPE. So which line are you getting your error? Please post your logcat. –  mike yaworski Oct 20 '13 at 0:21
    
@mikeyaworski Log is added –  LXcoder Oct 20 '13 at 0:32

If none of your CheckBoxes are checked, a and b are "" (an empty String). So g = Integer.valueOf(a); and h = Integer.valueOf(b); must fail.

share|improve this answer
    
Yes its true, but when i check cb1 and cb4 i must be equals 2 and I get an error anyway. –  LXcoder Oct 20 '13 at 0:06
    
You should'n get that far with your code, as CheckBoxes are initialized with 'unchecked' state. –  cybergen Oct 20 '13 at 0:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.