Question

Android keeps on reporting crashes from users which I can't reproduce on my phone. I can find the lines which seem to be incorrect:

cursor.moveToFirst();
elechs=cursor.getString(2);
elecls=cursor.getString(3);
gass=cursor.getString(4);
waters=cursor.getString(5);

cursor.close();

if (elechs.length()!=0){
  elechdb=Double.valueOf(elechs);
}
else {
  elechdb=0.0;
}

if (elecls.length()!=0){
  elecldb=Double.valueOf(elecls);}
else {
  elecldb=0.0;
}

if (gass.length()!=0){
  gasdb=Double.valueOf(gass);
}
else {
  gasdb=0.0;
}

if (waters.length()!=0){
  waterdb=Double.valueOf(waters);
}
else {
  waterdb=0.0;
}

elecldb=Double.valueOf(elecls);
gasdb=Double.valueOf(gass);
waterdb=Double.valueOf(waters);

If I look at the code, it doesn't make any sense.

I think I forgot to delete the last three lines. First I check the string. If the string is empty it will store the value as zero.

The incorrect last three lines will also try to make a if the cell is empty. This cause a lot of crashes. However not on my machine.

I believe that it shouldn't be possible to make a of an empty cell.

Does anyone know why this error doesn't crash my phone?

Was it helpful?

Solution 2

Since the users are getting NumberFormatException, you should catch the exception and perform appropriate action when it happens.

if (elechs.length()!=0) {
   try {
       elechdb=Double.valueOf(elechs);
   } catch (NumberFormatException e) { 
       // Perform error handling
   }
}

If the users are getting NullPointerException, you should check if the strings are null before checking for their lengths.

If the data is put in by the user, you should do both of the above to avoid future problems.

OTHER TIPS

Your best solution is probably just to remove the problematic lines that shouldn't be there anyway, preferably add actual error handling around calls to Double.valueOf() in case the input is completely malformed (there may be inconsistent behaviour if the cell is empty, but if it says "hello world", everything will crash), and release an update.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top