1

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?

Malachi
  • 33,142
  • 18
  • 63
  • 96
Riverside
  • 207
  • 1
  • 12
  • Did you try debugging it? What happens when you get to those lines if your strings are empty? – Michelle Aug 08 '13 at 12:39
  • Debugging doesn't give me much help because it doesn't crash.. It seems it still calculates further as a zero value – Riverside Aug 08 '13 at 12:46
  • Which exceptions are the users getting? You could be getting NullPointerExceptions if cursor is null or if any of the getString calls return null. You could be getting a NumberFormatException if either of the strings cannot be parsed to a double. – Mads Frandsen Aug 08 '13 at 12:50
  • They receive: java.lang.NumberFormatException and stacktrace is showing at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NumberFormatException: Invalid double: "" at java.lang.StringToReal.invalidReal(StringToReal.java:63) – Riverside Aug 08 '13 at 12:55
  • 1
    Try to parse the string with a try/catch block since Double.valueOf can throw NumberFormatException. – haventchecked Aug 08 '13 at 12:55

2 Answers2

1

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.

Michelle
  • 2,830
  • 26
  • 33
  • Well, the input validation is set to Integer. But because I'm working with a database it converts to a string, which I convert back to double. Input of text is not possible. It can be blank, so therefore my check in this code. Update will be tonight right away – Riverside Aug 08 '13 at 13:05
0

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.

Mads Frandsen
  • 523
  • 4
  • 12
  • That looks like a great idea!! However I need crashes from other devices to check if it works. Or can I simulate this in an emulator? – Riverside Aug 08 '13 at 13:02
  • Well, you can simulate it by setting either of the strings to a value that cannot be converted to a double (f.x. elchedb = "bla"). – Mads Frandsen Aug 08 '13 at 13:05
  • Yeah ok.. that just creates an exception. I will check it with the emulator to see if I can get my original to crash. Then I'm also able to test the solution. – Riverside Aug 08 '13 at 13:47
  • You are right, you have to reproduce the problem to find out the exact value of the string that is causing the problem. I think, you should have a look at the code that saves the data, to see if there could be any problems there. In the meantime, as a quick-fix to avoid crashes, you can catch the exception, and set the doubles to some default value that makes sense. – Mads Frandsen Aug 08 '13 at 14:57
  • 1
    Thanks.. I published the update and no crashes reported anymore. Looks like it worked. – Riverside Aug 10 '13 at 16:32