views:

54

answers:

2

In the snippet of code below, I'm using a class to get a reference to an instance of BookList. This class has a getBook() method which returns an instance of a Book from a String representing an isbn code. The instruction is run in the doGet() method of a HttpServlet.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
Book book = GlobalVar.bookList.getBook( request.getParameter("isbn") );
//
}

What potential problems do you see in using shorthand notation in this context ?

P.S: The Global class is going to be replaced with a ServletContext.

A: 

The request may not have a parameter "isbn".

Thomas Lötzer
Indeed. This will cause a NullPointerException as Roger has pointed out if the value of book isn't checked. I already have the habit of checking values that are obtained by a method so that isn't a problem. I just wish there was a checklist out there of good practises to apply.
James P.
+3  A: 

I would say NullPointerExceptions. What happends if the client, doing the HTTP GET request, has no paramated named "isbn". Is the getBook implemented in way that it supports null as an argument?

Schildmeijer
getBook() returns the get() from a HashMap. Javadoc isn't clear what happens when you do a get() with a null but testing shows that it has no consequence. My reflex would be to test if the instance of book is null afterwards. Ideally, I should develop a consistant style that will avoid Exceptions.
James P.
HashMap apparently accepts a null key too. Interesting.
James P.