Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Non-static variable error now a package not found error

    Hello. I wrote a program to display a simple gui. In compiling, I got a "non-static variable cannot be referenced from a static context" error. I understand that error was because I needed to create an object of the class so my non-static variable can be properly referenced from the Main method.

    Having made what I thought were the fixes, I now get another error message telling me the object I just created causes a "package does not exist" error.

    I think the problem is that I still have not properly referenced the non-static variable. Can anyone tell me how to fix this?

    The code:
    Java Code:
    // A program to create a GUI displaying a message
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class GUI3 {
    	public class MyPanel extends JPanel {
    	
    		private String currentText = "Hello Butt-wipes!";
    		private MessageDisplay displayPanel;
    
    		// creating the display panel
    		private class MessageDisplay extends JPanel {	
    			public void paintComponent(Graphics g) {
    				super.paintComponent(g);
    				g.drawString(currentText, 20, 30);
    			}
    		}
    
    		// creating the event listener
    		private class Handler implements ActionListener {
    			public void actionPerformed(ActionEvent e) {
    				if (currentText.equals("Hello Butt-wipes!"))
    					currentText = "Smell you later!";
    				else
    					currentText = "Hello Butt-wipes!";
    				// each time an action event occurs, the 					                         // display panel is re-painted
    				displayPanel.repaint();
    			}
    		}
    	
    		// The constructor creates the components that will be contained inside this 
                             // panel, then adds the componets to the panel
    		public MyPanel() {
    			displayPanel = new MessageDisplay();
    		
    			JButton changeBut = new JButton("Change Message");
    			Handler listen = new Handler();
    			changeBut.addActionListener(listen);
    	
    			// set the Layout Manager for the panel
    			setLayout(new BorderLayout());
    			add(displayPanel, BorderLayout.CENTER);
    			add(changeBut, BorderLayout.SOUTH);
    		}
    	}
    
    	public static void main(String[] args) {
    		GUI3 gt = new GUI3();       // trying to fix the "non-static variable cannot 
                                                                   // be referenced..." error, I create a GUI 3 object
    		JFrame window = new JFrame("GUI Test");
    		gt.MyPanel content = new gt.MyPanel();   // error message at this line
    		window.setContentPane(content);
    		window.setSize(250, 100);
    		window.setLocation(100, 100);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setVisible(true);
    	}
    }
    The error message tells me that the line
    gt.MyPanel content = new gt.MyPanel();
    has two errors in it: that package gt does not exist

    Interestingly, I can make this compile and run if I make the GUI3 class a separate file, and the Main method a separate file. When I do that, there is no "non-static variable cannot be referenced from a static context" error. So there are still parts of the making objects work together concept that I am not understanding.

    Any help is appreciated.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,923
    Rep Power
    4

    Default Re: Non-static variable error now a package not found error

    Syntax for line 53 is incorrect. Try this:

    Java Code:
    MyPanel content = gt.new MyPanel();
    Keep in mind that content is a local variable in main so an instance is not necessary to access it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Non-static variable error now a package not found error

    Hello. Thank you for your helpful reply. Your suggestion works - the code compiles and runs as it should.

    But now I have a new problem: I thought gt.MyPanel was the proper way to address the class MyPanel that is the gt instance of the GUI3 class.

    Now I have to go read some more about the dot operator.

    But thanks for your help.

Similar Threads

  1. Replies: 2
    Last Post: 04-10-2013, 12:46 AM
  2. Replies: 8
    Last Post: 11-22-2012, 07:16 AM
  3. Non static variable error
    By nonybrighto in forum New To Java
    Replies: 5
    Last Post: 10-03-2012, 12:51 AM
  4. non static variable from static context error
    By chopficaro in forum New To Java
    Replies: 2
    Last Post: 06-04-2012, 01:44 PM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 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
  •