Null Pointer Exception
Hi,
I am getting a null-pointer exception the reasons for which i am unable to figure out. The following is the fragment of code:
while(rs.next()){
arr[ i ] = rs.getString("UserName);
i++;
} where rs refers to a ResultSet, arr is a String array and the database table from which the ResultSet is obtained is having only a single field UserName.
Kindly help.
Message was edited by:
subhashmedhi
java.lang.NullPointerException
at ExcelWrite.Db2Excel.main(Db2Excel.java:74)
NOTE:Db2Excel.java:74 corresponds to the assignment statement.
Message was edited by:
subhashmedhi
while(rs.next()){
arr = rs.getString("UserName);
i++;
1.make sure that your database table contains records
2.you said that arr is a string array ,so you must use arr [i]
ie
while(rs.next()){
arr [i]= rs.getString("UserName");
i++;
3.if 1,2 does not fix the problem try the following
rs.first();
while(rs.next()){
arr [i]= rs.getString(0);
i++;
}
if it does not fix the problem
add the following code in the catch block
catch(Exception e)
{
e.printStackTrace();
}
probably from the stack trace you will be able to idenitfy what went wrong
Have you initialised your array?
String[] arr; // declaration
arr = new String[value]; // intialisation
The most funny part is that i am getting a null pointer exception even when i am first retrieving the ResultSet element in a String type variable and then assigning the String variable to the String array.
Code fargment:
while(rs.next()){
String t = rs.getString("UserName"); //or String t = rs.getString(0);
arr[ i ] = t;
i++;
}
why is it so?
you must understand that
the code
String t = rs.getString("UserName"); //or String t = rs.getString(0);
arr[ i ] = t;
is not different from (except that,you are assing twice,and usnig an extra variable which is not efficient)
arr[ i ] = rs.getString("UserName"); //or String t = rs.getString(0);