Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Norm

Thread: Accessing Object Array

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    259
    Rep Power
    10

    Default Accessing Object Array

    I am trying to access my object array and store records:

    Java Code:
    Object rowData[];
    			for (Member member : MemberList) {
    				// Return the data for the record in the object
    				//Object rowData[] = { member.getId(), member.getFname()};
    				rowData[] = { member.getId(), member.getFname()};
    				//System.out.println(member.getId());
    				System.out.println(rowData[0] + " " + rowData[1]+ "\n\n");
    			}
    The error that I get is that rowData can not be resolved to a type. When I uncomment the second line in the for loop, it works, but the first pair of records are written over.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Accessing Object Array

    rowData can not be resolved to a type
    The compiler is looking for a class named rowData and can not find it.

    What is the code on line 5 supposed to be doing?
    To assign a value to an element of an array, there needs to be an index value in the []s
    theArray[theIndex] = theValue;
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Accessing Object Array

    Lines 5 is not valid syntax.

    Do it like this:
    Java Code:
          Object [] foo; // or Object foo[]
          foo = new Object[] {10,20};
    
          // or like this
    
          Object [] bar1 = new Object[] {10,20};
          // or like this
    
          Object[] bar2 = {10,20};

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Accessing Object Array

    Why the array, why not just access the methods you want data from directly like on line 6?
    jim829 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Accessing Object Array

    Good question! I was thinking that too. But then I figured this might be part of a larger plan.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Accessing an object created in another class
    By kkid in forum New To Java
    Replies: 22
    Last Post: 03-12-2014, 12:44 AM
  2. Accessing Object methods
    By binary_matter in forum New To Java
    Replies: 2
    Last Post: 02-13-2013, 12:38 PM
  3. Accessing ModelAttribute Object in Javascript
    By ali_sakar in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-16-2012, 11:28 AM
  4. Accessing an object from within another object from the same type in Java
    By mhasgari in forum Threads and Synchronization
    Replies: 5
    Last Post: 06-20-2011, 04:21 PM
  5. Accessing GUI object methods
    By km0r3 in forum AWT / Swing
    Replies: 5
    Last Post: 10-06-2010, 01:39 AM

Posting Permissions

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