NilObjectException occurs when plotting coordinate on Google map

I am trying to plot multiple locations on a Google map, but every time after a certain amount I get a “NilObjectException” error. After I press the start button again, I did not quit the run, but it stopped, the application resumes till a certain amount of additions later and then it stops again with this error. This goes on till the whole list is processed and then it does it again with the next run.

First I thought that the coordinates were not existing or of a wrong value, so every time the run stopped with the “NilObjectException” error, I copied the coordinates from the debug window and entered them in the online Google maps where they were perfectly shown (?).

This is the input I use:
22092015/00:42:50,2.7 , PUERTO RICO REGION,18.71,-65.74
22092015/00:34:12,2.3 , NEVADA,41.87,-119.64
22092015/00:26:03,2.8 , EASTERN TURKEY,39.41,40.49
22092015/00:17:30,4.2 , OFFSHORE GUATEMALA,13.84,-91.51
22092015/00:12:28,3.4 , OFF COAST OF COQUIMBO - CHILE,-31.76,-72.70

and this is the code which read it from a file and should plot it in the map (I use zoom-3 to show the world map):
Dim f As FolderItem
Dim textInput As TextInputStream
Dim RLine, cell(4) as string

f = GetFolderItem(“C:\Users\Rob\Desktop\ewDTS\var\data\db\db.dat”)

If f <> Nil And f.Exists Then
textInput = TextInputStream.Open(f)
textInput.Encoding = Encodings.MacRoman

while not textinput.EOF
  RLine = textinput.ReadLine
  cell = split(Rline,",") 
  
  Try
    CurrentLocation = New WebMapLocation(cell(3)+","+cell(4))
    CurrentLocation.Visible = True
    MapLocation.AddLocation(CurrentLocation)                                      //<<<<<------  this is the line where the NilObjectException occurs
  Catch err As NilObjectException
  End Try

wend

end if

Because I am still in design/code/testphase I use flat sequential files as input. Once when all is working file it will be stored in a SQLite db.

Does somebody have a clue what and why it is happening?

Rob

you should replace

If f <> Nil And f.Exists Then

with

If f <> Nil Then if f.Exists Then ...

because if f is nil, then f.exists gives a nilobjectexception on the same line it gets executed.

@jean-yves pochez: No because if f = nil, it stops and will not continue with the And f.exists.

@Rob Valk : If there are commas in the text in the 3rd field, your code will break.

please try my solution… I already encountered this situation and the whole line gets tested in a “If”.

Sorry but no. It doesn’t.
And I’m glad because I use it everywhere.
If you have an If…Then statement with many ‘And’ -conditions, put the ones that most likely end up as false at the beginning so it stops evaluating the rest.

And as far as Rob’s question goes, the NilObjectException happens at MapLocation.AddLocation(CurrentLocation). Not at checking the FolderItem.

The only objects that could be nil there are MapLocation and CurrentLocation. Since CurrentLocation is used just previously that implies MapLocation is nil.

Try adding this just before the NOE line and see if it breaks. (and/or check MapLocation when you get the NOE)

if MapLocation = nil then break

I’m sorry, It put the comment line wrong, the NOE happens here:

Try
CurrentLocation = New WebMapLocation(cell(3)+","+cell(4)) //<<<<<------ this is the line where the NilObjectException occurs
CurrentLocation.Visible = True
MapLocation.AddLocation(CurrentLocation)
Catch err As NilObjectException
End Try

That is why I thought that cell(3) and cell(4) would have invalid entries or wrong values. But that is not the case. Every time when it stops I open the cell array in the debug windows and all entries are as expected. So, also not illegal comma’s in field 2 (cell(1)). That should also never be the case, because before I store it in a file, the text of field 2 is checked on comma’s and where needed replaced by a dash ‘-’.

So, now my problem is right, the NOE happens in “CurrentLocation = New WebMapLocation(cell(3)+”,"+cell(4))"

Does it always crash at the same line(s)?
If it does, it seems to me that there’s something in there that makes it choke.

(And what kind of control is CurrentLocation? I don’t do Web Apps so maybe it’s something specific?
I would assume you do something like Dim CurrentLocation As New WebMapLocation(cell(3)+","+cell(4)) and then MapLocation.AddLocation(CurrentLocation) but you’re doing a CurrentLocation.Visible = True in the following line.)

Have a look at the message of the exception in the debugger. This will give you a better clue as to what’s going on. Xojo throws a NOE when the provider returns an error.

Are you sure you have a NilObjectException and not an OutOfBoundsException?

[code]Dim cell(4) as string <––– wrong assumption: cell will have an upper bound set
by Split(RLine, “,”). Better write: Dim cell(-1) As String

RLine = textinput.ReadLine <––– what if there is a line break at the end of the file? RLine will be "
"
cell = split(Rline,",") <––– If RLine is "
", then cell(0) will be "
" and UBound of cell will be 0

Try
CurrentLocation = New WebMapLocation(cell(3)+","+cell(4)) <––– so you might have an OutOfBoundsException

[/code]

I can see how you’d get an OutOfBoundsException here but not a NilObjectException.

cell is declared as a 5 element array but that’s replaced by the result of split. The array returned by split may have any number of elements, depends on what RLine is. When you get the NOE I’d look at how many elements cell() consists of and what RLine is.

Dim RLine, cell(4) as string . . RLine = textinput.ReadLine cell = split(Rline,",")
Again, I don’t see how this will cause a NOE, unless somehow split is returning a nil array, which is valid to have a nil array but I’ve never seen split return that.

Me neither.

Here some debug output with a fragment of the input files. This error most of the time happens on the same record, but with next run (timer) it can have other records too with this NilObjectException:

From the debug window:
Line of error: CurrentLocation = New WebMapLocation(cell(3)+","+cell(4))

cell String(4)
0 23092015/01:12:24 (see record 23)
1 2.4
2 PUERTO RICO REGION
3 18.91
4 -67.70

Exception NilObJectException
ErrorNumber 0
Message Status ZERO_RESULTS
Reason Status ZERO_RESULTS

Happens in current run on record#11, 23, 31, 34, 38, 55, 56

- Record

11 23092015/03:31:30,2.4 , CENTRAL TURKEY,37.31,37.13
23 23092015/01:12:24,2.4 , PUERTO RICO REGION,18.91,-67.70
31 22092015/23:48:12,2.7 , PUERTO RICO REGION,19.02,-66.89
34 22092015/23:14:54,2.9 , NEAR THE COAST OF WESTERN TURKEY,38.87,26.43
38 22092015/22:31:15,2.8 , OFFSHORE NORTHERN CALIFORNIA,40.38,-124.85
55 21092015/23:06:01,5.0, New Britain Region - PNG.,-5.955,151.39
56 21092015/20:26:22,5.2, Tonga Islands.,-19.24,-173.381

On next run it gives the same row of records where it happens PLUS a new one, record #17

17 23092015/02:26:34,2.1 , NEAR THE COAST OF WESTERN TURKEY,39.36,26.93

And the strange thing is that this record was also available in the previous run, but did not give this error.

I hope this helps to explain my problem a bit more.

Thank you already for all your responses, it is highly appreciated.

Rob

Oh, and CurrentLocation is a propertie of the MapPage type WebMapLocation :slight_smile:

Looks like you stumbled on an internal bug in WebMapLocation.Constructor
<https://xojo.com/issue/17764>

er, Xojo doesn’t consider it a bug “This case has been closed because the behavior described is not a bug. We are intentionally throwing a NilObjectException for a failed lookup.”

oh…is there a way I can catch this error step over it and resume the application? Or somebody knows another workaround for this? Why would the intentionally throw this NilObjectException, while all the params are right?

Every-time when I enter the coords in Google maps, it perfectly shows the location. So, while does Xojo consider this as a failed lookup?

Anybody an idea how to work around this?

In theory you could apply your own api key to the object. In practice it won’t work. I had to roll my own geolocation class to use my api key then use the lat & long in the webmaplocation object to avoid google issues.

Google puts a limit on how many requests you can make in a certain period of time. If you have an API key as Wayne mentioned you get many more requests. Not sure if this is the issue but sounds like it could be.

A workaround is to repeatedly try to get the location until it’s not nil. (this code is just an example, it’ll lock up your app if run in the main thread and will loop infinitely if the WebMapLocation is never made.)

dim loc As WebMapLocation do Try loc = New WebMapLocation(s) Catch e As NilObjectException App.SleepCurrentThread(1000) //wait a second before trying again End Try loop until loc <> nil

Possibly a better solution is to get an API key and get Wayne to share his class :slight_smile:

Hi Will,

Thanks for your response, but I tried your code (I do have a key too, but have to find out how to use it), but app.SleepCurrentThread() works probably in desktop apps, etc., but it seems not to work in WebApplications. I tried it, but did get an error:

Type App does not have a member “SleepCurrentThread”

The above code runs in the action Event Handler of my eventTimer.

Or…do I something wrong?

No, I was just taking a stab in the dark :slight_smile: I don’t use Web and didn’t realize Sleeps absence.

Well, if it does have problems to use Google maps to plot my locations on, has anybody been busy in Xojo to create his/hers own worldmap and make a raster overlay on it containing coordinates? Any tips?

Else, if a Key is the solution of my problem, does somebody know how to use that in a web application? I am rather new to it and I would not yet know how to use my API key in above code.