I am getting this error when i use a string array
java.lang.NullPointerException
The code i used is as follows..
String[] list = null;
String sql = "SELECT * FROM pos_products";
ResultSet rs = mysql_query.execute_mysql(variables.con.conn, sql);
while (rs.next()) {
System.out.println(rs.getString("product_name"));
list[i] = rs.getString("product_name");
i++;
}
I have to add one more thing after seeign this replies.. First thank you guys for your response.. Now the problem is i am using JList and i have to add content to it.. If i use list i cant add directly. i can user either vector or a string array. what is your thoughts on that ??
Your list is
null, thusnull[i]throws NullPointerExceptionYou should consider either initialize it as ChrisJ describes or use a List
edit
This solves two parts, your
NullPointerExceptionand the dynamic fetch of the data.You mention you need to put this in a JList, in that case, you can still use this approach, but separate the data retrieval from the displaying code.
So, create a method list this:
And then call this method where you create your JList