Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default Problems troubleshooting errors

    I am still new to Java, and am trying to write a Guessing game program. I keep getting error messages that non-static methods can not be referenced by static context. Does anyone have any ideas how I can correct this issue?
    Sorry about the lack of notes and documentation, I haven't gotten that far yet. Trying to get a workable program. Thanks in advance for any help offered.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    public class JFrameGuess extends JFrame implements ActionListener
    {
    	private int uGuess;
    	final int LENGTH = 500;
    	final int HEIGHT = 200;
    	final int LIMIT = 1000;
    	private int selectedNum;
    	JLabel gameBoard = new JLabel("I am thinking of a number between 1 and 1000.\n" +
    										"Can you guess my number?");
    	JTextField guess = new JTextField(4);
    	JButton startGame = new JButton("Start Game");
    	boolean winGame = false;
    	
    	public JFrameGuess()
    		{
    			super("Program #5 - Number Guessing Game");
    			setSize(LENGTH, HEIGHT);
    			setLayout(new FlowLayout());
    			add(gameBoard);
    			add(guess);
    			add(startGame);
    			setVisible(true);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			startGame.addActionListener(this);
    			Random ranNum = new Random();
    			selectedNum = ranNum.nextInt(LIMIT) + 1;
    			}
    	
    	public void actionPerformed(ActionEvent e)
    		{
    			uGuess = Integer.parseInt(guess.getText());
    		}
    	
    	public static int getUserGuess()
    	{
    		return uGuess;
    	}
    
    	public static int getRandomNum()
    		{
    			return selectedNum;
    		}
    	
    			
    	public void compareValues(int newAnswer, int oldAnswer ,int comNumber, int count)
    		{
    			if(newAnswer > oldAnswer && newAnswer < comNumber)
    				displayWarmerLessThan(newAnswer, count);
    			if(newAnswer < oldAnswer && newAnswer > comNumber)
    				displayWarmerGreaterThan(newAnswer, count);
    			if(newAnswer < oldAnswer && oldAnswer < comNumber)
    				displayColderLessThan(newAnswer, count);
    			if(newAnswer > oldAnswer && oldAnswer > comNumber)
    				displayColderGreaterThan(newAnswer, count);
    			else
    				displayWinner(newAnswer);
    		}
    	
    	public void displayWarmerLessThan(int num, int count)
    		{
    				JLabel game = new JLabel(num + " is TOO LOW!  But you are getting WARMER. Enter guess number " + (count + 1));
    				setBackground(Color.RED);
    				add(startGame);
    				add(guess);
    				setSize(LENGTH, HEIGHT);
    				setVisible(true);
    				startGame.addActionListener(this);	
    		}
    	public void displayWarmerGreaterThan(int num, int count)
    		{
    				JLabel game = new JLabel(num + " is TOO HIGH!  But you are getting WARMER. Enter guess number " + (count + 1));
    				setBackground(Color.RED);
    				add(startGame);
    				add(guess);
    				setSize(LENGTH, HEIGHT);
    				setVisible(true);
    				startGame.addActionListener(this);	
    		}
    	
    	public void displayColderLessThan(int num, int count)
    		{
    				JLabel game = new JLabel(num + " is TOO LOW!  But you are getting COLDER. Enter guess number " + (count + 1));
    				setBackground(Color.BLUE);
    				add(startGame);
    				add(guess);
    				setSize(LENGTH, HEIGHT);
    				setVisible(true);
    				startGame.addActionListener(this);	
    		}
    				
    	public void displayColderGreaterThan(int num, int count)
    		{
    				JLabel game = new JLabel(num + " is TOO HIGH!  Ane you are getting COLDER. Enter guess number " + (count + 1));
    				setBackground(Color.BLUE);
    				add(startGame);
    				add(guess);
    				setSize(LENGTH, HEIGHT);
    				setVisible(true);
    				startGame.addActionListener(this);	
    		}
    		
    	public void displayWinner(int num)
    		{
    			winGame = true;
    			setBackground(Color.YELLOW);
    			
    			setVisible(true);
    			setSize(LENGTH, HEIGHT);
    			JLabel winner = new JLabel("Congratulations " + num + " is the right number.\n" +
    												"Would you like to play again?");
    		}
    
    	
    	public static void main(String[] args)
    		{
    			int count = 1;
    			int newAnswer = 0;
    			int oldAnswer = 0;
    			int CompNum = getRandomNum();
    			
    			JFrameGuess GuessingGame = new JFrameGuess();
    			
    			oldAnswer = newAnswer;
    			newAnswer = getUserNumber();
    			
    			compareValues(newAnswer, oldAnswer, CompNum, count);
    		}
    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Any method/block of code that has the static modifier must use static content. Take your compareValues method it is used inside static void main, but compareValues is non static (does not have the static modifier in its implementation).

  3. #3
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    I understand and see what you are talking about, but when I change my methods to static, my errror list on compile grows. It is even telling me that the preset methods like setColor, and setBackground can not be referenced. Any ideas what I am doing wrong or where the error is in my syntax?

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

    Default

    Don't start making things static as that's going backwards and is counter to good OOP coding technique. The correct approach is to call non-static methods on your objects.

  5. #5
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    You have to decide:
    1. better way - keep everything private and non-static and reference it from not-static context (not from main - use main only to initialize your main object).
    2. worse way - keep everything static and do whatever you want.
    You cannot mix-up static and normal variables and methods in a way you're doing it now.

  6. #6
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    Thanks for the help, I think that I am heading in the right direction now. Program compiles correctly, but when I attempt to run, I enter in my first guess and the java platform freezes. Below is my current code:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    public class JFrameGuess extends JFrame implements ActionListener
    {
    	final int LENGTH = 500;
    	final int HEIGHT = 200;
    	final int LIMIT = 1000;
    	private int newAnswer = 0, oldAnswer, comNumber, count, selectedNum;
    	JLabel gameBoard = new JLabel("I am thinking of a number between 1 and 1000." +
    											" Can you guess my number?");
    											
    	JTextField guess = new JTextField(4);
    	JButton startGame = new JButton("Start Game");
    	JButton exit = new JButton("Exit");
    	boolean winGame = false;
    	
    	public JFrameGuess()
    		{
    			super("Program #5 - Number Guessing Game");
    			setSize(LENGTH, HEIGHT);
    			setLayout(new FlowLayout());
    			add(gameBoard);
    			add(guess);
    			add(startGame);
    			add(exit);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			startGame.addActionListener(this);
    			exit.addActionListener(this);
    			Random ranNum = new Random();
    			selectedNum = ranNum.nextInt(LIMIT) + 1;
    		}
    	
    	public void actionPerformed(ActionEvent e)
    		{
    			Object source = e.getSource();
    			
    			if(source == exit)
    				System.exit(0);
    			else
    				{
    					oldAnswer = newAnswer;
    					newAnswer = Integer.parseInt(guess.getText());
    					int number = selectedNum;
    					int times = count;
    					compareValues(newAnswer, oldAnswer, number, times);
    				}
    		}
    	
    	public void compareValues(int newAnswer, int oldAnswer ,int comNumber, int count)
    		{
    			do
    			{
    			if(newAnswer > oldAnswer && newAnswer < comNumber)
    				displayWarmerLessThan(newAnswer, count);
    			if(newAnswer < oldAnswer && newAnswer > comNumber)
    				displayWarmerGreaterThan(newAnswer, count);
    			if(newAnswer < oldAnswer && oldAnswer < comNumber)
    				displayColderLessThan(newAnswer, count);
    			if(newAnswer > oldAnswer && oldAnswer > comNumber)
    				displayColderGreaterThan(newAnswer, count);
    			else
    				displayWinner(newAnswer);
    			
    			count++;
    			}while (newAnswer != selectedNum);
    		}
    	
    	public void displayWarmerLessThan(int num, int count)
    		{
    				JLabel game = new JLabel(num + " is TOO LOW!  But you are getting WARMER." +
    													"  Enter guess number " + (count + 1));
    				setBackground(Color.RED);
    					
    		}
    	public void displayWarmerGreaterThan(int num, int count)
    		{
    				JLabel game = new JLabel(num + " is TOO HIGH!  But you are getting WARMER." +
    													"  Enter guess number " + (count + 1));
    				setBackground(Color.RED);
    				
    		}
    	
    	public void displayColderLessThan(int num, int count)
    		{
    				JLabel game = new JLabel(num + " is TOO LOW!  But you are getting COLDER." +
    													"  Enter guess number " + (count + 1));
    				setBackground(Color.BLUE);
    						}
    				
    	public void displayColderGreaterThan(int num, int count)
    		{
    				JLabel game = new JLabel(num + " is TOO HIGH!  Ane you are getting COLDER." + 
    													"  Enter guess number " + (count + 1));
    				setBackground(Color.BLUE);
    						}
    		
    	public void displayWinner(int num)
    		{
    			winGame = true;
    			setBackground(Color.YELLOW);
    			
    			JLabel winner = new JLabel("Congratulations " + num + " is the right number.  " +
    												"It took you " + count + " attempts to guess my number.  " +
    												"Would you like to play again?");
    		}
    
    	
    	public static void main(String[] args)
    		{
    					
    			JFrameGuess GuessingGame = new JFrameGuess();
    			GuessingGame.setVisible(true);
    		}
    }

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

    Default

    Yep, this will freeze your app:

    Java Code:
    do
    {
       //....
    } while (newAnswer != selectedNum);
    You'd best get rid of this loop as it will lock the main Swing thread, the EDT. The code in the loop looks OK, but the do-while doesn't.

  8. #8
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    You can use Eclipse's debugger in order to find your error.

  9. #9
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    What am I missing? I tried a while loop and it didn't work either. So far I don't know if the background color and tests will work.

  10. #10
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    How about removing loop?

  11. #11
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    I removed the loop, but I can not figure out why the compareValues method doesn't seem to be working. Any ideas?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    public class JFrameGuess extends JFrame implements ActionListener
    {
    	final int LENGTH = 600;
    	final int HEIGHT = 200;
    	final int LIMIT = 1000;
    	private int newAnswer = 0, oldAnswer, comNumber, count = 1, selectedNum;
    	JLabel gameBoard = new JLabel("I am thinking of a number between 1 and 1000." +
    											"  Can you guess my number?");
    	JTextField guess = new JTextField(4);
    	JButton startGame = new JButton("Start Game");
    	JButton exit = new JButton("Exit");
    	boolean winGame = false;
    	
    	public JFrameGuess()
    		{
    			super("Program #5 - Number Guessing Game");
    			setSize(LENGTH, HEIGHT);
    			setLayout(new FlowLayout());
    			add(gameBoard);
    			add(guess);
    			add(startGame);
    			add(exit);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			startGame.addActionListener(this);
    			exit.addActionListener(this);
    			selectedNum = ((int)Math.random() * 100 + 1);
    		}
    	
    	public void actionPerformed(ActionEvent e)
    		{
    			Object source = e.getSource();
    			
    			if(source == startGame)
    				{
    						oldAnswer = newAnswer;
    						newAnswer = Integer.parseInt(guess.getText());
    						int number = selectedNum;
    						int times = count;
    						compareValues(newAnswer, oldAnswer, number, times);
    				}
    				
    			else
    				System.exit(0);
    				
    		}
    	
    	public void compareValues(int newAnswer, int oldAnswer ,int comNumber, int count)
    		{
    		
    			if(newAnswer > oldAnswer && newAnswer < comNumber)
    				displayWarmerLessThan(newAnswer, count);
    			if(newAnswer < oldAnswer && newAnswer > comNumber)
    				displayWarmerGreaterThan(newAnswer, count);
    			if(newAnswer < oldAnswer && oldAnswer < comNumber)
    				displayColderLessThan(newAnswer, count);
    			if(newAnswer > oldAnswer && oldAnswer > comNumber)
    				displayColderGreaterThan(newAnswer, count);
    			else if (newAnswer == selectedNum)
    				displayWinner(newAnswer);			
    			
    			count++;
    			
    		}
    	
    	public void displayWarmerLessThan(int num, int count)
    		{
    				gameBoard.setText(num + " is TOO LOW!  But you are getting WARMER." +
    													"  Enter guess number " + (count + 1));
    				gameBoard.setBackground(Color.RED);
    					
    		}
    	public void displayWarmerGreaterThan(int num, int count)
    		{
    				gameBoard.setText(num + " is TOO HIGH!  But you are getting WARMER." +
    													"  Enter guess number " + (count + 1));
    				gameBoard.setBackground(Color.RED);
    				
    		}
    	
    	public void displayColderLessThan(int num, int count)
    		{
    				gameBoard.setText(num + " is TOO LOW!  But you are getting COLDER." +
    													"  Enter guess number " + (count + 1));
    				gameBoard.setBackground(Color.BLUE);
    		}
    				
    	public void displayColderGreaterThan(int num, int count)
    		{
    				gameBoard.setText(num + " is TOO HIGH!  Ane you are getting COLDER." + 
    													"  Enter guess number " + (count + 1));
    				gameBoard.setBackground(Color.BLUE);
    		}
    		
    	public void displayWinner(int num)
    		{
    			gameBoard.setBackground(Color.YELLOW);
    			
    			gameBoard.setText("Congratulations " + num + " is the right number.\n" +
    									"It took you " + count + " attempts to guess my number.\n" +
    									"Would you like to play again?");
    			winGame = true;
    		}
    
    	
    	public static void main(String[] args)
    		{
    					
    			JFrameGuess GuessingGame = new JFrameGuess();
    			GuessingGame.setVisible(true);
    		}
    }

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    "it doesn't work" provides zero information.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Since you didn't provide any relevant information I will guess that the "warmer/colder" messages are not being displayed. Try repainting.

  14. #14
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    nope his logic inside those if statements never equate to true in the compareValues method

    problem originates inside actionPerformed():
    Java Code:
    oldAnswer=newAnswer;
    and newAnswer is compared to oldAnswer for greater or less than
    Java Code:
    public void compareValues(int newAnswer, int oldAnswer ,int comNumber, int count)
    		{
    		
    			if(newAnswer > oldAnswer && newAnswer < comNumber)
    				displayWarmerLessThan(newAnswer, count);
    			if(newAnswer < oldAnswer && newAnswer > comNumber)
    				displayWarmerGreaterThan(newAnswer, count);
    			if(newAnswer < oldAnswer && oldAnswer < comNumber)
    				displayColderLessThan(newAnswer, count);
    			if(newAnswer > oldAnswer && oldAnswer > comNumber)
    				displayColderGreaterThan(newAnswer, count);
    			else if (newAnswer == selectedNum)
    				displayWinner(newAnswer);			
    			
    			count++;
    			
    		}

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    oldAnswer=newAnswer;
    On the very next line newAnswer gets a different value from the textfield.

  16. #16
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    :o yup you are right, still think those if statements don't equate to true, because I made one of them absolutely be true and one of those displayWarmer methods ran

  17. #17
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    I put that logic in a stand alone if statement at the end of the compareValues method as follows:
    Java Code:
    if(winGame == false)
    				{
    					oldAnswer = newAnswer;
    					count++;
    				}
    I still can not get the red or blue background to display. The program runs, input first guess, and nothing happens. If I wasn't already bald, I would be pulling my hair out now!

  18. #18
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    try this in the constructor

    Java Code:
    gameBoard.setOpaque(true);

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Against my better judgement I ran your code and turns out you always have 1 as the secret number. Math.random generates a number from 0 to 1. So 0.00000009 is a valid random number. You then convert that to an int so it become 0. Multiply by 100 = 0. Add 1 = 1.

    I see in your previous code you were using the Random class. Why did you stop and use Math.random instead?

  20. #20
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    I was grasping at straws as to what the issue was. Like I said I am still new to Java, learning along the way. Thanks to all of you for the help so far.

Page 1 of 2 12 LastLast

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  2. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  3. Help with three errors -.-
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 03-30-2010, 06:52 PM
  4. New to this - having errors with static problems
    By afisher300 in forum New To Java
    Replies: 4
    Last Post: 04-20-2009, 11:42 PM
  5. Replies: 3
    Last Post: 03-08-2009, 01:51 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
  •