Calculator typecast and variables trouble

People, I´m trying to do a very simple calculator just two variables and one is linked to the other. It means that I only need to input one number in one of two fields that will appear on screen and the other would be automatically calculated.

But I´m getting some error and I don´t know why.

The error message is: main.lua attempts to compare nil with number.

The error is pointing to line 26 on my code.

What I´m doing wrong? 

Does anybody can help me?

Thanks in advance.

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here header = display.newText( "KalKHOS", 50, 5 , native.systemFontBold , 20 ) header:setFillColor(1, 0.5, 0) local KOHtxt = display.newText( "KOH (g.)", 50, 50 , native.systemFontBold , 20 ) KOHtxt:setFillColor(1, 1, 1) local KOHx = native.newTextField( 120, 90, 220, 36 ) KOHx.inputType = "number" local Sulftxt = display.newText( "S (g.)", 35, 140 , native.systemFontBold , 20 ) Sulftxt:setFillColor(1, 1, 0) local Sulfx = native.newTextField( 120, 180, 220, 36 ) Sulfx.inputType = "number" function show() local Phi = 1.6 local KOHxNumber = tonumber (KOHx.text) local SulfxNumber = tonumber (Sulfx.text) if KOHxNumber \> SulfxNumber then Sulftxt = (KOHxNumber \* Phi) print(Sulftxt.text) end end header:addEventListener("tap", show) --Result displayed when clicked on name.

If you’re only entering one number, then the other text field will be a blank string and convert to nil rather than 0. 

You could do:

[lua]

  1. local KOHxNumber = tonumber (KOHx.text) or 0
  2. local SulfxNumber = tonumber (Sulfx.text) or 0

[/lua]

Now it´s giving another error on line 26:

main.lua:26 attempt to index upvalue (‘Sulftxt’) a number value.

Shall I typecast it again?

[lua]

Sulftxt.text = (KOHxNumber * Phi)

[/lua]

Thank you !

If you’re only entering one number, then the other text field will be a blank string and convert to nil rather than 0. 

You could do:

[lua]

  1. local KOHxNumber = tonumber (KOHx.text) or 0
  2. local SulfxNumber = tonumber (Sulfx.text) or 0

[/lua]

Now it´s giving another error on line 26:

main.lua:26 attempt to index upvalue (‘Sulftxt’) a number value.

Shall I typecast it again?

[lua]

Sulftxt.text = (KOHxNumber * Phi)

[/lua]

Thank you !