Reputation: 14978
I am working on Java based COM program, when I send data in this format so it works:
serialPort.writeBytes( new byte[] { (byte)3, (byte)0, (byte)0,
(byte)0, (byte)4, (byte)5} );
but when I do following so it does not, where am I doing wrong?
byte[] bcode = null;
bcode[0] = (byte)3;
bcode[1] = (byte)0;
bcode[2] = (byte)0;
bcode[3] = (byte)0;
bcode[4] = (byte)4;
bcode[5] = (byte)5;
serialPort.writeBytes(bcode);
Upvotes: 0
Views: 656
Reputation: 178243
On the second example, you did not create your array, you assigned it null
. You can't reference an array element of an array that doesn't exist. You could do
byte[] bcode = new byte[6];
That will create your array with space for 6 bytes. Then assign your values individually.
Upvotes: 6