-2

I thought this will work, but sadly it doesn't. I get the error-

The method add(CustomerInfo) in the type ArrayList is not applicable for the arguments (String)

My aim is to return an Arraylist and make an access with the get Methods. When I'm using String for the Arraylist, I can not use arr.get(i).userID, ....FirstName ...

Class CustomerInfo.java

  public class CustomerInfo {
    private static Connection conn = null;
    private static ResultSet resultSet = null;
    public String UserID;
    public String FirstName;
    public String SecondName;


    public ArrayList<CustomerInfo> findCustomer (String userID) throws SQLException {

            conn = null;
            PreparedStatement pstmt = null;

            try {

                JDBCConnection jdbcConn = new JDBCConnection();
                conn = jdbcConn.openConnection();

                ArrayList<CustomerInfo> customerList  new ArrayList<CustomerInfo();

                String sql = "SELECT USERID FROM TAB0025 WHERE USERID = ?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, userID);

                resultSet = pstmt.executeQuery();


                while (resultSet.next()) {

                customerList.add(resultSet.getString("USERID"));
                customerList.add(resultSet.getString("FIRSTNAME"));
                customerList.add(resultSet.getString("SECONDNAME"));


                this.UserID = resultSet.getString("USERID");
                this.FirstName = resultSet.getString("FIRSTNAME");
                this.SecondName  resultSet.getString("SECONDNAME");

                }

                return customerList;

            } catch (Exception e) {
                throw e;
            }

            finally {
                conn.close();
            }


        public String getUserID() {
            return this.UserID;
        }

        public String getFirstname() {
            return this.FirstName;

        }

        public String getSecondName() {
            return this.SecondName;

        }

    }

Class InputReader.java

    // ...

    if (CustomerInfo.ExsistUserID(this.UserID)) {

                    CustomerInfo edit = new CustomerInfo();
                    ArrayList<CustomerInfo> arr = new ArrayList<CustomerInfo>();

                    arr = edit.findCustomer(this.UserID);


                    System.out.println("UserID: "+ arr.get(0).getUserID() + "  First Name: "arr.get(0).getFirstName() + " Second Name: " arr.get(0).getSecondName()); 

                } 
   // ...
codingenious
  • 8,385
  • 12
  • 60
  • 90
Panther
  • 15
  • 1
  • 6

1 Answers1

3

The error is in this three lines:

customerList.add(resultSet.getString("USERID"));
customerList.add(resultSet.getString("FIRSTNAME"));
customerList.add(resultSet.getString("SECONDNAME"));

As you can see above, resultSet.getString() method returns a String object but your ArrayList is container for objects of type CustomerInfo, so you need to create a new CustomerInfo object, populate its fields with the values from ResultSet and then add that object to your ArrayList like this:

custInfo = new CustomerInfo();
custInfo.UserID = resultSet.getString("USERID");
custInfo.FirstName = resultSet.getString("FIRSTNAME");
custInfo.SecondName = resultSet.getString("SECONDNAME");

customerList.add(custInfo);
Zlopez
  • 660
  • 1
  • 5
  • 11
  • Thanks, 0 Errors, finee ;-) – Panther Mar 13 '15 at 08:30
  • Also I suggest you move your `findCustomer()` to some other class. `CustomerInfo` is java bean to hold some state and any attempt to find an instance of that object should be outside that class, may be in some Service or DAO class – Arkantos Mar 13 '15 at 08:33