Click to See Complete Forum and Search --> : Bizzare ArrayList behavior in Java


sasKuatch
03-10-2004, 03:45 PM
Hi all,

I'm working on a database app for a mock video rental company as a school project.

There are objects that represent different items in inventory, and they are inherited from an abstract class. I'm using Bluej (http://www.bluej.org) as a dev environment. By using the debugger, I can verify that the inheritance structure works.

However, when I add these items to an ArrayList, I am no longer able to call some methods from these objects like I normally would be able to on their own.

For example:
for(int i=0;i<library.size();i++)
{
DVD tempDVD = (DVD)library.get(i);
return tempDVD.getRunTime();
}

where library is the ArrayList, and DVD is the class that represents a DVD object.

spits out an error like
./Library.java:110: cannot resolve symbol
symbol : method getRunTime ()
location: class Rental
return tempDVD.getRunTime();
^

This method works for sure when called on the object itself, but when I call it on the object in the ArrayList, it doesn't.

Any help or insight is greatly appreciated.

blackrax
03-11-2004, 07:08 PM
strange; well - have you checked that:
i) access privileges are appropriate?
ii) the output; when you call toString() on the presumed DVD class (which you extract from the arraylist), actually represents what you expect - ie; a DVD object?
iii) tempDVD is not referenced to another object after extraction?
iv) there's not another compatible class named DVD, not containing the called method?
v) you've given it some time off; frequently problems can be solved by drinking a beer (or just doing some else, in general, for a while).

if all else fails, submit some more code to the thread,
//blackrax

gamblor01
03-11-2004, 07:32 PM
Actually the first thing I noticed is that your code is what's bizarre. You have a for loop, and you get the first element in the arrayList, and make a DVD object. Fine. You might try making a new variable such as int runTime = tempDVD.getRunTime() and then just return runTime. However, I think more importantly, I question why you have this return statement in a for loop? Why not just return the first element of the ArrayList because that's all the for loop will do. After it executes the return for library[0] it will automatically break from your for loop, break from your current method, and return to the calling method anyway. Perhaps if you posted more code though it would make things clearer.

sasKuatch
03-11-2004, 07:39 PM
Thank you so much for the suggestions, but I'm afraid that I haven't progressed enough to actually make obscurity the culprit.

See, all I have really is a statement adding the DVD object to the ArrayList, and a test println statement that tries to access the object to make sure it works.

Something like this:

item = new DVD();
item.setTitle("some movie");
item.setRunTime(90);
item.setCustID(5893);

library.add(item); // position 0

System.out.println(item.getTitle()); //works fine
System.out.println(library.get(0).getTitle()); //creates compiler error

Calling library.get(0).length() works if the object is a string, but not with my object.

Again, I've verified in the debugger that the objects work, including all of the getter and setters. I also helped others with their projects hoping to gain some insight, and the method truly works. There must be something wrong with mine and I don't even know where to look.

Robert

blackrax
03-11-2004, 07:56 PM
Originally posted by gamblor01
I think more importantly, I question why you have this return statement in a for loop? Why not just return the first element of the ArrayList because that's all the for loop will do.

hehe - i completely missed that one ;)

sasKuatch
03-11-2004, 07:58 PM
gamblor01, yes indeed you are correct. It was actually a piece of code that had a println to show me the elements, but I never got it to work. The return makes more sense in the context of the program, even though it is incorrect.

I am attaching the project as a tar.bz2. However, it is far from finished so there may be quirky, pointless parts that are simply for testing. The relevant classes are Library as it manipulates the ArrayList, and Rental and DVD as those are the objects supposed to go into the ArrayList. There are decent comments as this is for a grade. The others do not work yet. My main goal is to get the getTitle(), getRunTime(), and getCoID() working for the DVD class.

Once again I thank you all kindly for taking time to help me.

NOTE: the file is renamed to have a ZIP extension due to the upload rules, please remove the .zip ending leaving just .tar.bz2

blackrax
03-11-2004, 08:02 PM
Originally posted by sasKuatch
item = new DVD();
item.setTitle("some movie");
item.setRunTime(90);
item.setCustID(5893);

library.add(item); // position 0

System.out.println(item.getTitle()); //works fine
System.out.println(library.get(0).getTitle()); //creates compiler error


ahh - you have to type cast it to a DVD object, ie


// as expected, you add an instance of DVD to the arraylist
library.add(item); // position 0

// works, since you are using the reference to that particular instance of DVD
System.out.println(item.getTitle()); //works fine

// TYPE CAST - in this case, you'd be calling Object.getTitle() - there's no such method in java.lang.Object, therefore:
//System.out.println(library.get(0).getTitle());

// try this
System.out.println( ((DVD)library.get(0)).getTitle() );


EDIT: i haven't checked this against your source code, but if it fails, i'll take a look at it.

sasKuatch
03-11-2004, 08:20 PM
Yes! Thank you so much, it works beautifully. I can now continue on the rest of it finally! I can't express my gratitude enough. I'd definitely buy you a drink.:D

Thank you

Robert

blackrax
03-11-2004, 10:37 PM
Originally posted by sasKuatch
Yes! Thank you so much, it works beautifully. I can now continue on the rest of it finally! I can't express my gratitude enough. I'd definitely buy you a drink.:D

Thank you

Robert

ah - who am i to say no to a drink. glad to be of assistance, however.

cheers,
//blackrax. feeling a bit like dr. zoidberg today.