• 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
Feb 23, 2011
8
0
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.
 
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.
 
Upvote 0
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.
 
Upvote 0
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)
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones