Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: non-static method cannot be referenced from a static context

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question non-static method cannot be referenced from a static context

    Hello again Java gurus. I am in the process of making a poker game and am having difficulties with using two methods from my player class which determine which type of hand a player has and what rank (in relation to the other hands) the player's hand type is. When I complie my poker class I am given the error message non-static method (insert method here) cannot be referenced from a static context. I am not quite sure what the brick (my computer) is trying to tell me. The methods in question are getHandType() and getHandStrength() which are located in the player class and the error ocurrs in the actionPerformed of the poker class where I attempt to set player2Hand and player1Hand text (towards the bottom of the class). I thought about extending Player to Poker (that way I dont have to fuss with all the dot notation) but I havnt been able to get that to work either. I will post the two classes, any guidance (or shoves) in the right direction would be much appreciated.

    Player Class
     
    import javax.swing.*;       // go get swing template
    import java.awt.*;          // go get standard graphic
    import java.awt.event.*;    // go get graphic listener
     
    public class Player
    {
        private String    name;				// name is a string
     
        // playerHand is an array of 5 cards
        private Card[]    playerHand  = new Card[5];
     
        // playerPicture is an ImageIcon
        private ImageIcon playerPicture;
     
        // numberDealt starts at 0;
        private int       numberDealt = 0;
     
    	// highCard is either true or false
        private boolean   highCard;
     
    	// pair is either true or false
        private boolean   pair;
     
    	//twoPair is either true or false
        private boolean   twoPair;
     
    	// threeOfAKind is either true or false
        private boolean   threeOfAKind;
     
    	// fullHouse is either true or false
        private boolean   fullHouse;
     
    	// straight is either true or false
        private boolean   straight;
     
    	// flush is either true or false
        private boolean   flush;
     
    	// fourOfAKind is either true or false
        private boolean   fourOfAKind;
     
    	// straightFlush is either true or false
        private boolean   straightFlush;
     
    	// royalFlush is either true or false
        private boolean   royalFlush;
     
        private String    handType;
     
        private int 	  handStrength;
     
    	/*
    	* The player constructor creates a basic player that has
    	* a name, a picture, and starts with no cards in his hand.
    	*/
        public Player()
        {
            name           = "Card Shark";	// name is Card Shark
     
            // playerPicture is an ImageIcon stored as owned.gif
            playerPicture  = new ImageIcon("owned.gif");
     
            // numberDealt starts at 0;
            numberDealt    = 0;
     
            handStrength   = 0;
        }
     
    	/*
    	* get the player's name and return it
    	*/
        public String getName()
        {
            return name;					// return the name
        }
     
    	/*
    	* set the name, name is a string and is assigned letter n
    	*/
        public void setName(String n)
        {
            name = n;						// name is n
        }
     
    	/*
    	* get the playerPicture which is an ImageIcon
    	*/
        public ImageIcon getplayerPicutre()
        {
            return playerPicture;			// return the playerPicture
        }
     
    	/*
    	* set the playerPicture and assign playerPicture p
    	*/
        public void setplayerPicture(ImageIcon p)
        {
            playerPicture = p;				// playerPicture is p
        }
     
    	/*
    	* get a card from the playerHand and return the card
    	*/
        public Card getplayerHand()
        {
    		// if numberDealt is less than 0 do nothing
            if(numberDealt < 0)
            {
     
            }
     
    		// if numberDealt is anything other than less than 0 numberDealt--
            else
            {
                numberDealt --;				// go back a card
            }
     
            // return the card in the hand as determined by numberDealt
            return playerHand[numberDealt];
        }
     
    	/*
    	* set a card in playerHand and assign playerHand c
    	*/
        public void setplayerHand(Card c)
        {
    		// this assigns numberDealt to the playerHand to c
            playerHand[numberDealt] = c;
     
    		// if numberDealt is less than 5 add 1
            if(numberDealt < 5)
            {
                numberDealt ++;				// add 1 to numberDealt
            }
        }
     
    	/*
    	* sortByRank() sorts the cards in the hand placing them in order
    	* of lowest to highest rank.
    	*/
        public void sortByRank()
        {
    					// do the next loop 5 times
            for(int x=0; x<4; x++)
            {
    					// do this loop 5 times.
                for(int s=0; s<4; s++)
                {
    					// if s' rank is greater than s+1 swap them
                    if(playerHand[s].getRank() > playerHand[s+1].getRank())
                    {
    					// tempCard is a card that will hold playerHand[s]
                        Card tempCard   = playerHand[s];
     
                        // put playerHand[s+1] in playerHand[s]
                        playerHand[s]   = playerHand[s+1];
     
                        // put tempCard in playerHand[s+1]
                        playerHand[s+1] = tempCard;
                    }
                }
            }
        }
     
    	/*
    	* sortByValue() sorts the cards in the hand placing them in order
    	* of lowest to highest rank
    	*/
        public void sortByValue()
        {
    					// do the next loop 5 times
            for(int x=0; x<4; x++)
            {
    					// do this loop 5 times
                for(int s=0; s<4; s++)
                {
    					// if s' value is greater than s+1 swap them
                    if(playerHand[s].getValue() > playerHand[s+1].getValue())
                    {
    					// tempCard is a card that will hold playerHand[s]
                        Card tempCard   = playerHand[s];
     
                        // put playerHand[s+1] in playerHand[s]
                        playerHand[s]   = playerHand[s+1];
     
                        // put tempCard in playerHand[s+1]
                        playerHand[s+1] = tempCard;
                    }
                }
            }
        }
     
    	/*
    	* sortByValue() sorts the cards in the hand palcing them in order
    	* of lowest to highest rank
    	*/
        public void sortBySuit()
        {
    					// do the next loop 5 times
            for(int x=0; x<4; x++)
            {
    					// do this loop 5 times
                for(int s=0; s<4; s++)
                {
    					// if s' value is greater than s+1 swap them
                    if(playerHand[s].getSuit() > playerHand[s+1].getSuit())
                    {
    					// tempCard is a card that will hold playerHand[s]
                        Card tempCard   = playerHand[s];
     
                        // put playerHand[s+1] in playerHand[s]
                        playerHand[s]   = playerHand[s+1];
     
                        // put tempCard in playerHand[s+1]
                        playerHand[s+1] = tempCard;
                    }
                }
            }
        }
     
    	/*
    	* pair() looks through the player's hand and determines wether or not
    	* the player has a pair then returns the answer.
    	*/
     
    	public boolean highCard()
    	{
    		if(pair == false);
    		{
    			if(twoPair == false);
    			{
    				if(threeOfAKind == false)
    				{
    					if(fullHouse == false)
    					{
    						if(straight == false)
    						{
    							if(flush == false)
    							{
    								if(straightFlush == false)
    								{
    									if(royalFlush == false)
    									{
    										highCard = true;
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    		return highCard;
    	}
     
    	/*
    	* pair() looks through the player's hand and determines wether or not
    	* the player has a pair then returns the answer.
    	*/
        public boolean pair()
        {
    		for(int slot=0; slot<4; slot++)
    		{
    			if(playerHand[slot].getValue() == playerHand[slot+1].getValue())
    			{
    				pair = true;
    			}
    			else
    			{
    				pair = false;
    			}
    		}
     
    		return pair;
    	}
     
    	/*
    	* twoPair() looks through the player's hand and determines wether or
    	* not the player has two pair then returns the answer.
    	*/
    	public boolean twoPair()
    	{
    		for(int slot=0; slot<2; slot++)
    		{
    			if(playerHand[slot].getValue() == playerHand[slot+1].getValue())
    			{
    				if(playerHand[slot+2].getValue() == playerHand[slot+3].getValue())
    				{
    					twoPair = true;
    				}
    				else
    				{
    					twoPair = false;
    				}
    			}
    		}
    		return twoPair;
    	}
     
    	/*
    	* threeOfAKind() looks through the player's hand and determines wether
    	* or not the player has three of a kind then returns the answer.
    	*/
    	public boolean threeOfAKind()
    	{
    		for(int slot=0; slot<3; slot++)
    		{
    			if(playerHand[slot].getValue() == playerHand[slot+1].getValue())
    			{
    				if(playerHand[slot+1].getValue() == playerHand[slot+2].getValue())
    				{
    					threeOfAKind = true;
    				}
    			}
    		}
    		return threeOfAKind;
    	}
     
    	/*
    	* fullHouse() looks through the player's hand and determines wether or
    	* not the player has a full house then returns the answer.
    	*/
    	public boolean fullHouse()
    	{
    		for(int slot=0; slot<1; slot++)
    		{
    			if(playerHand[slot].getValue() == playerHand[slot+1].getValue())
    			{
    				if(playerHand[slot+1].getValue() == playerHand[slot+2].getValue())
    				{
    					if(playerHand[slot+3].getValue() == playerHand[slot+4].getValue())
    					{
    						fullHouse = true;
    					}
    				}
    			}
    		}
    		return fullHouse;
    	}
     
    	/*
    	* straight() looks through the player's hand and determines wether or
    	* not the player has a straight then returns the answer.
    	*/
    	public boolean straight()
    	{
    		for(int slot=0; slot<3; slot++)
    		{
    			if(playerHand[slot].getValue() == playerHand[slot+1].getValue()+1)
    			{
    				straight = true;
    			}
    		}
    		return straight;
    	}
     
    	/*
    	* flush() looks through the player's hand and determines wether or
    	* not the player has a straight then returns the answer.
    	*/
    	public boolean flush()
    	{
    		for(int slot=0; slot<1; slot++)
    		{
    			if(playerHand[slot].getSuit() == playerHand[slot+1].getSuit())
    			{
    				if(playerHand[slot+1].getSuit() == playerHand[slot+2].getSuit())
    				{
    					if(playerHand[slot+2].getSuit() == playerHand[slot+3].getSuit())
    					{
    						if(playerHand[slot+3].getSuit() == playerHand[slot+4].getSuit())
    						{
    							flush = true;
    						}
    					}
    				}
    			}
    		}
    		return flush;
    	}
     
    	/*
    	* fourOfAKind() looks through the player's hand and determines
    	* wether or not the player has four of a kind then returns the answer.
    	*/
    	public boolean fourOfAKind()
    	{
    		for(int slot=0; slot<2; slot++)
    		{
    			if(playerHand[slot].getValue() == playerHand[slot+1].getValue())
    			{
    				if(playerHand[slot+1].getValue() == playerHand[slot+2].getValue())
    				{
    					if(playerHand[slot+2].getValue() == playerHand[slot+3].getValue())
    					{
    						if(playerHand[slot+3].getValue() == playerHand[slot+4].getValue())
    						{
    							fourOfAKind = true;
    						}
    					}
    				}
    			}
    		}
    		return fourOfAKind;
    	}
     
    	/*
    	* straightFlush() looks through the player's hand and determines
    	* wether or not the player has a straightFlush then retruns the answer
    	*/
    	public boolean straightFlush()
    	{
    		if(straight == true && flush == true)
    		{
    			straightFlush = true;
    		}
    		else
    		{
    			straightFlush = false;
    		}
     
    		return straightFlush;
    	}
     
    	/*
    	* royalFlush() looks through the player's hand and determines
    	* wether or not the player has a straightFlush then returns
    	* the answer
    	*/
    	public boolean royalFlush()
    	{
    		if(flush == true)
    		{
    			for(int slot=0; slot<1; slot++)
    			{
    				if(playerHand[slot].getValue() == 10)
    				{
    					if(playerHand[slot+1].getValue() == 11)
    					{
    						if(playerHand[slot+2].getValue() == 12)
    						{
    							if(playerHand[slot+3].getValue() == 13)
    							{
    								if(playerHand[slot+4].getValue() == 14)
    								{
    									royalFlush = true;
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    		return royalFlush;
    	}
     
    	/*
    	* handType() gives each hand type a ranking and returns which
    	* hand a player has.
    	*/
    	/*public String handType()
    	{
    		if(highCard == true)
    		{
    			handStrength = 0;
    			return "High Card";
    		}
     
    		if(pair == true)
    		{
    			handStrength = 1;
    			return " a pair";
    		}
     
    		if(twoPair == true)
    		{
    			handStrength = 2;
    			return " a pair";
    		}
     
    		if(threeOfAKind == true)
    		{
    			handStrength = 3;
    			return "three of a kind";
    		}
     
    		if(fullHouse == true)
    		{
    			handStrength = 4;
    			return "a full house";
    		}
     
    		if(straight == true)
    		{
    			handStrength = 5;
    			return "a straight";
    		}
     
    		if(flush == true)
    		{
    			handStrength = 6;
    			return "a flush";
    		}
     
    		if(fourOfAKind == true)
    		{
    			handStrength = 7;
    			return "four of a kind";
    		}
     
    		if(straightFlush == true)
    		{
    			handStrength = 8;
    			return "a straight flush";
    		}
     
    		if(royalFlush == true)
    		{
    			handStrength = 9;
    			return "a royal flush";
    		}
    		return handType;
    	}*/
     
    	public String gethandType()
    	{
    		return handType;
    	}
     
    	public int gethandStrength()
    	{
    		return handStrength;
    	}
     
    	public void sethandStrength()
    	{
    		if(royalFlush == true)
    		{
    			handStrength = 9;
    		}
    		else if(straightFlush == true)
    		{
    			handStrength = 8;
    		}
     
    		else if(fourOfAKind == true)
    		{
    			handStrength = 7;
    		}
    		else if(flush == true)
    		{
    			handStrength = 6;
    		}
    		else if(straight == true)
    		{
    			handStrength = 5;
    		}
    		else if(fullHouse == true)
    		{
    			handStrength = 4;
    		}
    		else if(threeOfAKind == true)
    		{
    			handStrength = 3;
    		}
    		else if(twoPair == true)
    		{
    			handStrength = 2;
    		}
    		else if(pair == true)
    		{
    			handStrength = 1;
    		}
    		else if(highCard == true)
    		{
    			handStrength = 0;
    		}
    	}
    }

    Poker class
     
    import javax.swing.*;		// go get swing template
    import java.awt.*;		    // go get standard graphic
    import java.awt.event.*;	// go get graphic listener
     
    public class Poker extends JFrame implements ActionListener
    {
    	// f is a JFrame
    	private static      JFrame f;
     
    	// p is a JPanel
    	private JPanel      p;
     
    	// rePlayButton is a JButton
    	private JButton     rePlayButton;
     
    	// groupButton1 is an array of 5 JButtons
    	private JButton[]   groupButton1 = new JButton[5];
     
    	// groupButton2 is an array of 5 JButtons
    	private JButton[]   groupButton2 = new JButton[5];
     
    	// dealButton
    	private JButton		dealButton;
     
    	// cardLabel1 is a JLabel
    	private JLabel      cardLabel1;
     
    	// cardLabel2 is a JLabel
    	private JLabel	    cardLabel2;
     
    	// card1 is an array of 5 cards
    	private Card[]      card1		= new Card[5];
     
    	// card2 is an array of 5 cards
    	private Card[]      card2		= new Card[5];
     
    	// myDeck is a Deck
    	private Deck        myDeck;
     
    	// menuBar is a JMenuBar
    	private JMenuBar    menuBar;
     
    	// item1 is a JMenuItem
    	private JMenuItem   item1;
     
    	// winText is a text field
    	private JTextField  winText;
     
    	// player1Hand is a text field
    	private JTextField  player1Hand;
     
    	// player2Hand is a text field
    	private JTextField  player2Hand;
     
    	/**
    	* The main method sets up a interface manager and catches any
    	* errors to keep the program from exploding in a ball of gooey ooze
    	*/
    	public static void main(String arg[])
    	{
    		try
    		{
    			UIManager.setLookAndFeel(
    		                UIManager.getCrossPlatformLookAndFeelClassName());
    		}
    		catch (Exception e)
    		{
     
    		}
     
    	    Poker app = new Poker();
     
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.pack();
    	   	f.setVisible(true);
    	}
     
    	/**
    	* The Poker() constructor method creates a default card game with 10
    	* cards arranged in 2 rows of five and a deck named myDeck.
    	*/
    	public Poker()
    	{
    		f = new    JFrame("Robert's Poker game.");
    		p = new    JPanel();
    		GridLayout layout   = new GridLayout(3,5);
    		p.setLayout(layout);
    		Card       card1[]  = new Card[0];
    		Card       card2[]  = new Card[0];
    				   myDeck   = new Deck();
    		addButtonsToContentPanel();
    		addMenuToFrame();
    		f.setContentPane(p);
    	}
     
    	/**
    	* Adds all of the buttons to the screen and making each listen for
    	* clicks. The back of each card is Icon frog.
    	*/
    	public void addButtonsToContentPanel()
    	{
    		for(int slot=0; slot<5; slot++)
    		{
    			groupButton1[slot] = new JButton("Card");
    			Icon frog          = new ImageIcon("back.gif");
    			groupButton1[slot] = new JButton("Change Card", frog);
    			p.add(groupButton1[slot]);
    			groupButton1[slot].addActionListener(this);
    		}
     
    		for(int slot2=0; slot2<5; slot2++)
    		{
    			groupButton2[slot2] = new JButton("Card");
    			Icon frog2         = new ImageIcon("back.gif");
    			groupButton2[slot2] = new JButton("Change Card", frog2);
    			p.add(groupButton2[slot2]);
    			groupButton2[slot2].addActionListener(this);
    		}
    		rePlayButton = new JButton("Play Again");
    		dealButton   = new JButton("Deal!");
    		p.add(rePlayButton);
    		p.add(dealButton);
    		rePlayButton.addActionListener(this);
    		dealButton.addActionListener(this);
    	}
     
    	public void addFieldToContentPanel()
    	{
    		winText     = new JTextField(10);
    		player1Hand = new JTextField(10);
    		player2Hand = new JTextField(10);
    		winText.setEditable(false);
    		player1Hand.setEditable(false);
    		player2Hand.setEditable(false);
    		p.add(winText);
    		p.add(player1Hand);
    		p.add(player2Hand);
    		winText.addActionListener(this);
    		player1Hand.addActionListener(this);
    		player2Hand.addActionListener(this);
    	}
    	/**
    	* Add menu to frame adds a menu to the application window with
    	* one button
    	*/
    	public void addMenuToFrame()
    	{
    		menuBar    = new JMenuBar();
    		JMenu menu = new JMenu("Game Menu");
    		item1      = new JMenuItem("Shuffle the deck cheater!");
    		item1.addActionListener(this);
    		menu.add(item1);
    		menuBar.add(menu);
    		f.setJMenuBar(menuBar);
    	}
     
    	/**
    	* actionPerformed does all of the work. When a button is clicked
    	* it's picture is replaced with a new card from myDeck.
    	* The rePlayButton sets all of the cards to the back image and
    	* shuffles the deck.
    	*/
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource() == item1)
    		{
    			myDeck.shuffle();
    		}
     
    		Icon frog = new ImageIcon("back.gif");
     
    		if(e.getSource() == rePlayButton)
    		{
    			myDeck.shuffle();
    			for(int slot=0; slot<5; slot++)
    			{
    				groupButton1[slot].setIcon(frog);
    				groupButton2[slot].setIcon(frog);
    			}
    		}
     
    		if(e.getSource() == dealButton)
    		{
    			myDeck.shuffle();
    			for(int slot=0; slot<5; slot++)
    			{
    				card1[slot] = myDeck.dealACard();
    				card2[slot] = myDeck.dealACard();
    				groupButton1[slot].setIcon(card1[slot].getFront());
    				groupButton2[slot].setIcon(card2[slot].getFront());
    			}
    		}
     
    		for(int x=0; x<5; x++)
    		{
    			if(e.getSource() == groupButton1[x])
    			{
    				for(int slot=0; slot<5; slot++)
    				{
    					card1[slot] = myDeck.dealACard();
    					groupButton1[slot].setIcon(card1[slot].getFront());
    					player1Hand.setText("Player 1 has" + Player.gethandType());
    				}
    			}
    		}
     
    		for(int y=0; y<5; y++)
    		{
    			if(e.getSource() == groupButton2[y])
    			{
    				for(int slot2=0; slot2<5; slot2++)
    				{
    					card2[slot2] = myDeck.dealACard();
    					groupButton2[slot2].setIcon(card2[slot2].getFront());
    					player2Hand.setText("Player 2 has" + Player.gethandType());
    				}
    			}
    		}
     
    		if(card1.gethandStrength() > card2.gethandStrength())
    		{
    			winText.setText("Player 1 Wins!");
    		}
    		else
    		{
    			winText.setText("Player 2 Wins!");
    		}
    		repaint();
    	}
    }	// end Poker.java

    Error Message
    G:\Programming\Poker.java:218: non-static method gethandType() cannot be referenced from a static context
    					player1Hand.setText("Player 1 has" + Player.gethandType());
    					                                           ^
    G:\Programming\Poker.java:231: non-static method gethandType() cannot be referenced from a static context
    					player2Hand.setText("Player 2 has" + Player.gethandType());
    					                                           ^
    G:\Programming\Poker.java:236: cannot find symbol
    symbol  : method gethandStrength()
    location: class Card[]
    		if(card1.gethandStrength() > card2.gethandStrength())
    		        ^
    G:\Programming\Poker.java:236: cannot find symbol
    symbol  : method gethandStrength()
    location: class Card[]
    		if(card1.gethandStrength() > card2.gethandStrength())
    		                                  ^
    4 errors
     
    Tool completed with exit code 1
    Last edited by Kaltonse; December 21st, 2010 at 06:40 PM.
    When in doubt: while( ! ( succeed = try() ) );


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: non-static method cannot be referenced from a static context

    See this post:
    Common Java Mistakes - Referencing Non-static variables/methods in a static way

    Summary:
    player1Hand.setText("Player 1 has" + Player.gethandType());

    You're trying to call a method from Player (the class) rather than a particular instance of the Player class (say, something like player1 and player2). The link should provide you with an easy fix to this problem.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Kaltonse (December 21st, 2010)

  4. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: non-static method cannot be referenced from a static context

    Thank you very much. After reading that document I was able to fix my problem and am one step closer to completion. Thanks! (+ Rep)
    When in doubt: while( ! ( succeed = try() ) );

Similar Threads

  1. Error using public static double Method
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2010, 03:01 PM
  2. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  3. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 02:24 AM
  4. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 12:56 PM
  5. Static method
    By kalees in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2009, 10:10 AM