user5307594
user5307594

Reputation:

Application failing with java.lang.NumberFormatException: Invalid int

If I try to launch my application, it fails and displays this error:

09-17 20:41:18.190    2063-2063/cz.test.sudoman281.lesnicketabulky E/AndroidRuntime? FATAL EXCEPTION: main
    Process: cz.test.sudoman281.lesnicketabulky, PID: 2063
    java.lang.NumberFormatException: Invalid int: ""
            at java.lang.Integer.invalidInt(Integer.java:138)
            at java.lang.Integer.parseInt(Integer.java:358)
            at java.lang.Integer.parseInt(Integer.java:334)
            at cz.test.sudoman281.lesnicketabulky.MainActivity$1.onFocusChange(MainActivity.java:36)
            at android.view.View.onFocusChanged(View.java:5192)
            at android.widget.TextView.onFocusChanged(TextView.java:7989)
            at android.view.View.handleFocusGainInternal(View.java:4948)
            at android.view.View.requestFocusNoSearch(View.java:7547)
            at android.view.View.requestFocus(View.java:7526)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2513)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2513)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2513)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2513)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2513)
            at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2557)
            at android.view.ViewGroup.requestFocus(ViewGroup.java:2516)
            at android.view.View.requestFocus(View.java:7493)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1968)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1087)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6040)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:793)
            at android.view.Choreographer.doCallbacks(Choreographer.java:606)
            at android.view.Choreographer.doFrame(Choreographer.java:575)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:779)
            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:5538)
            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:960)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

My code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button spocitat = (Button) findViewById(R.id.spocitat);
    EditText delka = (EditText) findViewById(R.id.delka);
    EditText tloustka = (EditText) findViewById(R.id.tloustka);

    delka.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            EditText delka = (EditText) findViewById(R.id.delka);
            EditText tloustka = (EditText) findViewById(R.id.tloustka);
            TextView prumer = (TextView) findViewById(R.id.prumer);
            RadioButton smrk = (RadioButton) findViewById(R.id.smrk);
            RadioButton borovice = (RadioButton) findViewById(R.id.borovice);
            RadioButton boroveOddenky = (RadioButton) findViewById(R.id.oddenky);
            RadioButton buk = (RadioButton) findViewById(R.id.buk);
            RadioButton dub = (RadioButton) findViewById(R.id.dub);

            if (delka != null && tloustka != null) {
                int L = Integer.parseInt(delka.getText().toString());
                int Dsk = Integer.parseInt(tloustka.getText().toString());
                if (smrk.isChecked()) {
                    double p0 = Math.pow(5.7723 * 10, -1);
                    double p1 = Math.pow(6.8968 * 10, -3);
                    double p2 = Math.pow(1.3123 * 10, 0);
                    double Dp2 = Math.pow(Dsk, p2);
                    double pi = 3.14159;
                    double cast1 = Math.pow(Dsk - (p0 + p1 * Dp2), 2);
                    int vypocet = (int) Math.floor(((cast1 * pi * L) / 40000) * 100);
                    prumer.setText(String.valueOf(vypocet));
                } else if (borovice.isChecked()) {

                } else if (boroveOddenky.isChecked()) {

                } else if (buk.isChecked()) {

                } else if (dub.isChecked()) {

                } else {
                    prumer.setText("Musíte zvolit drevinu!");
                }
            }
        }
    });
}

Upvotes: 0

Views: 575

Answers (1)

Amila
Amila

Reputation: 5213

This is because the String you're trying to parse into Integer is empty in one of the following two lines:

int L = Integer.parseInt(delka.getText().toString());
int Dsk = Integer.parseInt(tloustka.getText().toString());

The line if (delka != null && tloustka != null) doesn't fix it because you're only checking if the Text box instances are not null. You need to verify if their contents are not empty too.

Upvotes: 1

Related Questions