aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Database status - open or closed? NullPointerExceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Database status - open or closed? NullPointerExceptions" Watch "Database status - open or closed? NullPointerExceptions" New topic
Author

Database status - open or closed? NullPointerExceptions

Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Hi,
As mentioned elsewhere on this forum, calling close() on an already closed database caused a NullPointerException. Indeed a NullPointerException is likely to occur if you tried to call most of the Data classes methods after the database is closed as the close() method sets the db field on the Data class to null. This is contrary to the javadoc for the Data class, which specifies that most methods of the Data class throw a suncertify.db.DatabaseException if the database cannot be accessed.
My questions are:
1) Has anyone implemented a isOpen() method to check that the database is open before performing actions on it? If I do implement the isOpen() method should I fix the methods of the Data class to throw the DatabaseException as specified by the API or should I change the API to advised that a NullPointerException may be thrown?
2) Would we be penalised for implementing the isOpen()?
3) Would we be penalised for correcting the bugs in the Data class?
Cheers,
Steve
Enrico Mannarino
Ranch Hand

Joined: Dec 14, 2001
Posts: 133
Hi Steve,
This may not be the answer your looking for, but I just wanted to share with you my approach.
In the Instructions.htm it is said: "At present, a basic data storage and retrieval system (the "database") has been implemented and shown to work by the undergraduate."
So from my point of view, I haven't implemented any thing else then the 3 methods required.
That means: no check if the db is open or neither do I call close().
If I'm not mistaken, that's automatically handled by the finalize method when closing down.
By the way, it works real fine without a close().
Enrico
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Thanks,
I was just concerned that the api was saying that a DatabaseException will be thrown if the database cannot be accessed - but this is clearly wrong! Most people will be coding to catch this DatabaseException so will not catch the NullPointerException where they wish to handle database errors. The handling of the NPE will happen much higher up the chain than they expect.
It would be fairly straight forward to add a flag to the class to indicate whether the database is open or closed.
What do other people think about this and my questions in the original post?
Cheers,
Steve
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Also,
If the close() is not meant to be called by clients then it should be private or protected - not public!
The close() method has the following javadoc comment:
"...Any attempt to access the database after this results in a IOException."
This statement is wrong!
Cheers,
Steve
Enrico Mannarino
Ranch Hand

Joined: Dec 14, 2001
Posts: 133
Hi Steve,
You’re right, it should be private!
But I shouldn’t bother too much with the code in the db package, it’s not perfect and honestly I don’t care.
Enrico
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Hi,
You're right - maybe I'm worrying too much!
Thanks again,
Steve
[ February 02, 2002: Message edited by: Steve Granton ]
Enrico Mannarino
Ranch Hand

Joined: Dec 14, 2001
Posts: 133
You, I took it seriously as well at the beginning. I couldn’t believe that we should update the db.db! But now I understand that’s the way it is, so I’m just going to finish this assignment and move on.
But meanwhile, have fun and try to learn something, as Mark says.
If you’re interested about my first thoughts look at:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=25&t=001713
and
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=25&t=001720
Enrico
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database status - open or closed? NullPointerExceptions