Android Question java.lang.NumberFormatException error

luisftv

Member
Licensed User
Longtime User
I'm having a little problem. I made a very simple game of randomizing a range of numbers. I click a button and then a random number appears on the screen based on a manual range. EVERYTHING is working fine until the range is left empty (I have two "EditText" boxes to enter the range) and then I get this "java.lang.NumberFormatException" error. I only get the error if not entering any numbers as my ranges, if left empty.

This is my first app with Basic4Android, and although I find it very logical and awesome, it is hard to find specific examples and solutions...

Can someone please help? Can you provide the exact code to enter and explain it? Thank you so much in advance. Here is the code, the issue I think is in the "btnrandom_Click" section:


B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Timer1 As Timer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Start_Range, End_Range As EditText
    Dim K As Int
    Dim Label1 As Label
    Dim btnrandom As Button
    Dim Exit As Button
    Dim stayawake As PhoneWakeState
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("gnnLayout01custom")
    Timer1.Initialize ("Timer1",2000)
   
    '==========================================================================
    'The following is to change the background color on "EditText" fields:
    '
    Dim enabled1, disabled1 As ColorDrawable
        enabled1.Initialize(Colors.Transparent, 10dip)
          disabled1.Initialize(Colors.Transparent, 10dip)
   
      Dim sld As StateListDrawable
          sld.Initialize
          sld.AddState(sld.State_Enabled, enabled1)
          sld.AddState(sld.State_Disabled, disabled1)
      Start_Range.Background = sld
    End_Range.Background = sld
    '
    'Or use this other method, which uses a backgound image:
    '
    'Dim enabled2, disabled2 As BitmapDrawable
    'enabled2.Initialize(LoadBitmap(File.DirAssets, "Copper_Brushed_01.jpg"))
    'disabled2.Initialize(LoadBitmap(File.DirAssets, "Copper_Brushed_01.jpg"))
    '
    'Dim sld As StateListDrawable
    'sld.Initialize
    'sld.AddState(sld.State_Enabled, enabled2)
    'sld.AddState(sld.State_Disabled, disabled2)
    'Start_Range.Background = sld
    'End_Range.Background = sld
    '==========================================================================
   
End Sub

Sub Activity_Resume
    'stayawake.KeepAlive (True) 'This will automatically keep the screen on as app is loaded
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    'stayawake.ReleaseKeepAlive 'This will automatically keep the screen off as app is closed
End Sub

Sub btnrandom_Click
    Dim SR, ER As Float
    SR = Start_Range.Text
    ER = End_Range.Text

    'If IsNumber(Start_Range.Text) = True Then
    'If Start_Range.Text = True Then
    '    k = Rnd (SR, ER+1)
    '    Label1.Text = K
    '    Timer1.Enabled = True
    'Else
    '    Msgbox("Enter Range", "Error")
    'End If
    'If IsNumber(End_Range.Text) = True Then
    'If End_Range.Text = True Then
    '    k = Rnd (SR, ER+1)
    '    Label1.Text = K
    '    Timer1.Enabled = True
    'Else
    '    Msgbox("Enter Range", "Error")   
    'End If


    'If IsNumber(Start_Range.Text) = " " Then
    'If Start_Range.Text < 0 Then
    '    Msgbox("Enter Range", "Error")
    'Else
    '    k = Rnd (SR, ER+1)
    '    Label1.Text = K
    '    Timer1.Enabled = True
    'End If
    '   
    'If IsNumber(End_Range.Text) = " " Then
    'If End_Range.Text < 0 Then
    '    Msgbox("Enter Range", "Error")
    'Else
    '    k = Rnd (SR, ER+1)
    '    Label1.Text = K
    '    Timer1.Enabled = True
    'End If
   
    If Start_Range.Text > End_Range.Text Then
        Msgbox("'Start Range' must be less than 'End Range'", "")
    Else If Start_Range.Text = End_Range.Text Then
        Msgbox("The RANGE must be at least one number appart!", "")
    Else If Start_Range.Text = Null Then 'I am using Null to say empty or no value, is this right?
        Msgbox("Enter the Start Range!", "")
    Else If End_Range.Text = Null Then 'I am using Null to say empty or no value, is this right?
        Msgbox("Enter the End Range!", "")
    Else
        k = Rnd (SR, ER+1)
        Label1.Text = K
        Timer1.Enabled = True
    End If
End Sub

Sub Timer1_Tick
    Label1.Text = ""
    Timer1.Enabled = False
End Sub

Sub ScreenCheckBox1_CheckedChange(Checked As Boolean)
    If Checked = True Then
        stayawake.KeepAlive (True)
    Else
        stayawake.ReleaseKeepAlive
    End If
End Sub

Sub Exit_Click
        Activity.Finish
End Sub
 

klaus

Expert
Licensed User
Longtime User
The problem is here:
B4X:
SR = Start_Range.Text
ER = End_Range.Text
If the EditText view Text property is empty you cannot convert it to a number.

You need to check if one or both are empty like:
B4X:
If Start_Range.Text = "" Or End_Range.Text = "" Then
    MsgBox("One of the limits is missing", "Error")
    Return
End If

SR = Start_Range.Text
ER = End_Range.Text
 
Upvote 0

luisftv

Member
Licensed User
Longtime User
I've been in many and very difficult situations in my life, too many to count. Suffice to say that each time I was half a second from loosing my life... I've also been in unique, enlightening, amazing situations... but they are far less than the bad ones. I just thought to let you know that you Erel, and you Mr. Klaus have giving me another one of those awesome moments: Erel, thanks for making Basic4Android, and Erel and Klaus, thank you for your support... you both gave me the solution. Klause, I copy/pasted the code you posted and it worked perfectly. I modified it later though... that's my nature.

Now I know that the order into which you write the code affects how the output is grabbed by other parts of the code. It makes perfect sense. Now, I'll go hit myself against a wall for not thinking about it before...

Thank you again.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…