Question

I have a function in Android wherein it gets the value returned by a TextView which are totalcost_venue, totalcost_apparel, totalcost_souvenir, and totalcost_caterer.

The value is converted to double and after which I have to add those values. But I encountered an error when a particular TextView does not return any value. Any help will do. Thanks!

double totalcost_venue_finalconverted = Double.parseDouble(totalcost_venue.getText().toString());
double totalcost_apparel_finalconverted = Double.parseDouble(totalcost_apparel.getText().toString());
double totalcost_caterer_finalconverted = Double.parseDouble(totalcost_caterer.getText().toString());
double totalcost_souvenir_finalconverted =  Double.parseDouble(totalcost_souvenir.getText().toString());

double total = (totalcost_venue_finalconverted + totalcost_apparel_finalconverted + totalcost_caterer_finalconverted +totalcost_souvenir_finalconverted);

String total_all= Double.toString(total);

totalcost_all.setText(total_all);
String totalcost_final = totalcost_all.getText().toString();

double budget_converted = Double.parseDouble(budget_event);
double totalcost_converted = Double.parseDouble(totalcost_final);

if(budget_converted <= totalcost_converted){
                registerErrorMsg.setText(" Oops!! Already exceeded");
Was it helpful?

Solution

Double.parseDouble() may be throwing an exception if it recieves a null value, or it is recieves text.

To solve your issue with a null value, you could add the following check:

double totalcost_venue_finalconverted = 0;

if(!totalcost_venue.getText().toString().equals("")){
    totalcost_venue_finalconverted = Double.parseDouble(totalcost_venue.getText().toString());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top