Non stop errors even on code as simple as "sub globals". Can someone advise p

mistermentality

Active Member
Licensed User
Longtime User
[Solved]Non stop errors even on code as simple as "sub globals"

Apologies in advance for the long post but I don't know what I can do apart from ask for advice.

I am having weird non stop and bizarre errors with B4A. No matter what I do the compiler gives the exact same error for completely impossible unrelated things.

I removed everything except....

B4X:
Sub Process_Globals
Dim player As MediaPlayer
Dim phonenumber As String
Dim p As Phone
Dim pe2 As PhoneEvents
Dim serialnumber As String
Dim tempstring, password As String

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
   StartService(incomingdata)
End Sub

Sub Activity_Resume

End Sub
and even then get told the sub globals has a syntax error


I have removed pages of code and still get errors only then with things like the single line "sub globals" claiming it has a syntax error.

The problems only started when I added a simple routine to create and write a map (the map is called m2 in the code). I suddenly got compile errors saying

"Error parsing program.
Error description: An item with the same key has already been added."

Giving me the error line number as the line that wrote the map I removed that line and the error changed, same exact error message but different line.

Eventually I removed the whole process of creating the map by commenting it out but the same error messages continued only now about non related code that I know works and has not been changed (non map related) like

B4X:
Msgbox("Now please press ok to pick the mobile phone number I should contact for help, and send reports to. You can change this later.", "Press ok to continue")

It got to the stage where I removed the entire routine completely so no mention of maps except the already existing contact chooser sub and even then it gave me an error saying that the first line below has a syntax error...

But it doesn't it just says "sub globals" how can this code below be an error causing line?

B4X:
Sub Globals
 Dim enterpassword As InputDialog
End Sub

I can't change that line, there's nothing wrong with it.

If I post the bulk of the code can someone please verify I am not going mad by looking in particular at the map routine near the end and checking I really have not made some stupid error?

I know the code all worked.

It seems weird that even if I remove the map creating routine by commenting it out I still get errors with the same warning about a key yet for things like msgbox lines where it worked fine before. And if I comment out those I get other errors until it gets to the point where I get told sub globals is a syntax error and I can't comment that out or then I get an error that it's missing.

I feel like smashing my pc :(

My activity (back to original code including map routine) is as follows:
B4X:
Sub Process_Globals
Dim player As MediaPlayer
Dim phonenumber As String
Dim p As Phone
Dim pe As PhoneEvents
Dim serialnumber As String
Dim tempstring, password As String

Sub Globals
   Dim enterpassword As InputDialog
End Sub

Sub Activity_Create(FirstTime As Boolean)
   StartService(incomingdata)
   If FirstTime Then
   ' check for saved settings
   'If File.Exists(File.DirInternal, "file.ini") = False Then setup
    player.Initialize( )
    player.Load(File.DirAssets, "slide.mp3")

' code removed to make post shorter, just initialises some panels but
' has remained unchanged and is not causing any issues

End If
   
End Sub


Sub Activity_Resume

End Sub



Sub Button_changenumber_Click
   ' Get and verify password
   enterpassword.Input = enterpassword.INPUT_TYPE_TEXT
   tempstring = enterpassword.Show("Please enter your password in order to change the contact number I will use.", "Please enter your password", "Ok", "","", Null)
            tempstring = enterpassword.Input
            tempstring = tempstring.ToLowerCase
            tempstring = tempstring.Trim
   
   'If password matches the stored password then pick a new contact number
   If tempstring = password  Then picknum
   
   'If the password entered does not match the password stored then let the user know
   If tempstring <> password Then Msgbox("You entered the wrong password", "Error")
End Sub


Sub picknum
' Pick a contact number
Dim Contacts As Contacts
   Dim tempnum As String
   tempnum = "N/A"
    Dim list1 As List
   Do Until tempnum <> "N/A"
    list1 = Contacts.GetAll
    Dim listOfNames As List
    listOfNames.Initialize
    'Create a list with the contacts names
    For i = 0 To list1.Size - 1
        Dim c As Contact
        c = list1.Get(i) 'fetch the Contact from the original list
        If c.DisplayName.IndexOf("@") = -1 Then 'remove email only contacts
            listOfNames.Add(c.DisplayName)
        End If
    Next
    Dim res As Int
    res = InputList(listOfNames, "Choose a mobile phone number (eg mum or dads)", -1)
    If res <> DialogResponse.CANCEL Then
        Dim name As String
        name = listOfNames.Get(res)
        Dim c As Contact
        'find the original contact based on the chosen name
        For i = 0 To list1.Size
            c = list1.Get(i)
            If c.DisplayName = name Then Exit
        Next
       Dim m As Map
            m = c.GetPhones
            If m.Size > 0 Then
                tempnum = m.GetKeyAt(0)
            Else
                tempnum = "N/A"
            End If
        End If
   Loop
   phonenumber = tempnum
        ToastMessageShow(phonenumber, True)
End Sub


Sub setup
' Initialise the map that stores the users preferences
Dim m2 As Map
   m2.Initialize
' Create a password
enterpassword.Input = enterpassword.INPUT_TYPE_TEXT
tempstring = enterpassword.Show("This seems to be the first time you have run this app, please create a password then click on ok. Please remember this password for future use.", "Choose a password", "Ok", "","", Null)
            password = enterpassword.Input
            password = password.ToLowerCase
            password = password.Trim
            serialnumber = p.getsimserialnumber
' get a default number to store
Msgbox("Now please press ok to pick the mobile phone number I should contact for help, and send reports to. You can change this later.", "Press ok to continue")
'picknum

'm2.Put("hint", password)
'm2.Put("originalsim", serialnumber)
'm2.Put("phonenumber", phonenumber)
'File.WriteMap(File.DirInternal, "file.ini", m2)


'Msgbox(password, "Your password is....")
End Sub

And my service is:
B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim PE As PhoneEvents
    Dim SI As SmsInterceptor
   Dim message, sentby As String
   Dim n As Notification
   Dim player2 As MediaPlayer
   Dim simserial As String
   Dim GPS1 As GPS
   Dim latitude, longitude As String
   Dim ph As Phone

End Sub
Sub Service_Create
    PE.Initialize("PE")
    SI.Initialize("SI")
End Sub

Sub Service_Start

End Sub

Sub SI_MessageReceived (From As String, Body As String)
message = Body.ToLowerCase

' If the soundsiren command was issued then do the following
If message.IndexOf("soundsiren") >-1 Then
player2.Initialize
player2.Looping = True
player2.Load(File.DirAssets, "siren.mp3")
player2.Play
End If

'If the stopsiren command was issued then stop playing the siren
If message.IndexOf("stopsiren") >-1 Then
player2.Looping = False
player2.Stop
End If

End Sub

Sub Service_Destroy
    
End Sub

Please can someone confirm that I am indeed sane and there is no reason for the relentless errors that even occur on lines of code as simple as "sub globals"?

I can load previously saved apps and they don't give me the errors but what could possibly make B4A think even the sub globals line is an error yet the exact same code in another app works flawlessly?

Dave
 
Last edited:

kickaha

Well-Known Member
Licensed User
Longtime User
Is it as simple as:
B4X:
Sub Process_Globals
Dim player As MediaPlayer
Dim phonenumber As String
Dim p As Phone
Dim pe As PhoneEvents
Dim serialnumber As String
Dim tempstring, password As String

End Sub [COLOR="Red"]'THIS SEEMS TO BE MISSING!!!![/COLOR]
Sub Globals
   Dim enterpassword As InputDialog
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I can't answer your question directly, but have you tried creating a new app, in case some things has gone wrong internally with the current one?

If that works, try copying some of the code into it.

Steve
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
I always make backups and made a backup but that does the same even though it was a seperate file. Given that even removing all code except the little bit I showed results in an error I am wondering if it is actually a corrupted service module.

I think I will take your advice and try rewriting the module then the main code and report back. Thanks for the idea, and quick reply.

Dave
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
Is it as simple as:
B4X:
Sub Process_Globals
Dim player As MediaPlayer
Dim phonenumber As String
Dim p As Phone
Dim pe As PhoneEvents
Dim serialnumber As String
Dim tempstring, password As String

End Sub [COLOR="Red"]'THIS SEEMS TO BE MISSING!!!![/COLOR]
Sub Globals
   Dim enterpassword As InputDialog
End Sub

No, that must have got deleted when I pasted the code into the post, I was highlighting it to copy it and maybe deleted it but End Sub is in the original code.

Dave
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
First off I want to thank you both for very quick replies, thanks to your ideas I figured out the cause.

First I tried replacing the code but still had errors, I had a backup which had a few lines missing but pasted them in and still got errors so I deleted the service module and reloaded the data but had to give the service a different name because B4A said the module already existed.

I pressed Alt+2 and it compiled, so I added the map code line by line recompiling after each addition and it worked fine so I can only imagine that it was the service module that got corrupted somehow even though I never changed anything on it.

So I can't say what caused the problem but wish I had asked for help hours ago as would have made things much easier. Thank you, again, you really saved me having to try and redo it all from scratch :)

Dave
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I'm glad you got it working.

Steve
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
Thanks to the help I did, yes

I have a problem with the service which isn't a coding error but aside from that it's working :)

Dave
 
Upvote 0
Top