Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Java
public static Customer[] /*String*/ getCustomerDetails()
     {
         try
          {
              ConnnectToDB.setConnection();
              Statement CustList = ConnnectToDB.connection.createStatement();;
//              query = "Select customer.customerId,user.firstName from customer, user where customer.userid = user.userid";
              query = "Select user.firstName from  user";
              CustList.executeQuery(query);

              resultSet = CustList.getResultSet();

                if (resultSet != null)
                {
                    while(resultSet.next())
                    {
                       customer[count] = new Customer();
//                     customer[count].setCustomerNumber (resultSet.getInt(1));
                       firstName = resultSet.getString(1) ;
                       customer[count].setFirstName(firstName);
                       count++;

                    }
                }
          }

         catch(SQLException err)
          {
              JOptionPane.showConfirmDialog(null,"could not get customer Details" + " " + err.getMessage());
          }

         return customer;
     }


i want this code to populate the customer array with the result obtain from the query. what its currently doing is its returning an array with null as the value.

please help me.
Thanks in advance
Posted
Updated 1-Nov-12 3:54am
v2

1 solution

I guess your query is not ok.
But that's something you will have to figure out with your DB.
The forming of the array looks fine to me.


Addition
Here is a little example code.
You should probably check if you can simply return the Vector. A Vector is often simpler to work with.
Faster would be a HashMap, e.g. if you need to pick certain values out o the given set of data.

Java
import java.util.Vector;

public class Example {
	private Integer[] getValues(){
	  Vector<Integer> oVector = new Vector<Integer>(); // init Vactor
	  oVector.add(new Integer(1)); // load Vector
	  oVector.add(new Integer(2)); 
	  oVector.add(new Integer(3)); 
	  // ... more fun ...
	  return oVector.toArray(new Integer[oVector.size()]); // return Array
	}	
	
	public static void main(String[] args) {
		Example oExample = new Example();
		Integer[] oArray = oExample.getValues();
		for (Integer oValue : oArray) {
			System.out.println("Value is " + oValue);
		}
	}
}
 
Share this answer
 
v2
Comments
Georges23 1-Nov-12 9:59am    
Thanks for your reply TorstenH but i did tested my query in the db and its working fine. its only when i use the array that i get a null pointer exception. meaning that the array is null. thank you ones again.
TorstenH. 1-Nov-12 10:05am    
have you debugged it? where is the null popping?
is the customer array initialized? You are initializing the value, but is the Array?
You could also use a Vector and convert that to an array later - in case you are getting more results than expected.
Georges23 1-Nov-12 12:13pm    
Yes i have debugged it and the null pointer is at this point "customer[count] = new Customer();".
TorstenH. 1-Nov-12 12:18pm    
Yeep, check the number of results you get - I bet you're getting more results than the Array can handle.
I recommend to switch to a more variable Vector.
Georges23 1-Nov-12 12:49pm    
thanks very much TortenH it worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900