0

My floatpoint formulas fail to function if there is a null variable. IE if one of the input variables is null, then no result is displayed in the TextView that displays the result.

As far as I can tell there are 2 solutions to this.

One sets the null EditTexts to 0 if they are null:

if (charisma == null) {
        charisma.setText(0);
    } else if (charisma != null) {
        charisma.setText(getcharisma);
    }

The other ignores null variables in the equation:

try {
        float FP = Float.parseFloat(pref.getString("fp", ""));
        float CHR = Float.parseFloat(charismafpvalue.getText().toString());
        float PER = Float.parseFloat(persuasionfpvalue.getText().toString());
        float ELI = Float.parseFloat(elicitationfpvalue.getText().toString());
        float AML = Float.parseFloat(animalhandlingfpvalue.getText().toString());

        if (charismafpvalue == null) {
            fpr.setText(Float.toString(FP - (PER + ELI + AML)));    
        } else if (charismafpvalue != null) {
            fpr.setText(Float.toString(FP - (CHR + PER + ELI + AML)));
        }



    }
    catch (NumberFormatException ignore) {}

The problem is that my code for both of these fails to function. The values aren't set to 0 if they are null and the formula failed to function despite the if() {} else if() {} code.

Any suggestions as to which is the better way to handle this? And how to implement it?

Mikitz06
  • 340
  • 3
  • 14

4 Answers4

1

A clean way to solve this is to use Apache Commons NumberUtils#toFloat(String str, float defaultValue) method:

float chr = NumberUtils.toFloat(charismafpvalue.getText().toString(), 0.0f);
...

This sets chr to the default value if the conversion fails, so there is no need for try-catch either.

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • this results in the following error: `NumberUtils cannot be resolved` – Mikitz06 Sep 22 '14 at 11:08
  • It seems to fail to import NumberUtils. – Mikitz06 Sep 22 '14 at 11:17
  • Yes, this is part of an external dependency provided by Apache Commons called Commons Lang. You can read more about it and download it [here](http://commons.apache.org/proper/commons-lang/). – Keppil Sep 22 '14 at 11:43
  • I must be missing something, but I can't seem to figure out how to install it... any help here? – Mikitz06 Sep 22 '14 at 12:41
  • [Here](http://stackoverflow.com/questions/2824515/how-to-add-external-library-properly-in-eclipse) is an example on how to di it in Eclipse. There are similar questions for other IDEs as well. – Keppil Sep 22 '14 at 12:45
  • I followed those instructions but when I try to add the .jar files I can't seem them in Eclipse. I've restarted Eclipse already. ~~EDIT~~ Nevermind, I figured it out. – Mikitz06 Sep 22 '14 at 13:02
0

Improve you're String validation.

if (charismafpvalue != null && ! charismafpvalue.isEmpty()) {
  // doSomething
}
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
0

Null cannot be casted to Float so you first need to check

      if(charismafpvalue !=null && charismafpvalue.getText().toString()!=null && !charismafpvalue.getText().equals(""))

similarly to the other views

Suneel Prakash
  • 389
  • 5
  • 7
0

You could use

Float.valuOf(String s) 

Which returns a float representation of "s" if available, or catch the NPE if "s" is null. Like so:

try
{
    float someFloat = Float.valueOf(someString);
}
catch(NumberFormatException nfe)
{
}
catch(NullPointerException npe)
{
}
finally {}

More info here

Of course, this is here for completeness really. Null checking is always a winner! And Keppil's answer is very good also :)

Hope this helps

laminatefish
  • 5,197
  • 5
  • 38
  • 70