users@glassfish.java.net

Re: Null pointer execption

From: <glassfish_at_javadesktop.org>
Date: Thu, 10 Jul 2008 05:10:43 PDT

This problem comes up from time to time.

The Java EE platform spec says that injection will occur only on "managed classes" and, in an app client, only the main class is managed and only static elements are subject to injection.

My standard advice to developers is to

1. Declare private injected static variables on the main class.
2. Add public or package-visible static methods on the main class that return the value of the injected variable.
3. Use these methods from other classes in the client.

In my opinion, it's even better to create a utility class with business-oriented methods such as getCustomer. The other classes in the client would use those methods when they need to access the back-end beans. Internally, the implementation of getCustomer would use the main class getCustomerEJB method (or named however you want) to refer to the bean. It is a little more coding but it insulates nearly all of the client logic from which specific back-end beans and methods are used. This way, if the API of the bean changes you might be able to change only the utility class getCustomer method which actually depends on the bean's API. The logic in the client that invokes getCustomer could remain unchanged. But of course this last part is a matter of style and it's not necessary to implement your client this way to get injection to work.

- Tim
[Message sent by forum member 'tjquinn' (tjquinn)]

http://forums.java.net/jive/thread.jspa?messageID=285642