Results 1 to 7 of 7
  1. #1
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default Still having trouble with static & non-static variables

    I keep ending up with the following error message and still don't understand how to fix it.

    Non-static variable primaryEnergyImage cannot be referenced from a static context.

    I highlighted the line of code in red that is giving me the problem. I'm trying to change an image based on which selection is made from the combo box.

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
     
    public class temp {
    
        JLabel primaryEnergyImage;
    
        public static void main(String[] args) {
            buildGUI();
        }
    
        public static void buildGUI() {
            JFrame deckEditorFrame = new JFrame("Deck Editor");
            deckEditorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            String[] energyTypes = {"energy1","energy2","energy3"};
    
            JComboBox primaryEnergyComboBox = new JComboBox(energyTypes);
            primaryEnergyComboBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                        primaryEnergyComboBox(e);
                      }
                    });
    
            JLabel primaryEnergyImage = new JLabel();
            primaryEnergyImage.setIcon(new ImageIcon("Images/Icons/energy1.jpg"));
    
            deckEditorFrame.add(primaryEnergyComboBox);
            deckEditorFrame.pack();
            deckEditorFrame.setVisible(true);
        }
    
        public static void primaryEnergyComboBox(ActionEvent e) {
                JComboBox cb = (JComboBox)e.getSource();
                String homelandType = (String)cb.getSelectedItem();
                if(homelandType.equalsIgnoreCase("energy2"))
    [COLOR="Red"][B]                primaryEnergyImage.setIcon(new ImageIcon("Images/Icons/energy2.jpg"));[/B][/COLOR]
            }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There should be no static anything there except for the main method. Period.

    Also in your main method, you should create your object and use it. That's how the static world and the non-static world should interact.

    And finally (I think), please follow Java naming conventions so that we all have an easier time reading and understanding your code. Class names should begin with a capital letter.

    Much luck.
    Last edited by Fubarable; 02-14-2010 at 02:47 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To boil things down to the bare essentials, what you are trying to do is this:

    Java Code:
    public class Temp2 {
      int primaryEnergyImage;
      
      public static void main(String[] args) {
        primaryEnergyComboBox();
      }
      
      public static void primaryEnergyComboBox() {
        primaryEnergyImage = 3;
      }
    }
    and this won't work because primaryEnergyImage is not a static variable, and it is trying to be used in a static context.

    One solution would be to make all static:
    Java Code:
    public class Temp2 {
      static int primaryEnergyImage;
      
      public static void main(String[] args) {
        primaryEnergyComboBox();
      }
      
      public static void primaryEnergyComboBox() {
        primaryEnergyImage = 3;
      }
    }
    and while this would work, it would usually be wrong as you lose all the benefits of OOPs programming. Better would be to make things more OOPS like by getting rid of all statics:

    Java Code:
    public class Temp2 {
      int primaryEnergyImage;
      
      public static void main(String[] args) {
        Temp2 temp2 = new Temp2();
        temp2.primaryEnergyComboBox();
      }
      
      public void primaryEnergyComboBox() {
        primaryEnergyImage = 3;
      }
    }

  4. #4
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    OK, I'm still getting something wrong here. I tried to make the changes you said and it just spits out a different error.

    Java Code:
    package testing;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Temp extends JPanel  {
        JLabel label;
        
        public static void main(String[] args) {
        Temp temp = new Temp();
        temp.BuildGUI();
        }
    
        public void BuildGUI() {
            JFrame deckEditorFrame = new JFrame("Deck Editor");
            deckEditorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            deckEditorFrame.setLayout(null);
            JPanel panel1 = new JPanel();
            JPanel panel2 = new JPanel();
            deckEditorFrame.add(panel1);
            panel1.setBounds(0, 0, 100, 50);
            deckEditorFrame.add(panel2);
            panel2.setBounds(0, 50, 100, 100);
    
            String[] energyTypes = {"energy1","energy2","energy3"};
    
            JComboBox comboBox = new JComboBox(energyTypes);
            comboBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                        PrimaryEnergyComboBox(e);
                      }
                    });
    
            JLabel label2 = new JLabel("test");
            label2.setBounds(10, 10, 100, 100);
            panel1.add(label2);
            panel2.add(comboBox);
            deckEditorFrame.setBounds(0, 0, 100, 150);
    
            deckEditorFrame.setVisible(true);
        }
    
        public void PrimaryEnergyComboBox(ActionEvent e) {
                JComboBox cb = (JComboBox)e.getSource();
                String homelandType = (String)cb.getSelectedItem();
                System.out.println(homelandType);
                if(homelandType.equalsIgnoreCase("energy2"))
                    label.setText("Energy 2");
            }
    }
    The program still runs, but it produces the following error when I select the 2nd item from the combo box.

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at spiritwars.Temp.PrimaryEnergyComboBox(Temp.java:53)
            at spiritwars.Temp$1.actionPerformed(Temp.java:35)
            at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1240)
            at javax.swing.JComboBox.setSelectedItem(JComboBox.java:567)
            at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:603)
            at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:816)
            at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
            at java.awt.Component.processMouseEvent(Component.java:6263)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
            at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:480)
            at java.awt.Component.processEvent(Component.java:6028)
            at java.awt.Container.processEvent(Container.java:2041)
            at java.awt.Component.dispatchEventImpl(Component.java:4630)
            at java.awt.Container.dispatchEventImpl(Container.java:2099)
            at java.awt.Component.dispatchEvent(Component.java:4460)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
            at java.awt.Container.dispatchEventImpl(Container.java:2085)
            at java.awt.Window.dispatchEventImpl(Window.java:2475)
            at java.awt.Component.dispatchEvent(Component.java:4460)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Last edited by Psyclone; 02-14-2010 at 04:42 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Psyclone View Post
    OK, I'm still getting something wrong here. I tried to make the changes you said and it just spits out a different error.
    If it were me, then I'd debug the error.

    This is a NPE and has nothing to do with static and all to do with using an object that is null. The error will tell you what line is causing it, then you must do some investigation to find out why an object being dereferenced on that line is null.

    You may want to put GUI programming to the side a little bit and work through a Java textbook to learn about static, classes, and debugging techniques. Gaining this foundation would make your GUI coding much easier.

    Best of luck.
    Last edited by Fubarable; 02-14-2010 at 04:55 AM.

  6. #6
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    I went back and read the section on Objects and Classes again. I now see what I was doing wrong.

    I was declaring the variables at the start of the class, but then I was instantiating a new instance variable later in the code. I'm not sure I said that correctly. But here's what I mean...

    Java Code:
    // In main class
    JLabel label;
    
    // In method
    JLabel label = new JLabel();
    When I should have been doing...

    Java Code:
    // in main class
    JLabel label;
    
    // in method
    label = new JLabel();
    And if the object never changes, I don't need to declare it in main class and can just use...

    Java Code:
    JLabel label = new JLabel();
    Thanks again Fubarable!

  7. #7
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    By the way... how do I close my threads as SOLVED?

    EDIT: Nevermind. :)

Similar Threads

  1. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 06:48 PM
  2. static are instance variables
    By gabri in forum Advanced Java
    Replies: 12
    Last Post: 09-30-2008, 07:30 PM
  3. Using Static Variables
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:08 AM
  4. significance of static variables and methods
    By imran_khan in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 10:52 AM
  5. Help with static variables
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 06:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •