Reputation: 43
needs help on my problem, I need to combine 3 array into one multidimensional array. but I always encounter error which some value return null. the data in first array (String[][]dataset) was:
001200700001 70 70 70 55 70
001200700002 70 70 60 60 60
001200700003 85 85 85 60 85
001200700004 60 70 85 70 85
001200700005 70 60 60 60 85
data for String[][]predictiondata was:
15 74.25 64.25 64.25 64.25 89.25
14 74.25 64.25 64.25 64.25 89.25
and for Means[] was:
String[]Means={"Means","0","0","0","0","0"};
so here's my code so far and the error output:
public void hashtree(String[][]dataset, String[][]predictiondata){
//======fetch data from database then add 1 morre row for calculation
String[]Means={"Means","0","0","0","0","0"};
int heightofarray = (dataset.length)+(predictiondata.length)+(Means.length);
int lengthofarray=(dataset[0].length);
System.out.println("heightofarray: "+heightofarray+" lengthofarray: "+lengthofarray);
String[][]toCalculate = new String[heightofarray][lengthofarray];
for(int a=0;a<dataset.length;a++){
for(int b=0;b<dataset[0].length;b++){
toCalculate[a][b]=dataset[a][b];
}
}
for(int a=0;a<predictiondata.length;a++){
for(int b=0;b<predictiondata[0].length;b++){
toCalculate[(dataset.length)+a][b]=predictiondata[a][b];
}
}
for(int a=0;a<Means.length;a++){
toCalculate[heightofarray-1][a]=Means[a];
}
System.out.println("should print String[][]toCalculate");
for(int a=0;a<toCalculate.length;a++){
for(int b=0;b<toCalculate[0].length;b++){
System.out.print(toCalculate[a][b]+"\t");
}System.out.println("");
}
The order for the new combined array ( String[][]toCalculate ) is:
String[][]dataset
String[][]predictiondata
String[]Means
but however I always get the output
System.out.println("should print String[][]toCalculate");
001200700001 70 70 70 55 70
001200700002 70 70 60 60 60
001200700003 85 85 85 60 85
001200700004 60 70 85 70 85
001200700005 70 60 60 60 85
15 74.25 64.25 64.25 64.25 89.25
14 74.25 64.25 64.25 64.25 89.25
null null null null null null
null null null null null null
null null null null null null
Exception in thread "main" java.lang.NullPointerException
null null null null null null
null null null null null null
Means 0 0 0 0 0
Thank you very much for the solving
Upvotes: 0
Views: 63
Reputation: 48330
It will probably be helpful for you to learn some debugging techniques, so you can solve problems like this when you see them in the future.
To that end, here's one way to go about debugging:
Looking at the output, you can see that the dataset
and the predictiondata
are stored correctly. It's the Means
that are stored in the wrong place.
The for
loop that stores the Means
is different from the others. The first two loops calculate the first available row in toCalculate
by adding the number of rows that have already been populated. But the last one counts backwards from the end. That must be the problem.
The size of toCalculate
is given by
int heightofarray = (dataset.length)+(predictiondata.length)+(Means.length);
The Means
are stored exactly Means.length - 1
rows beyond the proper location. That's because heightofarray
is calculated incorrectly as the sum of the height of the dataset
array, the height of the predictiondata
array, and the length of the Means
.
The correct calculation is
int heightofarray = dataset.length + predictiondata.length + 1;
Hope that helps!
Upvotes: 1
Reputation: 22656
int heightofarray = (dataset.length)+(predictiondata.length)+(Means.length);
This should be:
int heightofarray = (dataset.length)+(predictiondata.length)+(1);
The height of Means is 1
Upvotes: 3