I have a method that is supposed to return a ByteBuffer. The meat of the method does the following:
if (true) {
//code that puts some data into ByteBuffer bb
return bb
}
else {
//should not be writing any data to bb
//intention: leave bb as is
return null;
}
After calling this method and putting the necessary data into the ByteBuffer, I turn it into a byte array by first calling bb.flip()
. However, if the last iteration through the if-else loop happened to be false, the last data inputted was null
and I get the java.lang.NullPointerException
error.
Allocating zero capacity ByteBuffer
According to the page above, I'm supposed to return a zero-length ByteBuffer, but I'm not sure how to return something like that.
Any ideas on how to return an empty byte buffer?