0
byteArray = new byte[10000];

-- some code here ----

byteBuffer.wrap(byteArray);
for (int i=0; byteBuffer.hasRemaining(); i++)
{
    shortArray[i] = byteBuffer.getShort();
    System.out.println(shortArray[i]);
}

The byteBuffer.hasRemaining() gets flagged with a NullPointerException although I have provided it with a backing array.

  • What is the problem here?
  • Berkay Turancı
    • 3,238
    • 4
    • 31
    • 44
    An SO User
    • 24,046
    • 31
    • 127
    • 207

    2 Answers2

    3

    Please, check how you initialize byteBuffer it should be something like this since wrap is a static method

    byte[] byteArray = new byte[10000];    
    ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
    
    Nikolay Kuznetsov
    • 9,217
    • 12
    • 53
    • 97
    1

    The code seems OK. I suspect this is (due to some bug) because byteBuffer variable = null

    Evgeniy Dorofeev
    • 129,181
    • 28
    • 195
    • 266