Results 1 to 2 of 2
  1. #1
    cachi is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Error: cannot find symbol

    Hello, After a little while of trying to figure this one out myself, I have given up on what should be a simple fix.

    Error:

    Java Code:
    GuiGuessingGameApp.java:7: cannot find symbol
    symbol  : constructor GuiGuessingGameFrame()
    location: class GuiGuessingGameFrame
    	GuiGuessingGameFrame frame = new GuiGuessingGameFrame();
    Java Code:
    public class GuiGuessingGameTest
    {
    	public static void main(String[] args)
    	{
    		GuiGuessingGameApp game = new GuiGuessingGameApp();
    		game.play();
    	}
    }
    Java Code:
    import java.util.Random;
    
    public class GuiGuessingGameApp
    {
    	Random rnd = new Random();
    	int secretNumber = 0;
    	GuiGuessingGameFrame frame = new GuiGuessingGameFrame();
    
    	public GuiGuessingGameApp()
    	{
    		secretNumber = 1 + rnd.nextInt(100);
    	}
    	
    	public void play()
    	{
    		if (frame != null)
    			frame.show();
    	}
    }

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class GuiGuessingGameFrame
    {
    	JFrame myFrame = new JFrame();
    	JTextField myField = new JTextField("",2);
    	JLabel myLabel = new JLabel("Enter A Guess");
    	JButton myButton = new JButton ("Guess!");
    	
    	int mySecretNumber  = -1;
    	
    	public GuiGuessingGameFrame(int mySecretNumber)
    	{
    		myFrame = new JFrame("Guessing Game!");
    		myFrame.setSize(200,95);
    		myFrame.setVisible(true);
    		myFrame.addWindowListener(new WinHandler());	
    
    		Container contain = myFrame.getContentPane();
    		contain.setLayout(new FlowLayout());
    		
    		contain.add(myField);
    		contain.add(myButton);
    		contain.add(myLabel);	
    		
    		myButton.addActionListener(new ButtonListener());
    	}
    
    	public void show()
    	{
    		myFrame.show();
    	}
    	
    	class ButtonListener implements ActionListener
    	{
    		public void actionPerformed( ActionEvent evt)
    		{
    			int guess = 0;
    			try
    			{
    				guess = Integer.parseInt(myField.getText());
    			}
    			catch (NumberFormatException e)
    			{
    				guess = 0;
    			}
    			System.out.println("The Number In The Text Box Is " + guess);	
    		}
    	}
    }
    class WinHandler extends WindowAdapter
    {
    	public void windowClosing(WindowEvent e) {System.exit(0);}
    }
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Your only constructor
    Java Code:
    public GuiGuessingGameFrame(int mySecretNumber)
    takes an int argument. In your test class you are using
    Java Code:
    new GuiGuessingGameApp();
    a noľargument constructor which the class does not have.
    If you do not explicitly define a constructor for a class java provides a noľargument constructor for it. If you provide any constructor that takes arguments you no longer get the default noľarg constructor from java. In this case you must add a noľargument constructor in the class.

Similar Threads

  1. [SOLVED] Java Error: Cannot find Symbol...
    By bobleny in forum New To Java
    Replies: 8
    Last Post: 04-15-2008, 06:35 AM
  2. Programm Error: cannot find symbol Help?
    By junix in forum New To Java
    Replies: 2
    Last Post: 12-10-2007, 05:30 AM
  3. cannot find symbol class error
    By po0oker in forum New To Java
    Replies: 5
    Last Post: 10-31-2007, 02:52 PM
  4. Error: cannot find symbol
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:39 AM
  5. Error: cannot find symbol constructor
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:24 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
  •