|
Replies:
1
-
Last Post:
Jul 10, 2008 5:10 AM
by: tjquinn
|
|
|
|
|
|
|
Null pointer execption
Posted:
Jul 10, 2008 2:46 AM
|
|
|
Hi
I am using Glassfish V2 with Netbeans 6.1 on Windows XP.
I have a simple application with a JFrame and main class that simply updates a label on the form with a string that comes from a EJB.
When I call the EJB from the main class it works, but when i call the ejb form the JFrame (MainForm) class I get a null pointer exception.
What am I doing wrong?
Here the is main class:
package enterpriseappee5;
/** * * @author Louw */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainForm().setVisible(true); } }); } }
Here is the JFrame (MainForm) class:
package enterpriseappee5;
import ejb.SessionRemote; import javax.ejb.EJB;
/** * * @author Louw */ public class MainForm extends javax.swing.JFrame { @EJB private static SessionRemote sessionBean;
/** Creates new form MainForm */ public MainForm() { initComponents(); }
/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {
jButton1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });
jLabel1.setText("jLabel1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1) .addContainerGap(279, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jLabel1)) .addContainerGap(266, Short.MAX_VALUE)) );
pack(); }// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jLabel1.setText(sessionBean.getResult()); }
// Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; // End of variables declaration
}
|
|
|
|
|
|
|
Re: Null pointer execption
Posted:
Jul 10, 2008 5:10 AM
in response to: hendrel
|
|
|
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
|
|
|
|
|