"Cannot find symbol" error

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1788 Views - Last Post: 30 January 2009 - 08:19 PM Rate Topic: -----

#1 codewolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 09

"Cannot find symbol" error

Posted 29 January 2009 - 11:38 AM

This is driving me nuts. Here's my basic object:

package Apartment;

public class Apartment {
	
	// Field declarations
	private int _number;
	private int _occupants;
	
	// Default constructor
	public Apartment() {
		_number = 0;
		_occupants = 0;
	}
	
	// Construct with input
	public Apartment (int Number, int Occupants) {
		_number = Number;
		_occupants = Occupants;
	}
	
	// Set methods
	public void setSuite (int N) { this._number = N; }
	public void setOccupants (int O) {this._occupants = O; }
	
	// Output methods
	public int Number() { return this._number; }	
	public int Occupants() { return this._occupants; }
}


Simple enough, right? But when I try to invoke the setSuite or setOccupants methods I get a "cannot find symbol" compile error.

	private static class addButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent event) {		
		
			// 1. Get unit & number of occupants from form UI			
			int N = Integer.parseInt(unitNumber.getText());
			int O = Integer.parseInt(numOccupants.getText());			
			
			// 2. Assign these values to suite (adjust for zero-based index)
						
			buildingSuites[N - 1].setSuite(N);
			buildingSuites[N - 1].setOccupants(O);					
		}
	}


This is just driving me batty.

Is This A Good Question/Topic? 0
  • +

Replies To: "Cannot find symbol" error

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: "Cannot find symbol" error

Posted 29 January 2009 - 11:45 AM

I can't seem to find what is going wrong...Can you post the entire error?
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: "Cannot find symbol" error

Posted 29 January 2009 - 11:50 AM

Is it safe to assume that buildingSuites[] is an array of Apartment objects? Could we see where you declare/initialize this array?
Was This Post Helpful? 0
  • +
  • -

#4 codewolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 09

Re: "Cannot find symbol" error

Posted 29 January 2009 - 01:54 PM

Okay, here's the full listing:

//****************************
// BuildingManager Application
//
//****************************

import java.io.*;
import java.awt.*;		//supplies layout manager
import java.awt.event.*;
import javax.swing.*;	//supplies JFrame

import Apartment.*;

public class BuildingManager {

	// Field declarations
	final static int BUILDING_SIZE 					= 10;
	private static int totalOccupants 				= 0;
	private static float avgOccupants 				= 0;
	private static int aboveAverageCount			= 0;
	private static int belowAverageCount 			= 0;
	private static Apartment[] buildingSuites 	= new Apartment[BUILDING_SIZE];	
	
	private static int[] Suites = new int[BUILDING_SIZE];
	private static int[] Occupants = new int[BUILDING_SIZE];
	
	// Window elements
	private static JLabel totalResidents;
	private static JLabel averagePerApartment;
	private static JLabel unitsAboveAverage;
	private static JLabel unitsBelowAverage;
	
	private static JTextField unitNumber;
	private static JTextField numOccupants;
	
	private static JButton storeButton;
	private static JButton quitButton;
	
	// Buttonhandlers
	private static class addButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent event) {		
		
			// 1. Get unit & number of occupants from form UI			
			int N = Integer.parseInt(unitNumber.getText());
			int O = Integer.parseInt(numOccupants.getText());			
			
			// 2. Assign these values to suite (adjust for zero-based index)	
			buildingSuites[N - 1].setSuite(N);
			buildingSuites[N - 1].setOccupants(O);					
		}
	}
	
	private static class quitButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent event) {
	
			// 1. Loop through to get total occupants
			totalOccupants = 0;
			for (int i = 0; i < buildingSuites.length; i++) {
				totalOccupants += Occupants[i];
				System.out.println("Unit " + Suites[i] + ": " + Occupants[i]);
				System.out.println(i);
			}
			
			totalResidents.setText("Total occupants: " + totalOccupants);
			
			// 2. Divide total to get average per apartment
			
			// 3. Loop through again to determine over/under average
			
			// 4. Refresh display
			
		}
	}
	
	
	public static void main(String[] args)	throws IOException	{
		
		JFrame outputFrame;		//Declare JFrame variable
		Container outputPane;	//Declare container variable
		JPanel displayPanel;
		JPanel uiPanel;
		
		// Initialize apartment array
		for (int i = 0; i < buildingSuites.length; i ++) {
			buildingSuites[i] = new Apartment(i + 1, 0);
		}
		
		// Initialize UI elements
		totalResidents 		= new JLabel("totalResidents");
		averagePerApartment 	= new JLabel("averagePerApartment");
		unitsAboveAverage 					= new JLabel("unitsAboveAverage");
		unitsBelowAverage		= new JLabel("unitsBelowAverage");
		
		unitNumber 		= new JTextField(2);
		numOccupants 					= new JTextField(2);
		
		storeButton		= new JButton("Store");
		storeButton.addActionListener(new addButtonHandler());
		
		quitButton 		= new JButton("Quit");
		quitButton.addActionListener(new quitButtonHandler());

		
		// Create JFrame object
		outputFrame = new JFrame();
		
		// Ask the JFrame object to return a content pane
		outputPane = outputFrame.getContentPane();
		displayPanel = new JPanel(new GridLayout(4,1));
		uiPanel = new JPanel(new GridLayout(2,2));
		
		displayPanel.add(totalResidents);
		displayPanel.add(averagePerApartment);
		displayPanel.add(unitsAboveAverage);
		displayPanel.add(unitsBelowAverage);
		
		uiPanel.add(unitNumber);
		uiPanel.add(numOccupants);
		uiPanel.add(storeButton);
		uiPanel.add(quitButton);
		
		outputPane.add(displayPanel, BorderLayout.NORTH);
		outputPane.add(uiPanel, BorderLayout.SOUTH);
		
		// Specify the action to take when the window is closed
		outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Specify the size of the JFrame object and make it visible
		outputFrame.setSize(200, 175);
		outputFrame.setVisible(true);
	}
}



PS: I appreciate the quick response to my original post! I just found d.i.c. this morning, and I'm really impressed!
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: "Cannot find symbol" error

Posted 29 January 2009 - 03:00 PM

I still don't see why it's acting like that. What does the compiler error say exactly? (as in...a copy/paste. :) )

This post has been edited by Locke: 29 January 2009 - 03:00 PM

Was This Post Helpful? 0
  • +
  • -

#6 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: "Cannot find symbol" error

Posted 29 January 2009 - 03:05 PM

Unfortunately your code-design doesn't exactly help debugging.. I would however rename tha variables _number and _occupants just to avoid any possible problems with that.
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: "Cannot find symbol" error

Posted 29 January 2009 - 03:13 PM

No, you can start your variable names with any letter or underscore. There's no problem there that I'm aware of.
Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: "Cannot find symbol" error

Posted 29 January 2009 - 03:41 PM

View Postcodewolf, on 29 Jan, 2009 - 12:54 PM, said:

import Apartment.*;



You are importing Apartment. What is your compiling environment - IDE, etc?
Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: "Cannot find symbol" error

Posted 29 January 2009 - 03:46 PM

View Postn8wxs, on 29 Jan, 2009 - 04:41 PM, said:

View Postcodewolf, on 29 Jan, 2009 - 12:54 PM, said:

import Apartment.*;



You are importing Apartment. What is your compiling environment - IDE, etc?


I'd bet that's the line it doesn't like. @codewolf: Comment that line out of the code and see if it runs.
Was This Post Helpful? 0
  • +
  • -

#10 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: "Cannot find symbol" error

Posted 29 January 2009 - 03:46 PM

Quote

No, you can start your variable names with any letter or underscore. There's no problem there that I'm aware of.


You can do It but I wouldn't advise doing it. Generally method/variable names tend to start with a lowercase letter

Where they used mixed case to separate words

Constants are usually all uppercase using underscores to separate the words.
Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: "Cannot find symbol" error

Posted 29 January 2009 - 03:48 PM

View PostBigAnt, on 29 Jan, 2009 - 04:46 PM, said:

Quote

No, you can start your variable names with any letter or underscore. There's no problem there that I'm aware of.

You can do It but I wouldn't advise doing it. Generally method/variable names tend to start with a lowercase letter


I know, all I was saying is that you can...and that it won't give an error.
Was This Post Helpful? 0
  • +
  • -

#12 codewolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 09

Re: "Cannot find symbol" error

Posted 29 January 2009 - 04:26 PM

View PostLocke, on 29 Jan, 2009 - 02:46 PM, said:

View Postn8wxs, on 29 Jan, 2009 - 04:41 PM, said:

View Postcodewolf, on 29 Jan, 2009 - 12:54 PM, said:

import Apartment.*;



You are importing Apartment. What is your compiling environment - IDE, etc?


I'd bet that's the line it doesn't like. @codewolf: Comment that line out of the code and see if it runs.


*sigh* Yeah, I've tried that before. I've got my Apartment class in its own file, in its own folder, and I can use an Apartment object in my program.

That's why it's so frustrating; the object is THERE. Why can't I use one of its methods?

BTW, I'm using jGrasp as my IDE. Not my first choice, but my course specified it.

I really appreciate the help.
Was This Post Helpful? 0
  • +
  • -

#13 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: "Cannot find symbol" error

Posted 29 January 2009 - 04:39 PM

Is it possible that the Apartment objects are not visible because they're inside a nested class and the Instances are declared outside the class? Try creating the objects inside the nested class..
Was This Post Helpful? 0
  • +
  • -

#14 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: "Cannot find symbol" error

Posted 29 January 2009 - 04:51 PM

Wouldn't you just import Apartment?

import Apartment.*; means import all the classes in the Apartment package.

Try import Apartment; without the .*
Was This Post Helpful? 0
  • +
  • -

#15 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: "Cannot find symbol" error

Posted 29 January 2009 - 05:24 PM

Paste the exact error...pretty please? I can't stress this enough. It'll probably tell us what line it's on and at least give us a slight hint.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2