Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-13-2008, 02:27 PM
Member
 
Join Date: Mar 2008
Posts: 3
markyoung1984 is on a distinguished road
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:

Code:
for (int i=0;i<siteList.size();i++) { siteList.get(i).test(3,3); }
The compiler states that it cannot resolve symbol for test. Any suggestions?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-13-2008, 02:53 PM
Member
 
Join Date: Mar 2008
Posts: 7
saint_jorjo is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-13-2008, 02:54 PM
aibtus's Avatar
Member
 
Join Date: Mar 2008
Location: Randburg, South Africa
Posts: 9
aibtus is on a distinguished road
Send a message via Yahoo to aibtus Send a message via Skype™ to aibtus
Quote:
Originally Posted by markyoung1984 View Post
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:

Code:
for (int i=0;i<siteList.size();i++) { siteList.get(i).test(3,3); }
The compiler states that it cannot resolve symbol for test. Any suggestions?
well, for starters...
Code:
siteList.get(i)
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......

It's specific to your class maybe...a particular class ...not a generic 'Object' class.
Do something like this
Code:
((MyClass)siteList.get(i)).test(3,3);
Cas the returned object to MyClass --- your particular class. and the call the in stance variable on the cast
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-13-2008, 03:42 PM
Member
 
Join Date: Mar 2008
Posts: 3
markyoung1984 is on a distinguished road
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.

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])); }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-13-2008, 11:45 PM
Member
 
Join Date: Mar 2008
Posts: 7
saint_jorjo is on a distinguished road
i need all source of the program
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HashMap with objects otoro_java New To Java 2 01-28-2008 04:28 PM
Array of Objects bluefloyd8 New To Java 5 01-22-2008 07:27 PM
Objects and Classes Aleve New To Java 8 12-31-2007 09:05 AM
Getting objects of a class ravian New To Java 1 12-04-2007 01:23 PM
Help with Objects! Shorinhio New To Java 1 07-10-2007 10:32 PM


All times are GMT +3. The time now is 08:04 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org