Reputation: 1079
I am new to Java and trying to use a two dimensional byte[ ] type. I have created a 2D byte[]
array as shown bellow. But whenever I try to insert some data to it, it gives an error saying: NullPointerException
byte[][][] Requisition = new byte[10][][];
byte[] someinput = ("example").getBytes();
Requisition[0][0]=someinput;
System.out.println("Printing:" + Requisition[0][0].toString());
etc..
Data type I am inserting in to this 2D array is type byte[]
. Because what I am inserting is an encrypted data, these data will have the format of byte[ ] , not byte
I want Requisition
array to hold two inputs for one line. For example
Requisition[0][1]=someinput1;
Requisition[0][2]=someinput2;
This someinput1
I am wanting to insert is not type byte
. Data I am inserting will be byte[]
type.
What I mean byte[] is (sequence of bytes using the platform's default charset, storing the result into a new byte array.)
Upvotes: 1
Views: 1091
Reputation:
No, you didn't create a two dimension array. You created a three dimension array! -> byte[1][2][3]
If you want create a two dimension array, you need to declare the columns and cells of your table. That's only two values, not three like you did. For clarity look at this picture: http://www.php-kurs.info/grafik/xyz.png
A two dimension array you can declare and fill like that:
int rows = 10;
int cols = 10;
int[][] twoDimension = new int[rows][cols];
for (int i = 0; i < twoDimension.length; i++) {
for (int j = 0; j < twoDimension[i].length; j++) {
twoDimension[i][j] = // Value you want to fill!
}
}
That's an array of int values. If you want create a byte array, you need to change the declaration of the array.
byte[][] twoDimension = new byte[rows][cols];
Upvotes: 0
Reputation: 1104
It should be:
byte[][][] someinput = new byte[5][5][5];
Then you can insert data in to this array.
Upvotes: 1
Reputation: 13940
When you do arr[n][m]
you're accessing an element of whatever type arr
is. In your case this is a byte
. If you want to assign an array to a 2D array simply do:
Requisition[0] = someinput;
And...
byte[][] Requisition = new byte[10][];
byte[] someinput = ("example").getBytes();
Requisition[0]=someinput;//These insertion will happens in a loop later
tested with...
for (byte b : Requisition[0])
System.out.println(b);
Of course, this answer is based on the fact you're not actually dealing with a 2D array, you have a 3D array which is likely a result of erroneous insertions of an array into a single element. If not, apply this logic to any n dimensional array.
Upvotes: 0