Basic Java Problem: Me, my IDE, Java 1.4.x, or the book I'm reading?

TommyWillB

Registered
I've recently started trying for the 3rd time to master some basic Java... So I bought a fairly straightforward Java 1.5 book that actually has some Mac OS X support.

I'm using Eclipse as my IDE and the vanilla Java 1.4.2 that's native on OS X 10.3.9.

I'm stuck on what seems like a very basic example of using other java classes/objects as fields/variables withing a class. Here is the code that I copied from the book:
Code:
package net.javagarage;


/**
 * @author tommywillb
 * <br />
 * May 19, 2005
 * <br />
 * net.javagarage/PersonWithAddress.java
 * <br />
 */
public class PersonWithAddress {
	
	public String name;
	public int age;
	
	// Address is a class
	public Address address;

	public static void main(String[] args) {
		Person person = new Person();
		
		person.name = "Tommy W";
		person.age = 32;
		
		person.address = new Address();
		person.address.street = "1 Private Address";
		person.address.city = "San Francisco";
		person.address.state = "California";

		System.out.println("The person named " + person.name + " is aged " + person.age);
		System.out.println("\r\r" + person.name + " lives in " + person.address.city);
	}
	
}

class Address {
	public String street;
	public String city;
	public String state;
}
The error I am getting is this
Code:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	person.address cannot be resolved or is not a field
	person.address cannot be resolved or is not a field
	person.address cannot be resolved or is not a field
	person.address cannot be resolved or is not a field
	person.address cannot be resolved or is not a field

	at net.javagarage.PersonWithAddress.main(PersonWithAddress.java:26)

So I'm not sure if the book is wrong, or this is a Java 1.5 construct, or if I'm doing something boneheadedly stupid.

(The book insists that its okay to have both classes in the same .java/.class file... I tried it seperated, but I don't think I know how to make 2 classes talk [import?] to each other yet...)

Help?
 
You're trying to create a new Person class, yet there is no Person object declared anywhere. Change

Person person = new Person();

to

PersonWithAddress person = new PersonWithAddress();

That *should* resolve your initial issues.
 
Thanks chornebe!

d'uh! I named the class different from the book, but dindn't change the way I created the object of that class.

I think we can clearly classify this one in the "doing something boneheadedly stupid" category.

smiley_blush.gif
 
*smiles* Don't we all? Should have seen me struggling with a compile error on Friday. Turns out I was completely ignoring the compiler errors right there on screen thanks to a healthy dose of "Friday Disease". I'll finish the work on Monday that I could have fixed Friday. It's all good :)
 
Back
Top