Runtime error when performing math on an empty text field

I have built a simple calculator, using a text field, function, and button. When you enter a number, it adds five and returns a result.

Thats fine. If you enter nothing in the text box, you get a runtime error, as Im assuming, arithmetic is attempting to be performed on a empty field, or nothing, and nothing qualifies as a string, which returns an error.

How could I go about getting around this? Im sure I should be doing something about the text in red highlighted below.

Thanks,

local widget = require( “widget” )

result = display.newText(“Result”, 240, 125, 250, 100,  native.systemFont, 20)
result:setFillColor(1,1,1)

field1 = native.newTextField( 150, 150, 180, 40 )
field1.inputType = “number”
field1.size = 16
field1.text = “1”
field1.align = “center”

local calc  = 5;

local function firstfield( event )

    if ( event.phase == “began” ) then
        result.text = (field1.text + calc)
    end   
end
       
local visit = widget.newButton
{
    defaultFile = “button.png”,
    label = “Display”,
    fontSize = 24,
    width = 120,
    height =40,
    labelColor =
    {
        default = { 1, 1, 1 },
    },
    emboss = true,
    onPress = firstfield,
}

visit.x = 150; visit.y = 225

Hi @Betlic,

From a UI standpoint, I would suggest that you don’t let the user proceed unless they enter something in the field. Or, when the button is pressed, if nothing is there simply “return” from the “firstfield” function (effectively just escape out of it and do nothing).

Take care,

Brent

I see, those are good ideas. If I wanted to do the second option, for a comparison to test if nothing is there, would I use nil, such as, if firstfield == nil then?

Hi @Betlic,

In this case, because it’s a text input field, you should check for its string value as being empty (or not). You can read this as “event.target.text” during the “ended” or “submitted” phase as shown in this document:

http://docs.coronalabs.com/api/library/native/newTextField.html

Take care,

Brent

I see. So the format for the example Im using would be, event.target.firstfield ?

No, it would still be “event.target.text”. If you look at the example code in the document I linked, you’ll see how the text field is handled, and how you can read the contents of it.

Brent

So basically, this should work (adjusting the example code):

[lua]

    elseif ( event.phase == “ended” or event.phase == “submitted” ) then

        if ( event.target.text == “” ) then

            return

        end

[/lua]

I see. Great Ill try that. Thank you.

Hi @Betlic,

From a UI standpoint, I would suggest that you don’t let the user proceed unless they enter something in the field. Or, when the button is pressed, if nothing is there simply “return” from the “firstfield” function (effectively just escape out of it and do nothing).

Take care,

Brent

I see, those are good ideas. If I wanted to do the second option, for a comparison to test if nothing is there, would I use nil, such as, if firstfield == nil then?

Hi @Betlic,

In this case, because it’s a text input field, you should check for its string value as being empty (or not). You can read this as “event.target.text” during the “ended” or “submitted” phase as shown in this document:

http://docs.coronalabs.com/api/library/native/newTextField.html

Take care,

Brent

I see. So the format for the example Im using would be, event.target.firstfield ?

No, it would still be “event.target.text”. If you look at the example code in the document I linked, you’ll see how the text field is handled, and how you can read the contents of it.

Brent

So basically, this should work (adjusting the example code):

[lua]

    elseif ( event.phase == “ended” or event.phase == “submitted” ) then

        if ( event.target.text == “” ) then

            return

        end

[/lua]

I see. Great Ill try that. Thank you.