• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Crash caused by EditText(empty)

Muazam

Lurker
My application crash if the user have empty EditText.

Code:
    TextView textA, textB, textX, textEqual, textIncorrect;
    Button buttonGo;
    EditText editA;
    
    Random rand = new Random();
    int textAA = rand.nextInt(10) + 1;
    int textBB = rand.nextInt(10) + 1;
    int Asnwer;
    private int andB;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textA = (TextView)findViewById(R.id.textA);
        textB = (TextView)findViewById(R.id.textB);
        textX = (TextView)findViewById(R.id.textX);
        textEqual = (TextView)findViewById(R.id.textEqual);
        textIncorrect = (TextView)findViewById(R.id.textIncorrect);
        
        editA = (EditText)findViewById(R.id.editA);
        buttonGo = (Button)findViewById(R.id.buttonGo);
        
        
        
        //Set
        textIncorrect.setText("");
        editA.setText(String.valueOf(""));
        textA.setText(String.valueOf(textAA));
        textB.setText(String.valueOf(textBB));

        
        buttonGo.setOnClickListener(this);
        
    }

    @Override
    public void onClick(View src) {
        switch(src.getId()) {
        case R.id.buttonGo:
            andB = this.textBB * this.textAA;
            if(andB == Integer.parseInt(editA.getText().toString())) {
                
                editA.setText("");
                this.textIncorrect.setTextColor(Color.BLUE);
                this.textIncorrect.setText("CORECCT: " + this.textBB + " x " + this.textAA + " = " + andB);
                this.textAA = rand.nextInt(50)+1;
                this.textBB = rand.nextInt(50)+1;
                textA.setText(String.valueOf(this.textAA));
                textB.setText(String.valueOf(this.textBB));
                
            }else if(Integer.parseInt(editA.getText().toString()) < 0){
                this.textIncorrect.setText("Error!");

            }else{
                this.textIncorrect.setTextColor(Color.RED);
                this.textIncorrect.setText("INCORECCT!");
            }
            }
        }
        
    }

I've tried several solution found on this board, but none of them worked for me. Anyone have any idea how to fix this? The problem is in the "else if".

Thanks.
 
use a Try/Catch statment for the parse. You can't parse nothing so it wil give an error
with try catch if there's a fatal error it wil skip to the catch

Good luck ;)
 
befor the switch case and use use the value

try {
Integer.parseInt(editA.getText().toString();
} catch (InterruptedException e) {
this.textIncorrect.setText("Error!");
boolean usableInt = false
}


//leave the elsif where it is now

else if(Integer.parseInt(editA.getText().toString()) < 0 && usableInt = true){
 
I added "boolean usableInt;" in my main class.

Code:
    public void onClick(View src) {
        switch(src.getId()) {
        case R.id.buttonGo:
            
            try {
                Integer.parseInt(editA.getText().toString());
                } catch (InterruptedException e) {
                this.textIncorrect.setText("Error!");
                this.usableInt = false;
                }
            
            andB = this.textBB * this.textAA;
            if(andB == Integer.parseInt(editA.getText().toString())) {
                
                editA.setText("");
                this.textIncorrect.setTextColor(Color.parseColor("#87d9ff"));
                this.textIncorrect.setText("CORECCT: " + this.textBB + " x " + this.textAA + " = " + andB);
                this.textAA = rand.nextInt(50)+1;
                this.textBB = rand.nextInt(50)+1;
                textA.setText(String.valueOf(this.textAA));
                textB.setText(String.valueOf(this.textBB));
                
            }else if(Integer.parseInt(editA.getText().toString()) < 0 && this.usableInt != false){ 

            }else{
                this.textIncorrect.setTextColor(Color.RED);
                this.textIncorrect.setText("INCORECCT!");
            }
        }
        }
I'm getting "Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body" any idea why?

on
} catch (InterruptedException e) {
Thanks.
 
Sorry that's somting from my code :P

InterruptedException e is not necceserry

Code:
            try {
                String sValue = Integer.parseInt(editA.getText().toString());
                } catch () {
                this.textIncorrect.setText("Error!");
                this.usableInt = false;
                }
 
Replaced switch with if-else to, to get it better sorted.

Code:
        buttonGo.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View view) {
                AtimesB = nrB * nrA;
                try {
                    sValue = Integer.parseInt(editA.getText().toString());
                }catch(Exception e){
                    textInC.setText("Error!");
                    usableInt = false;
                    }
                if(AtimesB == Integer.parseInt(editA.getText().toString()) && usableInt == true){
                    editA.setText("");
                    textC.setTextColor(Color.parseColor("#87d9ff"));
                    textC.append("CORECCT: " + nrB + " x " + nrA + " = " + AtimesB + "\n");
                    textInC.setText("");
                    nrA = rand.nextInt(50)+1;
                    nrB = rand.nextInt(50)+1;
                    textA.setText(String.valueOf(nrA));
                    textB.setText(String.valueOf(nrB));
                }else if(Integer.parseInt(editA.getText().toString()) < 0 && usableInt == false){
                    textInC.setTextColor(Color.RED);
                    textInC.setText("Error");
                }else{
                    textInC.setText("INCORECCT");
                }
            }
        });
        }
    
}
Hmm.. Looks like the Try does not get triggered, still does not work.
Any idea? Thanks.
 
Repace

Code:
else if(Integer.parseInt(editA.getText().toString()) < 0 && usableInt == false){
                    textInC.setTextColor(Color.RED);
                    textInC.setText("Error");
                }

with

Code:
else if(usableInt != false){
                    if (Integer.parseInt(editA.getText().toString()) < 0){
                           textInC.setTextColor(Color.RED);
                           textInC.setText("Error");
                    }
                }
 
I think an edittext returns null if there is no text in it, which will cause an exception when trying to make it to a String. You should check if editA.getText() returns a null before actually trying to make it to a String:
Code:
if (editA.getText() != null && Integer.parseInt(editA.getText().toString()) < 0)
 
Hey guys, I'm having a similar problem that I am having trouble working around. With the way this code runs, both fields have a 0 automatically populated into the fields onCreate, and the clear button reverts both values back to zero. My issue is that when I want to find the F
 
Back
Top Bottom