10 Replies Latest reply on Jan 15, 2008 1:19 AM by camickr

    Exception when adding TableHeader to ContentPane

    843806
      When I add the TableHeader of a JTable to the NORTH contraint of the ContentPane in BorderLayout, like this:
      jContentPane.add(PasswordTable.getTableHeader(),BorderLayout.NORTH);
      Everything works fine.

      Now I'm trying to add a button to the SOUTH contraint of the same ContentPane but I'm getting the following exception:
      Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
           at java.awt.Container.addNotify(Unknown Source)
           at javax.swing.JComponent.addNotify(Unknown Source)
           at java.awt.Container.addNotify(Unknown Source)
           at javax.swing.JComponent.addNotify(Unknown Source)
           at java.awt.Container.addNotify(Unknown Source)
           at javax.swing.JComponent.addNotify(Unknown Source)
           at javax.swing.JRootPane.addNotify(Unknown Source)
           at java.awt.Container.addNotify(Unknown Source)
           at java.awt.Window.addNotify(Unknown Source)
           at java.awt.Frame.addNotify(Unknown Source)
           at java.awt.Window.show(Unknown Source)
           at java.awt.Component.show(Unknown Source)
           at java.awt.Component.setVisible(Unknown Source)
           at org.eclipse.ve.internal.java.vce.launcher.remotevm.JFCLauncher.launchWindow(JFCLauncher.java:138)
           at org.eclipse.ve.internal.java.vce.launcher.remotevm.JFCLauncher$1.run(JFCLauncher.java:109)
           at java.awt.event.InvocationEvent.dispatch(Unknown Source)
           at java.awt.EventQueue.dispatchEvent(Unknown Source)
           at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
           at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
           at java.awt.EventDispatchThread.run(Unknown Source)
      When I comment out the line where I add the TableHeader to the ContentPane (see above) it also works. So it seems to work separately, but not both at the same time.

      Can someone help me?
        • 1. Re: Exception when adding TableHeader to ContentPane
          camickr
          Then you are doing something wrong. I'm not sure how you expect us to help you based on one line of code?

          If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)", that demonstrates the incorrect behaviour.
          http://homepage1.nifty.com/algafield/sscce.html

          Don't forget to use the "Code Formatting Tags", so the posted code retains its original formatting.
          http://forum.java.sun.com/help.jspa?sec=formatting
          • 2. Re: Exception when adding TableHeader to ContentPane
            843806
            This is the code of the Frame mostly generated by Eclipse Visual Editor:
            import java.awt.BorderLayout;
            
            import javax.swing.JButton;
            import javax.swing.JFrame;
            import javax.swing.JPanel;
            import javax.swing.JScrollPane;
            import javax.swing.JTable;
            
            public class MainFrame extends JFrame {
            
                 private static final long serialVersionUID = 1L;
            
                 private JPanel jContentPane = null;
            
                 private JScrollPane jScrollPane = null;
            
                 private JTable PasswordTable = null;
            
                 private JButton jButton = null;
            
                 /**
                  * This is the default constructor
                  */
                 public MainFrame() {
                      super();
                      initialize();
                 }
            
                 public MainFrame(String string) {
                      super(string);
                      initialize();
                 }
            
                 /**
                  * This method initializes this
                  * 
                  * @return void
                  */
                 private void initialize() {
                      this.setSize(300, 200);
                      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      this.setContentPane(getJContentPane());
                      this.setTitle("JFrame");
                 }
            
                 /**
                  * This method initializes jContentPane
                  * 
                  * @return javax.swing.JPanel
                  */
                 public JPanel getJContentPane() {
                      if (jContentPane == null) {
                           jContentPane = new JPanel();
                           jContentPane.setLayout(new BorderLayout());
                           jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
                           jContentPane.add(getJButton(), BorderLayout.SOUTH);
                      }
                      return jContentPane;
                 }
            
                 /**
                  * This method initializes jScrollPane     
                  *      
                  * @return javax.swing.JScrollPane     
                  */
                 public JScrollPane getJScrollPane() {
                      if (jScrollPane == null) {
                           jScrollPane = new JScrollPane();
                           jScrollPane.setViewportView(getPasswordTable());
                      }
                      return jScrollPane;
                 }
            
                 /**
                  * This method initializes PasswordTable     
                  *      
                  * @return javax.swing.JTable     
                  */
                 public JTable getPasswordTable() {
                      if (PasswordTable == null) {
                           PasswordTable = new JTable();
                           jContentPane.add(PasswordTable.getTableHeader(),BorderLayout.NORTH);
                      }
                      return PasswordTable;
                 }
            
                 /**
                  * This method initializes jButton     
                  *      
                  * @return javax.swing.JButton     
                  */
                 private JButton getJButton() {
                      if (jButton == null) {
                           jButton = new JButton("Add Row");
                      }
                      return jButton;
                 }
            
            }
            This is the program to test it with:
            public class Program {
                 public static void main(String[] args) {
                      MainFrame frm = new MainFrame("Testing ...");
                      frm.pack();
                      frm.setVisible(true);
                 }
            }
            I hope this is more helpful.

            Edited by: Aries-Belgium on Jan 14, 2008 11:16 PM
            • 3. Re: Exception when adding TableHeader to ContentPane
              843806
              I think that the problem may be because you are adding your jtable to a jscrollpane and adding its header to a JPanel borderlayout.NORTH. When you add the jtable to the jscrollpane, I think that its header is already displayed and set up at the top of the scrollpane. Somehow trying to add the header again to the jpanel NORTH messes things up. I'd chose one of the other.
              • 4. Re: Exception when adding TableHeader to ContentPane
                843806
                For instance, in this simplified program the commented line, which is unnecessary, causes a NPE when the JFrame.pack() is called, similar to your program.:
                import java.awt.BorderLayout;
                import java.awt.Dimension;
                import javax.swing.JFrame;
                import javax.swing.JPanel;
                import javax.swing.JScrollPane;
                import javax.swing.JTable;
                
                public class MainFrame2 extends JPanel
                {
                    MainFrame2()
                    {
                        setPreferredSize(new Dimension(400, 400));
                        setLayout(new BorderLayout());
                        JTable table = new JTable();
                        JScrollPane scrollPane = new JScrollPane(table);
                        add(scrollPane, BorderLayout.CENTER);
                        //add(table.getTableHeader(), BorderLayout.NORTH); //*** this causes NPE
                    }
                    
                    private static void createAndShowGUI()
                    {
                        JFrame frame = new JFrame("MainFrame2 Application");
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.getContentPane().add(new MainFrame2());
                        frame.pack();
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);
                    }
                
                    public static void main(String[] args)
                    {
                        javax.swing.SwingUtilities.invokeLater(new Runnable()
                        {
                            public void run()
                            {
                                createAndShowGUI();
                            }
                        });
                    }
                }
                So again the answer is: either add your jtable to a jscrollpane and be done with it, or add the table to a jpanel borderlayout.center and its header to the jpanel borderlayout.north, but don't try to do both.

                Edited by: Encephalopathic on Jan 14, 2008 3:08 PM
                • 5. Re: Exception when adding TableHeader to ContentPane
                  843806
                  But the weird thing is that it goes well if you don't add anything else to the contentpane. When I comment out this line in my previous code it works:
                  jContentPane.add(getJButton(), BorderLayout.SOUTH);
                  • 6. Re: Exception when adding TableHeader to ContentPane
                    843806
                    Aries-Belgium wrote:
                    But the weird thing is that it goes well if you don't add anything else to the contentpane. When I comment out this line in my previous code it works:
                    jContentPane.add(getJButton(), BorderLayout.SOUTH);
                    You are right, and I can't explain this last bit, but regardless, I think that in your current situation where you've added the jtable to a jscrollpane, adding the table's header to the jpanel borderlayout.north is unnecessary, and (as shown in my small SSCCE) dangerous. So just don't do this.
                    • 7. Re: Exception when adding TableHeader to ContentPane
                      843806
                      Okay, I solved it by adding an extra JPanel with BorderLayout which contains the JScrollPane (CENTER) and the TableHeader (NORTH). Now it works. Thanks anyway!
                      • 8. Re: Exception when adding TableHeader to ContentPane
                        843806
                        Aries-Belgium wrote:
                        Okay, I solved it by adding an extra JPanel with BorderLayout which contains the JScrollPane (CENTER) and the TableHeader (NORTH). Now it works. Thanks anyway!
                        No. I advise you not to do this as I think that it is wrong. If you are adding a JTable to a JScrollPane and then displaying this JScrollPane, the table's header will be displayed properly. You do not need to try to redisplay them in the north position of some jpanel. I believe that this is redundant and possibly dangerous.

                        I am not a JTable expert, not by any means, but per my tests today, I think I'm right here. If someone knows otherwise, please let me know.
                        • 9. Re: Exception when adding TableHeader to ContentPane
                          843806
                          Oh yeah, you're right. The TableHeader automatically sticks to the top of the JScrollPane. Great!
                          • 10. Re: Exception when adding TableHeader to ContentPane
                            camickr
                            Next time I might suggest you read the API first. Both situations described in the API.