Results 1 to 7 of 7
- 02-14-2010, 01:42 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
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] } }
-
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 01:47 AM.
-
To boil things down to the bare essentials, what you are trying to do is this:
and this won't work because primaryEnergyImage is not a static variable, and it is trying to be used in a static context.Java Code:public class Temp2 { int primaryEnergyImage; public static void main(String[] args) { primaryEnergyComboBox(); } public static void primaryEnergyComboBox() { primaryEnergyImage = 3; } }
One solution would be to make all static:
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 { static int primaryEnergyImage; public static void main(String[] args) { primaryEnergyComboBox(); } public static void primaryEnergyComboBox() { primaryEnergyImage = 3; } }
Java Code:public class Temp2 { int primaryEnergyImage; public static void main(String[] args) { Temp2 temp2 = new Temp2(); temp2.primaryEnergyComboBox(); } public void primaryEnergyComboBox() { primaryEnergyImage = 3; } }
- 02-14-2010, 03:40 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
OK, I'm still getting something wrong here. I tried to make the changes you said and it just spits out a different error.
The program still runs, but it produces the following error when I select the 2nd item from the combo box.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"); } }
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 03:42 AM.
-
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 03:55 AM.
- 02-15-2010, 04:30 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
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...
When I should have been doing...Java Code:// In main class JLabel label; // In method JLabel 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:// in main class JLabel label; // in method label = new JLabel();
Thanks again Fubarable!Java Code:JLabel label = new JLabel();
- 02-15-2010, 04:31 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
Similar Threads
-
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM -
static are instance variables
By gabri in forum Advanced JavaReplies: 12Last Post: 09-30-2008, 06:30 PM -
Using Static Variables
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:08 PM -
significance of static variables and methods
By imran_khan in forum New To JavaReplies: 4Last Post: 08-02-2007, 09:52 AM -
Help with static variables
By bbq in forum Advanced JavaReplies: 1Last Post: 06-28-2007, 05:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks