0

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

6 Answers 6

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

but i want the size to be dynamic.. i dont want to allocate too much space or too small space..
@Deepak have a look at my answer. You may be able to create the array after you know how big you need it to be.
@Deepak in that case you're using the wrong data structure. In Java arrays size is fixed and ChrisJ answer is the correct for your question ( NullPointerException ) . For an alternative see mine :) stackoverflow.com/questions/5098230/…
1

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 

2 Comments

but i got a problem with using list. i can only put in JList vector or array type data structure. i cant use list.. any suggestion..
Yeap, separate the db retrieval from the display and create a method ... let me put that in the answer... see my edit
1

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++;
}

Comments

1

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.

Comments

1

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"));

Comments

1

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"));  

    }

3 Comments

Using an ArrayList is inefficient when you have a large number of writes to it. It creates a new array one index larger each time you add a new element.
@Endophage true! changed to linkedlist
If you have a raw idea of how big the data wold be, you can specify the initial capacity to that. The OP mentions he'll use a JList so I don't think it would be that long

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.