1. Download our Official Android App: Forums for Android!

Apps Crash caused by EditText(empty)

Discussion in 'Android Development' started by Muazam, Feb 23, 2011.

  1. Muazam

    Muazam Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    8
    Joined:
    Feb 23, 2011

    Feb 23, 2011
    8
    0
    25
    My application crash if the user have empty EditText.

    Code (Text):
    1.     TextView textA, textB, textX, textEqual, textIncorrect;
    2.     Button buttonGo;
    3.     EditText editA;
    4.    
    5.     Random rand = new Random();
    6.     int textAA = rand.nextInt(10) + 1;
    7.     int textBB = rand.nextInt(10) + 1;
    8.     int Asnwer;
    9.     private int andB;
    10.    
    11.    
    12.     @Override
    13.     public void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.         setContentView(R.layout.main);
    16.         textA = (TextView)findViewById(R.id.textA);
    17.         textB = (TextView)findViewById(R.id.textB);
    18.         textX = (TextView)findViewById(R.id.textX);
    19.         textEqual = (TextView)findViewById(R.id.textEqual);
    20.         textIncorrect = (TextView)findViewById(R.id.textIncorrect);
    21.        
    22.         editA = (EditText)findViewById(R.id.editA);
    23.         buttonGo = (Button)findViewById(R.id.buttonGo);
    24.        
    25.        
    26.        
    27.         //Set
    28.         textIncorrect.setText("");
    29.         editA.setText(String.valueOf(""));
    30.         textA.setText(String.valueOf(textAA));
    31.         textB.setText(String.valueOf(textBB));
    32.  
    33.        
    34.         buttonGo.setOnClickListener(this);
    35.        
    36.     }
    37.  
    38.     @Override
    39.     public void onClick(View src) {
    40.         switch(src.getId()) {
    41.         case R.id.buttonGo:
    42.             andB = this.textBB * this.textAA;
    43.             if(andB == Integer.parseInt(editA.getText().toString())) {
    44.                
    45.                 editA.setText("");
    46.                 this.textIncorrect.setTextColor(Color.BLUE);
    47.                 this.textIncorrect.setText("CORECCT: " + this.textBB + " x " + this.textAA + " = " + andB);
    48.                 this.textAA = rand.nextInt(50)+1;
    49.                 this.textBB = rand.nextInt(50)+1;
    50.                 textA.setText(String.valueOf(this.textAA));
    51.                 textB.setText(String.valueOf(this.textBB));
    52.                
    53.             }else if(Integer.parseInt(editA.getText().toString()) < 0){
    54.                 this.textIncorrect.setText("Error!");
    55.  
    56.             }else{
    57.                 this.textIncorrect.setTextColor(Color.RED);
    58.                 this.textIncorrect.setText("INCORECCT!");
    59.             }
    60.             }
    61.         }
    62.        
    63.     }
    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.
     

    Advertisement

  2. pietjuhhh1990

    pietjuhhh1990 Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    235
    Joined:
    Feb 21, 2011

    Feb 21, 2011
    235
    22
    38
    App/web development
    Netherlands
    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 ;)
     
    Muazam likes this.
  3. Muazam

    Muazam Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    8
    Joined:
    Feb 23, 2011

    Feb 23, 2011
    8
    0
    25
    Thanks for replying. Can you tell me where to put it and what to put in it?
     
  4. pietjuhhh1990

    pietjuhhh1990 Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    235
    Joined:
    Feb 21, 2011

    Feb 21, 2011
    235
    22
    38
    App/web development
    Netherlands
    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){
     
    Muazam likes this.
  5. Muazam

    Muazam Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    8
    Joined:
    Feb 23, 2011

    Feb 23, 2011
    8
    0
    25
    I added "boolean usableInt;" in my main class.

    Code (Text):
    1.     public void onClick(View src) {
    2.         switch(src.getId()) {
    3.         case R.id.buttonGo:
    4.            
    5.             try {
    6.                 Integer.parseInt(editA.getText().toString());
    7.                 } catch (InterruptedException e) {
    8.                 this.textIncorrect.setText("Error!");
    9.                 this.usableInt = false;
    10.                 }
    11.            
    12.             andB = this.textBB * this.textAA;
    13.             if(andB == Integer.parseInt(editA.getText().toString())) {
    14.                
    15.                 editA.setText("");
    16.                 this.textIncorrect.setTextColor(Color.parseColor("#87d9ff"));
    17.                 this.textIncorrect.setText("CORECCT: " + this.textBB + " x " + this.textAA + " = " + andB);
    18.                 this.textAA = rand.nextInt(50)+1;
    19.                 this.textBB = rand.nextInt(50)+1;
    20.                 textA.setText(String.valueOf(this.textAA));
    21.                 textB.setText(String.valueOf(this.textBB));
    22.                
    23.             }else if(Integer.parseInt(editA.getText().toString()) < 0 && this.usableInt != false){
    24.  
    25.             }else{
    26.                 this.textIncorrect.setTextColor(Color.RED);
    27.                 this.textIncorrect.setText("INCORECCT!");
    28.             }
    29.         }
    30.         }
    I'm getting "Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body" any idea why?

    on
    Thanks.
     
  6. pietjuhhh1990

    pietjuhhh1990 Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    235
    Joined:
    Feb 21, 2011

    Feb 21, 2011
    235
    22
    38
    App/web development
    Netherlands
    Sorry that's somting from my code :p

    InterruptedException e is not necceserry

    Code (Text):
    1.  
    2.             try {
    3.                 String sValue = Integer.parseInt(editA.getText().toString());
    4.                 } catch () {
    5.                 this.textIncorrect.setText("Error!");
    6.                 this.usableInt = false;
    7.                 }
    8.  
     
  7. Muazam

    Muazam Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    8
    Joined:
    Feb 23, 2011

    Feb 23, 2011
    8
    0
    25
    Replaced switch with if-else to, to get it better sorted.

    Code (Text):
    1.         buttonGo.setOnClickListener(new View.OnClickListener() {
    2.            
    3.             @Override
    4.             public void onClick(View view) {
    5.                 AtimesB = nrB * nrA;
    6.                 try {
    7.                     sValue = Integer.parseInt(editA.getText().toString());
    8.                 }catch(Exception e){
    9.                     textInC.setText("Error!");
    10.                     usableInt = false;
    11.                     }
    12.                 if(AtimesB == Integer.parseInt(editA.getText().toString()) && usableInt == true){
    13.                     editA.setText("");
    14.                     textC.setTextColor(Color.parseColor("#87d9ff"));
    15.                     textC.append("CORECCT: " + nrB + " x " + nrA + " = " + AtimesB + "\n");
    16.                     textInC.setText("");
    17.                     nrA = rand.nextInt(50)+1;
    18.                     nrB = rand.nextInt(50)+1;
    19.                     textA.setText(String.valueOf(nrA));
    20.                     textB.setText(String.valueOf(nrB));
    21.                 }else if(Integer.parseInt(editA.getText().toString()) < 0 && usableInt == false){
    22.                     textInC.setTextColor(Color.RED);
    23.                     textInC.setText("Error");
    24.                 }else{
    25.                     textInC.setText("INCORECCT");
    26.                 }
    27.             }
    28.         });
    29.         }
    30.    
    31. }
    Hmm.. Looks like the Try does not get triggered, still does not work.
    Any idea? Thanks.
     
  8. pietjuhhh1990

    pietjuhhh1990 Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    235
    Joined:
    Feb 21, 2011

    Feb 21, 2011
    235
    22
    38
    App/web development
    Netherlands
    Repace

    Code (Text):
    1.  
    2. else if(Integer.parseInt(editA.getText().toString()) < 0 && usableInt == false){
    3.                     textInC.setTextColor(Color.RED);
    4.                     textInC.setText("Error");
    5.                 }
    6.  
    with

    Code (Text):
    1.  
    2. else if(usableInt != false){
    3.                     if (Integer.parseInt(editA.getText().toString()) < 0){
    4.                            textInC.setTextColor(Color.RED);
    5.                            textInC.setText("Error");
    6.                     }
    7.                 }
    8.  
     
  9. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    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 (Text):
    1.  
    2. if (editA.getText() != null && Integer.parseInt(editA.getText().toString()) < 0)
    3.  
     
  10. Muazam

    Muazam Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    8
    Joined:
    Feb 23, 2011

    Feb 23, 2011
    8
    0
    25
    Its not triggering the "try".

    Fixed it, thanks trying guys.
     
  11. dcparham

    dcparham Lurker
    Rank:
    None
    Points:
    6
    Posts:
    7
    Joined:
    Jun 17, 2011

    Jun 17, 2011
    7
    1
    6
    Web Developer
    35
    Can you please elaborate?? THANKS!
     
  12. iceslim0

    iceslim0 Lurker
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Jun 28, 2011

    Jun 28, 2011
    3
    0
    5
    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
     
  13. manvinder

    manvinder Newbie
    Rank:
    None
    Points:
    16
    Posts:
    27
    Joined:
    May 9, 2011

    May 9, 2011
    27
    1
    16
    Software Developer
    New Delhi
    you are not able to find the F
     

Share This Page

Loading...