Reputation: 149
I'm pretty new to android development and i'm working my way through some sample programs. currently i'm attempting to use a try catch block to prevent the application to crash if the user just presses the created button without entering a number and selecting a route. The try-catch unfortunately is not working as the app keeps crashing and returning the same NumberFormatException. This is a sample of what my code looks like so far.
handleSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleInput = findViewById(R.id.groupInput);
int numAthletes = Integer.parseInt(handleInput.getText().toString());
int totalCost;
try {
if(dropDown.getSelectedItem().toString().equals("Route 1") ||
dropDown.getSelectedItem().toString().equals("Route 2") ||
dropDown.getSelectedItem().toString().equals("Route 3") ){
totalCost = numAthletes * 725;
displayData.setText(dropDown.getSelectedItem().toString() + " Race Team Fee is - $" + Integer.toString(totalCost));
}
}catch (NumberFormatException e){
Log.e( "onClick: ","Empty submission" );
}
}
});
this is the error that is not being catched.
java.lang.NumberFormatException: For input string: ""
Upvotes: 0
Views: 49
Reputation: 3234
You're not catching the right line.
int numAthletes = Integer.parseInt(handleInput.getText().toString());
That's the line causing the exception, but you've placed it outside of the try{} block.
Upvotes: 3