Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: (Resolved) Extracting data elements from a user-defined class stored in an ArrayList



Thanks everyone for your advice and patience.
I modified the parameter list on the printArrayList method to:
 
public static void printArrayList(ArrayList<Person> personArrayList)
 
and it worked fine.
 
Thanks again for your help.
 
 
Joe Broghamer
On Nov 9, 2007, at 8:11 PM, Joel Barnum wrote:

Your method:

public static void printArrayList(ArrayList personArrayList)

defines an ArrayList reference that is not typed (i.e., it's not a JDK 5 generic), so it treats each element as java.lang.Object.

You will either have to change it to:

public static void printArrayList(ArrayList<Person> personArrayList)

or will have to cast when you access the elements:

System.out.println ( ((Person)(personArrayList.get(0)).getFirstName() );

Joel

Joseph Broghamer wrote:
Greg,
I copied your code to a new project in NetBeans and it worked fine.
You confirmed the correctness of my syntax to extract a data field from an element in the ArrayList:
System.out.println ( personArrayList.get(0).getFirstName() );
However, your code differs from mine in that I have a separate class with methods to load and print the ArrayList.
My project is made up of three classes:
           DataInputDem    (GUI display)
           Person        (defines data fields for a person and set and set methods)
    PersonQueries  (manipulates the data for ArrayList and future MySQL)
 Method insertNewPerson in class PersonQueries loads the ArrayList from the object newPerson:
public static int printArrayList (Person newPerson, ArrayList personArrayList)
Method printArrayList in class PersonQueries prints the ArrayList:
public static void printArrayList(ArrayList personArrayList)
The statement “System.out.println ( personArrayList.get(0).getFirstName() );” works fine in the class DataInputDemo where the ArrayList is instantiated but not in the printArrayList method.  I don’t understand what is happing.  It appears that the ArrayList is being loaded correctly from the newPerson object in method insertNewPerson and that the ArrayList is being passed correctly to the method printArrayList, but none of the methods are found.  Below is a copy of the error message I am getting:
 Created dir: /Users/broghamerjt/MyJavaProjects/DataInputDemo/build/classes
Compiling 3 source files to /Users/broghamerjt/MyJavaProjects/DataInputDemo/build/classes
/Users/broghamerjt/MyJavaProjects/DataInputDemo/src/datainputdemo/PersonQueries.java:85: cannot find symbol
symbol  : method getFirstName()
location: class java.lang.Object
                               + personArrayList.get(0).getFirstName());
Note: /Users/broghamerjt/MyJavaProjects/DataInputDemo/src/datainputdemo/PersonQueries.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
BUILD FAILED (total time: 1 second)
Below is the source code for the PersonQueries class with error statement marked in read:
 /*
* PersonQueries.java
*
* Created on November 5, 2007, 11:29 PM
*
* This class accesses data structures
* to store records of type person
* in ArrayList
* and
* in MySQL Database (not yet implemented)
*
* Author:  Joseph T. Broghamer
*/
package datainputdemo;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class PersonQueries
{
   // No-arguments Constructor
   public PersonQueries()
   {
       System.out.println ("From class constructor in PersonQueries");
           } // end no-arguments constructor PersonQueries
              public static int insertNewPerson(Person newPerson, ArrayList personArrayList)
   {
   int insertresult = 0; // used to determine if adding object (record) to ArrayList worked
                         // not currently implemented
           System.out.println ("Printing out contents of parameter newPerson ");
       System.out.println ("to ensure object data fields are correctly populated \n");                         System.out.println ("   " + newPerson.getFirstName());
       System.out.println ("   " + newPerson.getLastName());
       System.out.println ("   " + newPerson.getMiddleInitial());
       System.out.println ("   " + newPerson.getTelephoneNumber());
       System.out.println ("   " + newPerson.getApartmentSuite());
       System.out.println ("   " + newPerson.getCityName());
       System.out.println ("   " + newPerson.getStateName());
       System.out.println ("   " + newPerson.getzipCodeName());
       System.out.println ();
            System.out.println ("From method insertNewPerson, "
                     + "adding newPerson to personArrayList \n");
       personArrayList.add(newPerson);
      System.out.println (" ArrayList index = " + personArrayList.indexOf(newPerson));
   System.out.println ();
      System.out.println (personArrayList.get(0));
   System.out.println ();
         //  Print contents of ArrayList to scrollTextArea of GUI
   //  Not currently implemented
         return insertresult;
     } // end method insertNewPerson
       public static void printArrayList(ArrayList personArrayList)
   {
               System.out.println (" From method printArraylist");
                System.out.println ("Value for personArrayList.get(0) = " + personArrayList.get(0));
                         System.out.println ("From class DataInputDemo, personArrayList.get(0).getFirstName()) = "
                               + personArrayList.get(0).getFirstName());
                 } // end method printArraylist
   } // end class PersonQueries
On Nov 9, 2007, at 12:42 AM, Greg Guerin wrote:
Joseph Broghamer wrote:

Using the NetBeans debugger I was able to confirm that the data does exist
in ArrayList element zero (0). However, when I try to extract a data
element from the class with a the following statement:

System.out.println (personArrayList.get(0).getFirstName());

I get the 'cannot find symbol' error during compile.

Please copy and paste the exact error you you get into an email, and post
it to the list.  There can be any number of different symbols that aren't
being found, and what isn't found tells a lot about what's wrong.
Paraphrasing errors is vague; accuracy matters.

The following isolated test-case works for me.  If it doesn't compile or
run correctly for you, please copy and paste any errors you get into an
email, and post to the list.

If the following works, then I suggest making your own test-case from your
original code, perhaps by removing things until it works, then adding
things back until it breaks.


import java.util.*;

public class Lister
{
public static void main( String[] args )
{
  ArrayList <Person> personArrayList = new ArrayList<Person>();

  Person newPerson = new Person( "Arnold", "Rimmer" );

  personArrayList.add( newPerson );

  System.out.println( "Value for personArrayList.get(0) = " +
personArrayList.get(0) );
  System.out.println( personArrayList.get(0).getFirstName() );
}
}

class Person
{
private String first, last;

public Person( String first, String last )
{  this.first = first;  this.last = last;  }

public String getFirstName()
{  return first;  }

public String getLastName()
{  return last;  }

public String toString()
{  return "Hi, I'm " + getFirstName() + " " + getLastName();  }
}

-- GG


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden <mailto:email@hidden>)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
------------------------------------------------------------------------
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden
This email sent to email@hidden


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden

References: 
 >Re: Extracting data elements from a user-defined class stored in an ArrayList (From: Greg Guerin <email@hidden>)
 >Re: Extracting data elements from a user-defined class stored in an ArrayList (From: Joseph Broghamer <email@hidden>)
 >Re: Extracting data elements from a user-defined class stored in an ArrayList (From: Joel Barnum <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.