Getting values from native textboxes

In my app, I am getting two values from seperate native textboxes - one for estimated burn percentage and one for patient weight.  

The problem is, if there is no value and they click the calculate button, I get an error about nil.

Here’s my code:

 local Burns = 0 local Weight = 0 if (EstTextArea.text == nil) then Burns = 0 else Burns = tonumber( EstTextArea.text ) end if (WtTextArea.text == nil) then Weight = 0 else Weight = tonumber( WtTextArea.text ) end local TotalFluids = 4 \* Burns \* Weight local Fluids8 = (TotalFluids / 2) / 8 local Fluids16 = (TotalFluids / 2) / 16 

I am testing whether the text is nill and if it is, setting Burns to 0 otherwise I’m setting it to the numeric value of EstTextArea.text.

I get an error that says "attempt to perform arithmetic on local Burns (a nil value)"

Shouldn’t it be setting Burns to 0 if EstTextArea.text but it doesn’t look like it is.

What am I doing wrong?

Hi John,

You might want to be checking that “EstTextArea.text” is not an empty string ("") instead of nil. Or better yet, just do a conditional check on whether “tonumber” succeeds… if it doesn’t, then you know it’s not a number, and you default it to 0.

[lua]

if ( tonumber( EstTextArea.text ) == nil ) then

   Burns = 0

else

   Burns = tonumber( EstTextArea.text )

end

[/lua]

Take care,

Brent

Hi John,

You might want to be checking that “EstTextArea.text” is not an empty string ("") instead of nil. Or better yet, just do a conditional check on whether “tonumber” succeeds… if it doesn’t, then you know it’s not a number, and you default it to 0.

[lua]

if ( tonumber( EstTextArea.text ) == nil ) then

   Burns = 0

else

   Burns = tonumber( EstTextArea.text )

end

[/lua]

Take care,

Brent