In Java, how can I tell I have a valid view?

In Java, when I do:

…

Database db = session.getDatabase(“myServer”, “myDb.nsf”);

I can always test

If ( db.isOpen() ){ blah … blah…}

Now if I say:

View v = db.getView(“myView”)

How can I ascertain that the view is really there?

if ( v != null ){ blah … blah…}

is this as good as I can get?

Marcus

Subject: In Java, how can I tell I have a valid view?

Hi Marcus.

I want to make sure someone else doesn’t lead you into very poor programming practices. Just letting Try/Catch catch everything is almost as bad as the old days of the GOTO command, and many respectable Java programmers would consider it lazy.

Anyhow…

Unfortunately, there’s no perfect way of checking for the true existance of a View in Lotus Notes using java. You can get a Vector of all views in a database by using the .getViews() method of the Database object, and then enumerate each view, and comparing its name to the views name you want to find, but this, I think, is overkill.

The other problem with this method is that a view might exist that you just don’t have the rights to access. This technique of searching for it wouldn’t help, because the getViews method won’t return a view that you do not have access to.

So, yes, your method is the best way. Always try to use your own code to catch an exception, as opposed to using a Java try/catch block. Try/Catch is very useful, but don’t rely on it to just be the catch-all. Exceptions are very expensive creatures to create, and because a NotesException extends the Exception class, they’re even more expensive objects. If you know something can obviously happen, check for it yourself, and attempt to gracefully handle the issue.

Unfortunately, there are many Notes Developers who haven’t learned proper coding practices and techniques, because Notes makes it way too easy to write sloppy code that still “works”. I might be able to use a screwdriver to smack a nail into a wall, but its only the right way if I don’t have a hammer.

  • Steve

Subject: In Java, how can I tell I have a valid view?

Try to open a document and use the Catch code to do whatever you want to do if the view isn’t there.