Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Cannot make a static reference to the non-static method

  1. #1
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default Cannot make a static reference to the non-static method

    So I have this Japplet which represents a bunch of Jpanels. I first say to the Japplet that it has to display the first Jpanel. In this JPanel is a button which has an actionlistener connected to it. When you click on it the method ChangePanel is called from the container (the Japplet) which changes the Jpanel that is displayed (from present to future)

    Here's the code:

    Java Code:
    import java.awt.Image;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    
    
    public class menu extends JApplet {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	private JPanel[] GamePanels = {new FirstPanel(), new SecondPanel()};
    	Image BackgroundImage;
    	
    	public static void ChangePanel(int present,int future)
    	{
    		remove(GamePanels[present]); [COLOR="red"]Point A[/COLOR]
    		add(GamePanels[future]);  [COLOR="red"]Point B[/COLOR]
    	}
    	
    	public void init()
    	{
    		this.setSize(200,300);
    		this.add(GamePanels[0]);
    		GamePanels[0].setVisible(true);
    	}
    
    }
    So I try to run this code and it says at point A and B:

    Cannot make a static reference to the non-static field GamePanels
    And it gives a fast solution:
    Change modifier GamePanels to static
    I do this (Which I shouldn't because I actually want the Jpanel to be a instance method and not a static one)

    and in return get the following error messages:
    Exception in thread "AWT-EventQueue-1" java.lang.Error: Unresolved compilation problems:
    Cannot make a static reference to the non-static method remove(Component) from the type JApplet
    Cannot make a static reference to the non-static method add(Component) from the type Container
    What am I doing wrong? Or how do I get this to work?

    PS: I doubt there's anything wrong with my panels

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    And why is ChangePanel, which should be changePanel* static?

    db

    * Code Conventions for the Java(TM) Programming Language: Contents
    sunde887 likes this.

  3. #3
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default

    Tru that...

    It's because I changed the method to static as a fast solution to the following problem:
    (This is my first panel called FirstPanel.java)

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    
    public class FirstPanel extends JPanel implements ActionListener {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	protected JButton b1,b2;
    	Image Background;
    	Color color;
    
    	public FirstPanel()
    	{
    	    try {                
    	    Background = ImageIO.read(new File("ButtonEscape.png"));
    	    } catch (IOException ex) {
    	    }
    		color= Color.black;
    		b1 = new JButton("Change My Color!!");
    		b1.setMnemonic(KeyEvent.VK_D);
    		b1.setActionCommand("ChangeColor");
    		
    		b2 = new JButton("DisplayNextScreen");
    		b2.setMnemonic(KeyEvent.VK_E);
    		b2.setActionCommand("Next");
    		
            b1.addActionListener(this);
            b2.addActionListener(this);
    		
    	    add(b1);
    	    add(b2);
    	}
    	
        @Override
        public void paintComponent(Graphics g) {
        	g.setColor(color);
        	g.fillRect(0, 0, getWidth(), getHeight());
            g.drawImage(Background, getWidth()/2-Background.getWidth(this)/2, 2*getHeight()/3, null);
        }
        
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if ("ChangeColor".equals(e.getActionCommand())) 
    			{
    			if (color==Color.black){
    				color=Color.white;
    			} else {
    				color=Color.black;
    			}
    			repaint();
    			}
    		else {
    			menu.changePanel(0,1); [COLOR="red"]Point A[/COLOR]
            }
    		
    	}
    
    }
    It says at point A the following:
    Cannot make a static reference to the non-static method changePanel(int, int) from the type menu
    And the fast solution is to make changePanel() a static method which I did (and thus it went static)

    But now I have the above error haunting with no idea how to solve it...

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

    Default

    You're fixing things backwards and it's coming back to bite you. The solution is not to make any methods static, but rather to call methods on objects, not on classes. Suggestions include

    • Fix your Java naming (as Darryl mentions) so you don't confuse others who try to help you or grade you. Class names should begin with a capital letter and methods and variables with a lower case letter. You should do this now.
    • Make changePanel a non-static method.
    • Pass an instance of the applet (this within the applet itself) into your FirstPanel and SecondPanel constructors and have these classes store that instance in a Menu variable. This way these classes can call non-static methods on the Main object.
    • Use a CardLayout to do the JPanel swapping for you. The tutorials will show you how to do this.
    Last edited by Fubarable; 08-02-2011 at 12:47 AM.

  5. #5
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Your fixing things backwards and it's coming back to bite you. The solution is not to make any methods static, but rather to call methods on objects, not on classes. Suggestions include

    • Fix your Java naming (as Darryl mentions) so you don't confuse others who try to help you or grade you. Class names should begin with a capital letter and methods and variables with a lower case letter. You should do this now.
    • Make changePanel a non-static method.
    • Pass an instance of the applet (this within the applet itself) into your FirstPanel and SecondPanel constructors and have these classes store that instance in a Menu variable. This way these classes can call non-static methods on the Main object.
    • Use a CardLayout to do the JPanel swapping for you. The tutorials will show you how to do this.
    thnx a lot fuba and db! it works properly now I will read about the Cardlayout tomorrow its rather late now though

Similar Threads

  1. Cannot make a static reference...
    By Guy in forum New To Java
    Replies: 12
    Last Post: 07-28-2011, 07:22 AM
  2. Garbage Collection - Self reference, static and non-static.
    By garyiskidding in forum Advanced Java
    Replies: 10
    Last Post: 03-17-2011, 11:22 AM
  3. Replies: 10
    Last Post: 11-14-2010, 07:06 AM
  4. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  5. make static ref to non-static method?
    By McChill in forum New To Java
    Replies: 7
    Last Post: 02-23-2009, 05:48 AM

Posting Permissions

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