NilObjectException

Hello,

I have an app in which after upgrading to 2018r3 keeps getting a NilObjectException at random times even though I have a trap on it. The exception does not crash the program and it keeps operating (a client/server app) but obviously if I click the dialog the program will quit. With the trap handled with a Try/Catch how is this exception happening and what should I look for? Any help would be appreciated.

Thanks

More info required here. Your app is trying to access an object which has not been created, or was created but destroyed. You need to trace the code that relates to your object to see why it has not been instantiated, or why it was destroyed after created.

Cheers
Grant - From XDC 2018

Hey. I think it has to do with the client disconnecting from the server. so If I write a response to a client that disconnects will that return a nil object? here is the code:

[code]dim sock() as TCPSocket = Window1.MainServerSocket.ActiveConnections()
Dim Looper, BailOut as integer

//find the socket handle to write to
for Looper = 0 to Ubound(sock)
if sock(Looper).IsConnected AND sock(Looper).handle = SocketID then
sock(Looper).Write TheAction
me.Sleep 150
sock(Looper).Close
exit
end
next[/code]

I am checking it with the .IsConnected but perhaps I need to trap some kind of error there?

Thanks

No. That will not cause a nilobjectexception.

Ok thanks Tim. I put IOExceptions on all of the server text file logging and double checked all of the objects like Dates, Nil files etc. Its trapping the NilObject but I still cannot find the problem as I cannot recreate the crash. All of these are happening in a thread and Grant mentioned “an object which has not been created, or was created but destroyed”. I am wondering if for some reason the threads are not being terminated properly. I have Return statements at the end of the thread and it in other spots so I cannot see how its not being terminated. I have been working on this problem for a week and still no luck :frowning:

Michael,

I usually put a nil object check in my code somewhere. You could use code like this:

[code]dim sock() as TCPSocket = Window1.MainServerSocket.ActiveConnections()
Dim Looper, BailOut as integer

//find the socket handle to write to
for Looper = 0 to Ubound(sock)
if sock(Looper) <> nil then
if sock(Looper).IsConnected AND sock(Looper).handle = SocketID then
sock(Looper).Write TheAction
me.Sleep 150
sock(Looper).Close
exit
end
end if
next[/code]

This will catch ANY sockets object which are nil.

Cheers
Grant - From XDC 2018

Thanks Grant. I added that to my code just in case. Could also the problem be with nil value coming from a DLL? I added Nil check just in case:

[code]soft Declare Function GetHandState lib “psim.dll” (MyHand as Cstring, postflopinfo as ptr) as integer
Dim postflopstate as new MemoryBlock(36)

value = GetHandState(Cards,postflopstate)
if value = Nil then Return 0

Byte0 = postflopstate.byte(0)
Byte1 = postflopstate.byte(1)
Byte2 = postflopstate.byte(2)[/code]

Michael,

IMHO, any object from any source should be checked for nil before attempting to use it.

Grant - From XDC 2018

I am still getting the NilObject. I put in this in the app.UnHandledException event but the stack trace file is blank:

dim result() as string = error.Stack Dim type As String = Introspection.GetType(error).Name dim f as FolderItem dim i as integer Dim t As TextOutputStream f=GetFolderItem("stack.txt") If f <> Nil Then try t = TextOutputStream.Append(f) t.WriteLine "Error Type: "+Type t.WriteLine "=============" for i = 0 to result.Ubound t.WriteLine(result(i)) next t.Close Catch e As IOException //do nothing end End If