Results 1 to 5 of 5
Thread: Getting objects from a list
- 03-13-2008, 01:27 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 3
- Rep Power
- 0
Getting objects from a list
I am very new to Java.
I have a List called siteList, which has about 65 objects of type Site. I have a for loop in which I wish to iterate through the List and call a function (test) from each Site object. I'm but quite sure how to do this. So far I have:
The compiler states that it cannot resolve symbol for test. Any suggestions?Java Code:for (int i=0;i<siteList.size();i++) { siteList.get(i).test(3,3); }
- 03-13-2008, 01:53 PM #2
Member
- Join Date
- Mar 2008
- Posts
- 7
- Rep Power
- 0
You have to downCast to your type because siteList.get(i) is Object you have to made it like that ((Your Object)siteList.get(i)).test(3,3);
your object is the name of this class, try it
- 03-13-2008, 01:54 PM #3
well, for starters...
will return an Object.... a generic object...Every class in Java is an Object and I can tell you this "test" is not a java object instance variable......Java Code:siteList.get(i)
It's specific to your class maybe...a particular class ...not a generic 'Object' class.
Do something like this
Cas the returned object to MyClass --- your particular class. and the call the in stance variable on the castJava Code:((MyClass)siteList.get(i)).test(3,3);
- 03-13-2008, 02:42 PM #4
Member
- Join Date
- Mar 2008
- Posts
- 3
- Rep Power
- 0
Need to add elements
Thanks for your replies, that works fine. My program now compiles but I have an issue when adding things to the list (siteList). My program basically reads in a line from a file and then splits it into tokens and stores these in an array temporarily. The values in this array (lineContents) are then written to an object constructor of type site. However, when doing this using the code below, I get a Null Pointer Exception. Let me know your thoughts.
Java Code:List siteList; while ((line = in.readLine()) != null){ //whilst there are lines in the file StringTokenizer st = new StringTokenizer(line, ","); while (st.hasMoreTokens()) { lineContents[tokenCount] = st.nextToken(); tokenCount = tokenCount + 1; } //add to the sites siteList.add(new site(lineContents[0],lineContents[1])); }
- 03-13-2008, 10:45 PM #5
Member
- Join Date
- Mar 2008
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
HashMap with objects
By otoro_java in forum New To JavaReplies: 2Last Post: 01-28-2008, 03:28 PM -
Array of Objects
By bluefloyd8 in forum New To JavaReplies: 5Last Post: 01-22-2008, 06:27 PM -
Objects and Classes
By Aleve in forum New To JavaReplies: 8Last Post: 12-31-2007, 08:05 AM -
Getting objects of a class
By ravian in forum New To JavaReplies: 1Last Post: 12-04-2007, 12:23 PM -
Help with Objects!
By Shorinhio in forum New To JavaReplies: 1Last Post: 07-10-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks