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,373
    • 4
    • 32
    • 45
    An SO User
    • 24,612
    • 35
    • 133
    • 221

    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,467
    • 12
    • 55
    • 101
    1

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

    Evgeniy Dorofeev
    • 133,369
    • 30
    • 199
    • 275