Deepak
Deepak

Reputation: 6802

Error in declaring string array

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 ??

Upvotes: 0

Views: 464

Answers (6)

Mauricio
Mauricio

Reputation: 5853

I recommend you to change the String Array for a String List (or Set) because in your example you don't know how many products will be:

List<String> list = new LinkedList<String>();  
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.add(rs.getString("product_name"));  

    }

Upvotes: 1

OscarRyz
OscarRyz

Reputation: 199205

Your list is null, thus null[i] throws NullPointerException

You should consider either initialize it as ChrisJ describes or use a List

List<String> list = new ArrayList<String>();
.... 
    list.add( rs.getString("product_name"));
....

edit

This solves two parts, your NullPointerException and 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:

 public String[] fetchOptions() { 
      List<String> list = new ArrayList<String>();
      ... db code here
      while .... etc. et 
          list.add( rs.getString("product_name"));
      ... 
      // convert it to String[] 

     return list.toArray( new String[list.size()] );
 }

And then call this method where you create your JList

String [] options = fetchOptions();
JList aList = ... use it normally 

Upvotes: 1

bconneen
bconneen

Reputation: 154

As already noted, you need to declare the Array using "new", but I'm guessing you didn't do this because you have no idea how large the Array should be. In this case, have you considered using ArrayList?

ArrayList<String> list = new ArrayList<String>();
...
list.add(rs.getString("product_name"));

Upvotes: 1

msarchet
msarchet

Reputation: 15232

list is currently null, so there is nothing to append the items to.

Try doing something like this instead

List<String> list = new ArrayList<String>;
String[] listArray = 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.add(rs.getString("product_name"));
i++;
}
listArray = list.toArray();

This will allow the size of your array to be created at the time you have read all of the items from the ResultSet.

Upvotes: 1

Endophage
Endophage

Reputation: 21463

You have to initialize your array list to a specific length. Does the ResultSet object have some function/attribute for the size of the set. Then you could do the following:

String sql = "SELECT * FROM pos_products";
ResultSet rs = mysql_query.execute_mysql(variables.con.conn, sql);
String[] list = new String[rs.size()];
while (rs.next()) 
{
    System.out.println(rs.getString("product_name"));
    list[i] = rs.getString("product_name");
    i++;
}

Upvotes: 1

ChrisJ
ChrisJ

Reputation: 5241

Your code declares an array of strings, but it does not create the array object. In other words, it does not allocate memory for storing the contents of the array.

You should write something like:

String[] list = new String[SOME_SIZE];

The new operator creates the array.

Upvotes: 4

Related Questions