Java GUI Static component, NullPointerException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AlarV
    New Member
    • Dec 2008
    • 37

    Java GUI Static component, NullPointerException

    Hey everyone, I created a GUI project, where I'm using a JTree. When it's created everything is ok, but in my run() method when I call it i get a nullPointerExce ption. Here is the code.
    Code:
    treePanel = new javax.swing.JPanel();
    DefaultMutableTreeNode top =
            new DefaultMutableTreeNode("blah");
            mytree = new javax.swing.JTree(top);
    javax.swing.GroupLayout treePanelLayout = new javax.swing.GroupLayout(treePanel);
            treePanel.setLayout(treePanelLayout);
            treePanelLayout.setHorizontalGroup(
                treePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, treePanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
            );
            treePanelLayout.setVerticalGroup(
                treePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, treePanelLayout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            );
    of course I add stuff to the tree, but i'm not showing it here cause it's part of the project and personal data.

    and in run() method

    Code:
    mPanel= new JPanel();
    mPanel.setLayout(new BoxLayout(mPanel,BoxLayout.X_AXIS));
    mPanel.add(treePanel);
    and the declaration of my variables
    Code:
        // Variables declaration - do not modify
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTree mytree;
        private javax.swing.JPanel treePanel;
        // End of variables declaration
    Last edited by AlarV; Jan 7 '11, 10:11 AM. Reason: additional code
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    when you get the exception does it tell you which line in the code it is on?

    Comment

    • AlarV
      New Member
      • Dec 2008
      • 37

      #3
      well yeah it says:

      Exception in thread "AWT-EventQueue-0" java.lang.NullP ointerException
      at charybdis.Main$ 2.run(Main.java :260)
      and it is this line
      Code:
      mPanel.add(treePanel);

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        it looks like treePanel does not reference a JPanel object hence you get a NullPointerExce ption
        have you a
        Code:
        treePanel=new JPanel();
        in your code ? is it before you call the run() method ?

        Comment

        • AlarV
          New Member
          • Dec 2008
          • 37

          #5
          yes it's on line 1 in the first code tag!

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            looks like you have not executed that code before you call the run() method
            as a quick test try in the declarations
            Code:
            private javax.swing.JPanel treePanel = new javax.swing.JPanel();

            Comment

            • AlarV
              New Member
              • Dec 2008
              • 37

              #7
              well the main program has this method
              Code:
              public Main() {
                      initComponents();
                  }
              and then in initComponents it does all the work I posted. then there is this

              Code:
              public static void main(String args[]) {
                      java.awt.EventQueue.invokeLater(new Runnable() {
                          
                          public void run() {
                              try {
              and that's where I add the panel to the main frame.

              I can't try what you suggested because the variable declaration part is locked and I can't add stuff!
              If the code isn't executed the problem is why, as the initComponents runs even before the main() method..

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                when do you call method Main() ?
                try calling initComponents( ); in main() before you call run() ?

                Comment

                • AlarV
                  New Member
                  • Dec 2008
                  • 37

                  #9
                  I can't cause of the classic bull#*$@#* of java.

                  initComponents isn't static and I can't call it in static main..

                  I tried what you suggested before though.
                  treePanel=new JPanel(); before run() and it worked!
                  this means that initComponents doesn't run, so I'll have to check this out.

                  If you have any ideas why initComponents is not working you are more than welcome to tell them. I'm stuck at this for like 2 days..

                  Comment

                  • horace1
                    Recognized Expert Top Contributor
                    • Nov 2006
                    • 1510

                    #10
                    it is difficult not being able to see all the code

                    is your main() method in a class call Main?
                    if so could you do
                    Code:
                    public static void main(String args[]) {
                       Main x = new Main();
                    to create an instance of Main which would call initComponents( );

                    Comment

                    • AlarV
                      New Member
                      • Dec 2008
                      • 37

                      #11
                      sorry for the delay.. you are right! that was the problem I thought that as it was in the class it ran alone, but you have to call it in main.

                      Thanx!

                      Comment

                      Working...