java - Error in declaring string array -


i getting error when use string array java.lang.nullpointerexception

the code used follows..

 string[] list = null;    string sql = "select * 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 add 1 more thing after seeign replies.. first thank guys response.. problem using jlist , have add content it.. if use list cant add directly. can user either vector or string array. thoughts on ??

your list null, null[i] throws nullpointerexception

you should consider either initialize chrisj describes or use list

list<string> list = new arraylist<string>(); ....      list.add( rs.getstring("product_name")); .... 

edit

this solves 2 parts, nullpointerexception , dynamic fetch of data.

you mention need put in jlist, in case, can still use approach, separate data retrieval displaying code.

so, create 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 string[]        return list.toarray( new string[list.size()] );  } 

and call method create jlist

string [] options = fetchoptions(); jlist alist = ... use  

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -