Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Wed Jul 30, 2014 4:21 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: NilObjectException was not handled error in console app
PostPosted: Tue Mar 31, 2009 11:59 pm 
Offline

Joined: Sat Nov 12, 2005 4:19 pm
Posts: 9
I am using REALBasic 2009 Release 1 on Mac OS X 10.5.6.

I have been searching through these forums, checking the Language Reference, and included example files but I cannot figure out how to handle an error that I am getting.

I am writing a Console Application for Windows that I am passing one or two file pathnames as arguments on the command line. I check the number and contents of the args() being read in with a boolean function. This function works properly and then I use the following code to read FolderItem of args(1).

Code snippit in the Run Event Handler.
...
if not CheckArguments( args ) then return -1

sourcef = GetFolderItem(args(1))

if sourcef <> nil then
' Process files here
...
else
mess = args(1) + " is not a valid SDF file"
stdout.WriteLine(mess)
end if
return 0

Exception err as NilObjectException
StdErr.WriteLine("Invalid file. Bye!")


Code in the UnhandledException Event Handler
if error IsA NilObjectException then
StdErr.WriteLine("Bad file")
else
StdErr.WriteLine("Very Bad file")
end if


When a valid file path is passed, the code runs fine and the program executes without any errors.

When a partial or invalid file path is passed,the console outputs the following:

C:\tempinstalls\sdtest>sdfieldc c:\tem

Bad file
Exception Message:
Exception Error Number: 0
An exception of class NilObjectException was not handled. The application must
shut down.

And Windows also throws up the Program crash dialog box for reporting the error to Microsoft

So it looks like the UnhandledExeception code is being run, and not the Exception block at the end of the Run event. Also the else statements are not executed as I would have expected a different message.

How am I supposed to handle the exception if the methods above are not catching the error? Am I missing something? I have also tried to use Try...Catch...End Try blocks as well with the same result.

Regards,

Robin


Top
 Profile  
 
 Post subject: Re: NilObjectException was not handled error in console app
PostPosted: Wed Apr 01, 2009 1:13 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
It doesn't look like sourcef is the problem. It's never reaching the test. Put some more stdout.writeline statements in to narrow it down.

Tim


Top
 Profile  
 
 Post subject: Re: NilObjectException was not handled error in console app
PostPosted: Wed Apr 01, 2009 6:03 am 
Offline
User avatar

Joined: Thu Mar 05, 2009 1:03 pm
Posts: 1786
Location: USA
You might want to try stepping through your code, beginning at your UnhandledException event handler.

if error IsA NilObjectException then
StdErr.WriteLine("Bad file") //PUT BREAKPOINT HERE
else
StdErr.WriteLine("Very Bad file")
end if


Once the breakpoint stops you, step through your code and see if you can find the object causing your NilObjectException. The Properties panel in the RB debugger might be helpful in spotting the Nil object... ;)

_________________
With great power comes great responsibility.
Learn something new every day, and the rest will take care of itself.
Life is a journey, not a destination. Enjoy the trip! :D


Top
 Profile  
 
 Post subject: Re: NilObjectException was not handled error in console app
PostPosted: Wed Apr 01, 2009 2:44 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Um, if you're using the debugger and have BreakOnExceptions turned on, it will stop on the actual line of code that throws the error. No need to muddy the waters with breakpoints in app.UnhandledException.

When you run a console app in the debugger, you can set CommandLineArgs in the app properties to simulate passing arguments on the command line. Click on the app object in the Project tab.

Tim


Top
 Profile  
 
 Post subject: Re: NilObjectException was not handled error in console app
PostPosted: Wed Apr 01, 2009 6:56 pm 
Offline

Joined: Sat Nov 12, 2005 4:19 pm
Posts: 9
I seem to have found my error. I had to perform a further check within my CheckArguments function to verify the path and filename of the incoming argument (args(1)). All works as I expect now.

I am still curious about why I got the unhandled error when I had handled the error with the UnhandledException method.

Does anyone have any examples from Console apps that require command line arguments that you may have written?

Regards,

Robin


Top
 Profile  
 
 Post subject: Re: NilObjectException was not handled error in console app
PostPosted: Wed Apr 01, 2009 7:27 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Putting code in app.UnhandledException does not "handle" the exception. That's your last chance stop to get information before your program crashes. The only way to "handle" the exception in UnhandledException is to execute a Quit command.
if error IsA NilObjectException then
StdErr.WriteLine("Bad file")
Quit
else
StdErr.WriteLine("Very Bad file")
end if


Top
 Profile  
 
 Post subject: Re: NilObjectException was not handled error in console app
PostPosted: Wed Apr 01, 2009 8:06 pm 
Offline

Joined: Sat Nov 12, 2005 4:19 pm
Posts: 9
timhare wrote:
Putting code in app.UnhandledException does not "handle" the exception. That's your last chance stop to get information before your program crashes. The only way to "handle" the exception in UnhandledException is to execute a Quit command.
if error IsA NilObjectException then
StdErr.WriteLine("Bad file")
Quit
else
StdErr.WriteLine("Very Bad file")
end if


Thanks Tim.

I believe I had tried putting a Quit statement in there at one point, but don't remember now. Something to remember for the future.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group