0

Given below code is giving null pointer exception at

public class Testing {
    public static final String HD_DATA = "STOCK,D1,D2,D3,D4,D5,D6,D7,D8,D9,D10";
    public static final String HD_DATA_1 = "1,10,20,30,40,50,60,70,80,90,100";
    public static final String HD_DATA_2 = "2,11,22,33,44,55,66,77,88,99,111";
    public static void main(String[] args) {
        String[] hdDataArray = HD_DATA.split(",");
        String[] hdDataArray1 = HD_DATA_1.split(",");
        String[] hdDataArray2 = HD_DATA_2.split(",");
        String[][] pram = new String[3][];   //[null, null, null]

        for(int i=0;i<3;i++) {
            for(int j=0;j<hdDataArray.length;j++) {
                if(i ==0) {
                    pram[i][j] = hdDataArray[j]; // Exception in thread "main" java.lang.NullPointerException
                }
                else if (i ==1){
                    pram[i][j] = hdDataArray1[j];
                } else if(i == 2) {
                    pram[i][j] = hdDataArray2[j];
                }
            }
        }   
        System.out.println(pram);
    }
}

Null pointer exception generate while trying to give but i would like to insert the data at the run time is there any way to we can insert data in array of arrays at runtime identify the size of arrays of array like jagged array

Sadina Khatun
  • 1,136
  • 2
  • 18
  • 27
  • please explain more what you want, it's not clear what you mean in ` insert the data at the run time `,.. – Filipp Voronov Oct 05 '16 at 21:22
  • 2
    Please... refer to http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it and http://stackoverflow.com/questions/1922677/nullpointerexception-when-creating-an-array-of-objects Those questions (especially the first) will show you the core of the issue and give you the tools you need to understand the exception, why it's coming up, and fix it this time and _for the future_ as well. – Tunaki Oct 05 '16 at 21:24
  • at run time assigne the arrays of array means new String[3][atRuntime] – Sadina Khatun Oct 05 '16 at 21:24
  • @SadinaKhatun, the arrays which you assign into the array of arrays can have any lenghtif you use `new String[3][]` and assign it in the way i wrote in my answer. – Filipp Voronov Oct 05 '16 at 21:29

3 Answers3

4

Because pram = [null, null, null] and thus pram[i] (array at i position in the array of arrays) is null when you access it via pram[i][j].

public class Testing {
    public static final String HD_DATA = "STOCK,D1,D2,D3,D4,D5,D6,D7,D8,D9,D10";
    public static final String HD_DATA_1 = "1,10,20,30,40,50,60,70,80,90,100";
    public static final String HD_DATA_2 = "2,11,22,33,44,55,66,77,88,99,111";

    public static void main(String[] args) {
        String[] hdDataArray = HD_DATA.split(",");
        String[] hdDataArray1 = HD_DATA_1.split(",");
        String[] hdDataArray2 = HD_DATA_2.split(",");
        String[][] pram = new String[3][];   //[null, null, null]

        pram[0] = hdDataArray;
        pram[1] = hdDataArray1;
        pram[2] = hdDataArray2;

        System.out.println(pram);
    }
}
Filipp Voronov
  • 4,077
  • 5
  • 25
  • 32
2

You can do this a couple of ways:

pram[0] = hdDataArray;
pram[1] = hdDataArray1;
pram[2] = hdDataArray2;

Or you can initialize pram directly:

String[][] pram = {
    hdDataArray,
    hdDataArray1,
    hdDataArray2
};
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
-1
String[][] pram = new String[][] { hdDataArray, hdDataArray1, hdDataArray2 };

try this. you can directly assign your arrays into the new array.

user3754008
  • 275
  • 2
  • 12
  • 5
    These sorts of answers are frowned upon; while you're technically right, a non-authoritative "try this" is discouraged as opposed to actually demonstrating *why* this answer is right. – Makoto Oct 05 '16 at 21:19