Reputation: 2906
Guys i need some help here , im getting this error here at the code snippet below. here's the stack trace. i have a feeling that somebody's gonna say not much effort on my part. its ok i agree , give me a suggestion what should i do more and ill do it just that i've got a little stuck and not moving ahead so thought of posting it here.
i think i'm having a problem because i'm using a code that was used for writing an excel file when the size of content to write to excel was pre - known . what i actually need is some way to write to excel when the length of rows and collumns may vary .
Length of Array:2000
java.lang.NullPointerException
at org.temp2.cod2.WriteToExcel.setExcel(WriteToExcel.java:149) >> line 149 is last line of the code snippet below i.e. byte buf[] = s.getBytes();
here the code snippet
String data[][] = new String [2000][5];
System.out.println("Length of Array:"+data.length);
for(int j=0;j<colN.length;j++){
Label myLabel1=new Label(j,1,colVal[j]);
wst.addCell(myLabel1);
}
for(int i=2;i<=data.length+1;i++){
for(int j=0;j<colN.length;j++)
{
// Encrypt
encrypter.encrypt(new FileInputStream("C:\\Users\\abc\\Desktop\\Encrypted.txt"),new FileOutputStream("temp.txt"));
// Decrypt
ByteArrayOutputStream f = new ByteArrayOutputStream();
String s = data[i-2][j];
byte buf[] = s.getBytes();
Upvotes: 1
Views: 3894
Reputation: 11332
You never initialize the contents of the array. You will need to loop over the array and assign strings to the elements:
String[][] data = new String[2000][5];
for(int i = 0; i < data.length; i++) {
for(int j = 0; j < data[i].length; j++) {
data[i][j] = new String(...);
}
}
Upvotes: 0
Reputation: 3063
Hey rover, You have declared your array of array as
String data[][] = new String [2000][5];
and you are accessing the location [2000]. In the above array your index should always be less that 2000.
Upvotes: 0
Reputation: 570315
Looks like data[i-2][j]
is null
. Is it supposed to be non null
? Check how you are initializing it then because, currently, you're just declaring data[][]
to be a String[2000][5]
array, you're not initializing its content. So it's filled with nulls.
Upvotes: 1
Reputation: 13908
At what point do you initialize the data[][] array?
String data[][] = new String [2000][5];
allocates the space but it appears that you never put any Strings in the array.
Also I feel I should point out that string.getBytes() is not character-encoding safe. You should always specify the character encoding, such as UTF-8 or UTF-16. Remember, the characters in the string are not equivalent to bytes; getBytes() transforms the data.
Upvotes: 1
Reputation: 99694
Your reference to s
in s.getBytes
is probably null. So that means what is getting executed is something like
byte buf[] = null.getBytes();
Which doesn't make much sense. So what you want to do to correct this might be something like this
String s = data[i-2][j];
byte buf[];
if (s != null)
buf = s.getBytes();
else
buf = //What do you want the default behavior to be? maybe "".getBytes()?
Upvotes: 4
Reputation: 597046
Obviously data[i-2][j]
is null. You are not setting it anywhere, so it is null.
Upvotes: 2