1

I am newbie to Java so if my question doesn't make sense then please suggest to improve it so that I get my question answered.

This is how, I am initializing arrays.

public static String[][] data = null;
String[] ReadValue= new String[3];
int p = 0;  

I am reading element of CSV file and trying to put in a JTable. Below is code to feed to two dimensional array from CSV file. It throws NullPointerException error when I try to assign value to two dimensional array.

In Line - data[p][i] = ReadValue[i].trim();

My code:

br = new BufferedReader(new FileReader(csvFile));

while ((line = br.readLine())!= null) {         
       ReadValue= line.split(csvSplitBy);

       for (int i = 0; i < ReadValue.length; i++){             
          data[p][i] = ReadValue[i].trim();
       // System.out.println(""+ReadValue[i].toString());
     }
   p++;  
 }

Error:

 java.lang.NullPointerException
 at com.srinar.graphicsTest.JtableTest.LoadCSVdata(JtableTest.java:82)
 JtableTest.java:82 : -     data[p][i] = ReadValue[i].trim();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Amit.D
  • 113
  • 1
  • 13
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Oct 12 '15 at 06:28

1 Answers1

1

You must initialize your array by choosing the number of rows and columns you wish to store in it.

For example :

public static String[][] data = new String[rowNum][colNum];
Eran
  • 387,369
  • 54
  • 702
  • 768