Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filling a variable 1

Status
Not open for further replies.

Krolsky

Programmer
Apr 16, 2002
20
DE
Hi there. I'm trying to set a variable(e.g. seatcolor), that I generate in a class generateCar, in an object(e.g. seat) that itself is in another object(e.g. car).
The car object exists in an array with 200 cars. All filled with null.

The seat object has a setSeatColor() method and the car object has a getSeat(). As far as I know getSeat() will return a seat object, but how can I call setSeatColor() when the array exists in another class(e.g. CarList) than the class(generateCar) I'm trying to fill it with?

The plan is to make this thing distributed so I guess that could make it a bit more difficult.

Well I hope I was clear enough with my explanation, if not I'll be happy to answer any question on this thing.

Thanks in advance.
Krolsky
 
you shouldn't be able to set the seatColor if the car doesn't exist. so assuming you've got something, I think:

carArray[0].getSeat().setSeatColor(Color c);

should do the trick.
 
yeah the car object itself is already in the array. its just not filled yet.

I will try this out and also try some stuff similar to this. I guess I should have thought of this myself somehow...

thnx alot tho.

I'm also trying to get info out of the array now. So on one hand I set the variables and on the other hand I read em to display them.
I figured I could read it and compare to null. If its null the array location is not filled,if its not null the location is. So something like:
if (carArray[location].getSeat.getSeatColor()= null){
System.out.println("no car");
}
else{
myColor = carArray[location].getSeat.getSeatColor();
System.out.println("car with color: "+myColor);
}

does this sound reasonable? or am I trying to do rubbish? The getSeatColor() does exist just like setSeatColor().
 
rubbish? no.

While your way of going about it is good, you do have one mistake/typo in your code: (car = null) is different than (car == null).

But no worries, that's a mistake every beginner makes. The important thing is you're understanding how to use Java.
 
thnx alot for the compliment :):):):)

It's just kind of weird I keep getting a nullpointerexception. still didnt figure out why.

I work it out different than it says here because I want to be able to reuse variables(mebbe wrong name).
I worked it out like this:

mySeat = carArray[location].getSeat();
myColor = mySeat.getSeatColor();
myGarment = mySeat.getGarment();


now the problem i have with this is that I don't know what to put in getSeatColor().

well I guess i'll have to puzzle some more to get that right :)
 
do you mean by any chance how you model color? If so, try java.awt.Color. Be lazy; reuse stuff. It's a virtue!

NullPointers are a classic. cat.debug (if you're using log4j, else System.out.println) is your friend in those cases. good luck!
 
Hey folks!

I am just wondering if you can do something like:
myColor = carArray[location].Seat.getSeatColor();

Instead of:
myColor = carArray[location].getSeat.getSeatColor();

Why do I have to have a method that returns an object instead of calling the getSeatColor-method directly?

btw: I'm still a beginner so please be patient with me ;-)

bye

frag patrick.metz@epost.de
 
Look at access modifiers... the easiest way to do that is by declaring Seat as a public variable.

That is not always a good thing, since any object can then directly modify it. You might not like it if someone did this:

carArray[location].Seat = null;

or

carArray[location].Seat = anotherSeat;

spending time looking at those modifiers can help you create cleaner, safer code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top