Search code examples
androidnumberformatexception

Unable to reproduce a NumberFormatException


Recently I published an update to my Android app (GitHubRepo) and I've started receiving bug reports through the Play Store from users. I'm trying to troubleshoot, but I'm having trouble tracking down exactly what the error means.

Here's what the reports look like:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.ajwest.BeerConverter/ca.ajwest.BeerConverter.BeerConverterActivity}: java.lang.NumberFormatException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException
at org.apache.harmony.luni.util.FloatingPointParser.parseDblImpl(Native Method)
at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:283)
at java.lang.Double.parseDouble(Double.java:318)
at java.lang.Double.valueOf(Double.java:356)
at ca.ajwest.BeerConverter.BeerConverterActivity.roundTwoDecimals(BeerConverterActivity.java:1513)
at ca.ajwest.BeerConverter.BeerConverterActivity.calculate(BeerConverterActivity.java:987)
at ca.ajwest.BeerConverter.BeerConverterActivity.access$0(BeerConverterActivity.java:757)
at ca.ajwest.BeerConverter.BeerConverterActivity$21.onTextChanged(BeerConverterActivity.java:469)
at android.widget.TextView.sendOnTextChanged(TextView.java:6584)
at android.widget.TextView.setText(TextView.java:2813)
at android.widget.TextView.setText(TextView.java:2671)
at android.widget.EditText.setText(EditText.java:178)
at ca.ajwest.BeerConverter.BeerConverterActivity.onCreate(BeerConverterActivity.java:541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more

Am I correct in assuming it has something to do with the values being rounded by my rounding method?

double roundTwoDecimals(double d){
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
    }

Does anyone have any tips for how to proceed when I can't seem to reproduce the error on my devices? The app relies heavily on users inputting numbers, and even after trying various 'messy' numbers in my fields (I've tried some really long ones), it works flawlessly for me.


Solution

  • You might have not accounted for the user locale. Many locales use commas for separators.

    This will force your formatter to use a point as the separator.

    double roundTwoDecimals(double d){
    
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        DecimalFormatSymbols dfSymbols = new DecimalFormatSymbols();
        twoDForm.setDecimalSeparator('.');
        format.setDecimalFormatSymbols(dfSymbols);
    
        return Double.valueOf(twoDForm.format(d));
    }
    

    If your users are entering numbers manually, you could get the locale separator and use that instead.

    DecimalFormat.getInstance().getDecimalFormatSymbols().getDecimalSeparator();
    

    PS. Your assumption is correct. In general, read down the stack trace until you see a package name from your application. This points to the offending line, in this case:

    ca.ajwest.BeerConverter.BeerConverterActivity.roundTwoDecimals(BeerConverterActivity.java:1513)