Tuesday, April 1, 2014

Accessing a method from another class




I think you may have a couple of issues.



I'm assuming that dancer is a List of Dancer objects?



If so, maybe think about renaming it to dancers, so it's a little clearer.




On line 9, you are adding a new Dancer object to dancer.



The problem is, you are doing it in an anonymous fashion, meaning that once you add the new Dancer, you no long have a reference to it to



be able to update its attributes (name, etc).




As for the problem you pointed out, the setName method is non-static, meaning that it is not a class method, but rather tied to a particular instance of a Dancer. You are calling the setName method on the Dancer CLASS, rather than on a particular dancer instance.




You probably want to do something like:






Java Code:






Dancer dancerToCreate = new Dancer();
dancerToCreate.setName(lineScanner.next());
//set other attributes if there are more




Then, do the equivalent of line 9, where you actually add the dancer to your list(assuming that 'dancer' was a list)





Java Code:






dancers.add(dancerToCreate);







No comments:

Post a Comment