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

[449 byte] By [subhashmedhia] at [2007-11-27 11:46:19]
# 1

Copy and paste the exact error message your get.

floundera at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 2

java.lang.NullPointerException

at ExcelWrite.Db2Excel.main(Db2Excel.java:74)

NOTE:Db2Excel.java:74 corresponds to the assignment statement.

Message was edited by:

subhashmedhi

subhashmedhia at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 3

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

}

srinivassa at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 4

I am using both 1 and 2 but let me try out your last suggestion. I shall let you know about it.

subhashmedhia at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 5

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

srinivassa at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 6

Have you initialised your array?

String[] arr; // declaration

arr = new String[value]; // intialisation

floundera at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 7

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?

subhashmedhia at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 8

Did you read reply #6?

floundera at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 9

pls paste the entire try block

srinivassa at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 10

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

srinivassa at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 11

Plz give your full program and error msg

Paddua at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 12

I think the problem mostly happened on your string array had not been initialized. Had you? liked this: String[ ] arr = new String[20];

smartlukea at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 13

Initialise u r array to length of fields in u r database .. that will solve u r NullPointer

AmitChalwade123456a at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 14

I have solved the problem. it was due to the String array not being initialized. It happens, sometimes. Thanks anyway.

subhashmedhia at 2007-7-29 18:06:49 > top of Java-index,Java Essentials,Java Programming...
# 15

u r most welcome!!!!!!!!!!!

AmitChalwade123456a at 2007-7-29 18:06:55 > top of Java-index,Java Essentials,Java Programming...