Results 1 to 5 of 5
  1. #1
    markyoung1984 is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Question 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:

    Java 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?

  2. #2
    saint_jorjo is offline Member
    Join Date
    Mar 2008
    Posts
    7
    Rep Power
    0

    Default

    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

  3. #3
    aibtus's Avatar
    aibtus is offline Member
    Join Date
    Mar 2008
    Location
    Randburg, South Africa
    Posts
    9
    Rep Power
    0

    Wink

    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:

    Java 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...
    Java 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
    Java 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

  4. #4
    markyoung1984 is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default 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]));
    }

  5. #5
    saint_jorjo is offline Member
    Join Date
    Mar 2008
    Posts
    7
    Rep Power
    0

Similar Threads

  1. HashMap with objects
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 03:28 PM
  2. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 06:27 PM
  3. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 08:05 AM
  4. Getting objects of a class
    By ravian in forum New To Java
    Replies: 1
    Last Post: 12-04-2007, 12:23 PM
  5. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •