New topic: NilObjectException masquerading as an OutOfBoundsException
<http://forums.realsoftware.com/viewtopic.php?t=45692> Page 1 of 1 [ 4 posts ] Previous topic | Next topic Author Message Jason_Adams Post subject: NilObjectException masquerading as an OutOfBoundsExceptionPosted: Tue Oct 23, 2012 2:13 pm Joined: Fri Nov 10, 2006 4:10 pm Posts: 1689 Location: Michigan, USA Hello all! So here's an interesting one. I have code like this in the DataRecieved() event of a TCPSocket: Try Select Case Msg(0) Case INSystemStatus // Code#Description Dim Status(1) As String = Msg(1).Split("#") SystemStatus(Status(0).Val, Status(1)) End Select Catch OutOfBoundsException ErrorMessage = "PLC command failed: caught OutOfBoundsException" + EndOfLine + EndOfLine _ + "Message: " + List(x) + EndOfLine + EndOfLine _ + "Buffer: " + Join(List, EndOfLine) _ + "Position: " + Str(Position) Error (ErrorMessage, True) End Try Now SystemStatus is an Event Definition. This in turn is used by a Window, which passes the information to a Container Control. In that CC it calls a sub, and in this sub is this code (there's a lot more adjacent code, but this is the point of interest): // Set Graphing Windows If (winDataGraph Is Nil) Then winDataGraph = New wGraph If (winFrictionGraph Is Nil) Then winFrictionGraph = New wGraph If (winTempGraph Is Nil) Then winTempGraph = New wGraph winDataGraph.NewGraph ("Data", "Revolutions", "Distance (microns)") winFrictionGraph.NewGraph ("Friction", "Revolutions", "No Unit") winTempGraph.NewGraph ("Temperature", "Seconds", "Celcius") winVertGraph.NewGraph ("Vertical Force", "Revolutions", "Force (lbf)") What's wrong here? Oops! I forgot to instantiate winVertGraph (notice the last line). That constitutes a NilObjectException, because I'm immediately trying to use one of its methods. Here's where this get's interesting. What I just posted is caught as an OutOfBoundsException by the Try Catch in the TCPSocket â which is interesting, because I'm only trying to catch OutOfBoundExceptions specifically (see first code). Is this a bug? Is this an element of Event-driven Programming I'm unaware of? Or something else? I'd really like to know because this bug was a bear to find, and I'd like to know how to better deal with it in the future. Thanks for taking the time! _________________ Windows 7 Ultimate x64 Windows XP Pro SP3 Ubuntu 11.04 via Virtual Box RS Enterprise 2012r1.1 Programming Tutorials & Free Projects: http://www.JasonTheAdams.com "Christianity has not been tried and found wanting; it has been found difficult and not tried." - G.K. Chesterton Top ktekinay Post subject: Re: NilObjectException masquerading as an OutOfBoundsExceptiPosted: Tue Oct 23, 2012 2:29 pm Joined: Mon Feb 05, 2007 5:21 pm Posts: 209 Location: New York, NY Without trying, I think the problem is in the form of your Try/Catch block. You wrote: Catch OutOfBoundsException I'm pretty sure what you've done is declared a variable "OutOfBoundsException" and told it to hold the reference to any type of exception. Try this instead: Catch err As OutOfBoundsException _________________ Kem Tekinay MacTechnologies Consulting http://www.mactechnologies.com/ Need to develop, test, and refine regular expressions? Try RegExRX. Top charonn0 Post subject: Re: NilObjectException masquerading as an OutOfBoundsExceptiPosted: Tue Oct 23, 2012 2:30 pm Joined: Mon Apr 02, 2007 2:08 am Posts: 933 Location: San Francisco, CA, USA Jason_Adams wrote:Try ... Catch OutOfBoundsException You must declare a type to only handle exceptions of a particular type. In the above code, you declare only that the exception is named OutOfBoundsException. So, your code is catching all exceptions regardless of type. This is what you would see if you broke into the debugger within the Catch block: To catch only certain exception types: Catch Error As ExceptionType Multiple Catch blocks may be used in the same Try block: Try Foo() '... Catch Error As NilObjectException Break Catch Error As OutOfBoundsException Break 'etc End Try _________________ Boredom Software Top Jason_Adams Post subject: Re: NilObjectException masquerading as an OutOfBoundsExceptiPosted: Tue Oct 23, 2012 6:08 pm Joined: Fri Nov 10, 2006 4:10 pm Posts: 1689 Location: Michigan, USA Ah.. That would do it. Thanks for taking the time to point it out, gentleman. I think this is one of those where I've been staring at a hunk of code for too long. Thanks again! _________________ Windows 7 Ultimate x64 Windows XP Pro SP3 Ubuntu 11.04 via Virtual Box RS Enterprise 2012r1.1 Programming Tutorials & Free Projects: http://www.JasonTheAdams.com "Christianity has not been tried and found wanting; it has been found difficult and not tried." - G.K. Chesterton Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 4 posts ]
-- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
