2 Replies Latest reply on Mar 12, 2003 8:30 PM by 807577

    HELP - NULLPOINTEREXCEPTION - Putting JTable into a JScrollPane

    807577
      Hi all,

      I am trying to add a JTable into a JScrollPane but it generates NullPointerException whenever the parent frame is set visible. Please help!!
      Thanks in advance!!!

      Here is my code (in a constructor of a class extending JPanel):
      ////////////
      String teststr[][] = {{"a", "b", "c"}, {"e", "f", "g"}, {"a", "b", "c"}, {"e", "f", "g"}};
      String colNames[] = {"colA", "colB", "colC"};
      JTable testTable = new JTable(teststr, colNames);
      testTable.setPreferredScrollableViewportSize(new Dimension(400, 70));

      JScrollPane testPane = new JScrollPane(testTable);

      add(testPane, BorderLayout.CENTER);
      ////////////

      It generates this error:
      java.lang.NullPointerException
      at java.awt.Container.addNotify(Container.java:2045)
      at javax.swing.JComponent.addNotify(JComponent.java:4283)
      at java.awt.Container.addNotify(Container.java:2045)
      at javax.swing.JComponent.addNotify(JComponent.java:4283)
      at java.awt.Container.addNotify(Container.java:2045)
      at javax.swing.JComponent.addNotify(JComponent.java:4283)
      at java.awt.Container.addNotify(Container.java:2045)
      at javax.swing.JComponent.addNotify(JComponent.java:4283)
      at java.awt.Container.addNotify(Container.java:2045)
      at javax.swing.JComponent.addNotify(JComponent.java:4283)
      at java.awt.Container.addNotify(Container.java:2045)
      at javax.swing.JComponent.addNotify(JComponent.java:4283)
      at java.awt.Container.addNotify(Container.java:2045)
      at javax.swing.JComponent.addNotify(JComponent.java:4283)
      at java.awt.Container.addNotify(Container.java:2045)
      at javax.swing.JComponent.addNotify(JComponent.java:4283)
      at java.awt.Container.addNotify(Container.java:2045)
      at javax.swing.JComponent.addNotify(JComponent.java:4283)
      at javax.swing.JRootPane.addNotify(JRootPane.java:673)
      at java.awt.Container.addNotify(Container.java:2045)
      at java.awt.Window.addNotify(Window.java:410)
      at java.awt.Frame.addNotify(Frame.java:479)
      at java.awt.Window.show(Window.java:448)
      at java.awt.Component.show(Component.java:1134)
      at java.awt.Component.setVisible(Component.java:1089)