Results 1 to 20 of 30
Thread: Problems troubleshooting errors
- 02-20-2011, 08:15 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 22
- Rep Power
- 0
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); } }
- 02-20-2011, 08:30 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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).
- 02-20-2011, 08:48 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 22
- Rep Power
- 0
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?
-
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.
- 02-20-2011, 08:54 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 30
- Rep Power
- 0
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.
- 02-20-2011, 09:49 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 22
- Rep Power
- 0
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); } }
-
Yep, this will freeze your app:
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.Java Code:do { //.... } while (newAnswer != selectedNum);
- 02-20-2011, 10:02 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 30
- Rep Power
- 0
You can use Eclipse's debugger in order to find your error.
- 02-20-2011, 10:04 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 22
- Rep Power
- 0
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.
- 02-20-2011, 10:44 PM #10
Member
- Join Date
- Feb 2011
- Posts
- 30
- Rep Power
- 0
- 02-20-2011, 11:02 PM #11
Member
- Join Date
- Jan 2011
- Posts
- 22
- Rep Power
- 0
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); } }
- 02-20-2011, 11:17 PM #12
"it doesn't work" provides zero information.
- 02-20-2011, 11:19 PM #13
Since you didn't provide any relevant information I will guess that the "warmer/colder" messages are not being displayed. Try repainting.
- 02-20-2011, 11:31 PM #14
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
nope his logic inside those if statements never equate to true in the compareValues method
problem originates inside actionPerformed():
and newAnswer is compared to oldAnswer for greater or less thanJava Code:oldAnswer=newAnswer;
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++; }
- 02-20-2011, 11:36 PM #15
On the very next line newAnswer gets a different value from the textfield.Java Code:oldAnswer=newAnswer;
- 02-20-2011, 11:39 PM #16
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
: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
- 02-20-2011, 11:39 PM #17
Member
- Join Date
- Jan 2011
- Posts
- 22
- Rep Power
- 0
I put that logic in a stand alone if statement at the end of the compareValues method as follows:
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!Java Code:if(winGame == false) { oldAnswer = newAnswer; count++; }
- 02-20-2011, 11:47 PM #18
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
try this in the constructor
Java Code:gameBoard.setOpaque(true);
- 02-20-2011, 11:54 PM #19
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?
- 02-21-2011, 12:00 AM #20
Member
- Join Date
- Jan 2011
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
InputStream/Jar Problems/File IO Problems
By rdjava in forum Advanced JavaReplies: 31Last Post: 01-17-2011, 11:12 AM -
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
Help with three errors -.-
By Insomniac Riot in forum New To JavaReplies: 5Last Post: 03-30-2010, 06:52 PM -
New to this - having errors with static problems
By afisher300 in forum New To JavaReplies: 4Last Post: 04-20-2009, 11:42 PM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks