Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Calculator typecast and variables trouble
Started by le_salvagnini Mar 25 2019 12:18 PM

- - - - -
4 replies to this topic
calculator variables typecast nil value error

Best Answer nick_sherman , 25 March 2019 - 01:12 PM

 
Sulftxt.text = (KOHxNumber * Phi)
 

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

le_salvagnini

[GLOBAL: userInfoPane.html]
le_salvagnini
  • Observer

  • 20 posts
  • Corona SDK

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.


[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,787 posts
  • Corona SDK

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:

 

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


  • le_salvagnini likes this

[TOPIC: post.html]
#3

le_salvagnini

[GLOBAL: userInfoPane.html]
le_salvagnini
  • Observer

  • 20 posts
  • Corona SDK

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?



[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,787 posts
  • Corona SDK

  Best Answer

 
Sulftxt.text = (KOHxNumber * Phi)
 


  • le_salvagnini likes this

[TOPIC: post.html]
#5

le_salvagnini

[GLOBAL: userInfoPane.html]
le_salvagnini
  • Observer

  • 20 posts
  • Corona SDK

Thank you !




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: calculator, variables, typecast, nil value, error