-4

I am getting NullPointerException in the code.I want to handle it. In the following code i have specified the line in which the null pointer exception occurs.

 public static void mapPcm16bitLittleEndian2Float(final byte[] pcm16bitBytes,
            final int offsetInput,
            final float[] samples,
            final int offsetOutput,
            final int length) {
        if (pcm16bitBytes.length - offsetInput < 2 * length) {
            throw new IllegalArgumentException("Insufficient Samples to convert to floats");
        }
        if (samples.length - offsetOutput < length) {
            throw new IllegalArgumentException("Insufficient float buffer to convert the samples");// Getting null pointer exception here.
        }
        for (int i = 0; i < length; i++) {
            samples[offsetOutput + i] = ((pcm16bitBytes[offsetInput + 2 * i] & 0xff) | (pcm16bitBytes[offsetInput + 2 * i + 1] << 8)); // no & 0xff at the end to keep the sign
        }


and

 public boolean processData(final byte[] data,
            final int offset,
            final int len) {
        // converty raw bytes into float samples
        mapPcm16bitLittleEndian2Float(data, offset, rawData, 0, len / 2);// getting null pointer exception
        // encode the bitstream
        return processData(rawData, len / 2);
    }

Please help me getting out of this.

kenju
  • 5,866
  • 1
  • 41
  • 41

2 Answers2

1

One of the arrays you are referencing is null, hence referencing its length property will throw a NullPointerException.

You should check for both of your arrays being null (== null) to handle this Exception, and act accordingly (e.g. rethrow an IllegalArgumentException).

Mena
  • 47,782
  • 11
  • 87
  • 106
  • @ Mena Thanks for the above coding.it works.getting null pointer exception in public boolean processData(final float[] data, final int numSamples) { int numSamplesRequired = channels * frameSize; if (numSamples != numSamplesRequired) { throw new IllegalArgumentException("SpeexEncoder requires " + numSamplesRequired + " samples to process a Frame, not " + numSamples ); } // encode the bitstream if (channels==2) { Stereo.encode(bits, data, frameSize); } encoder.encode(bits, data); return true; } – – user3912813 Aug 07 '14 at 10:36
0

As suggested by Mena, null pointer check is a must.

Hope the below code snippet helps :

 public static void mapPcm16bitLittleEndian2Float(final byte[] pcm16bitBytes,
                final int offsetInput,
                final float[] samples,
                final int offsetOutput,
                final int length) {


            if(pcm16bitBytes != null && pcm16bitBytes.length >0 && samples != null && samples.length >0){
                System.out.println("If the arrays are null ,should not enter this block");
                if (pcm16bitBytes.length - offsetInput < 2 * length) {
                    throw new IllegalArgumentException("Insufficient Samples to convert to floats");
                }
                if (samples.length - offsetOutput < length) {
                    throw new IllegalArgumentException("Insufficient float buffer to convert the samples");// Getting null pointer exception here.
                }

                for (int i = 0; i < length; i++) {
                    samples[offsetOutput + i] = ((pcm16bitBytes[offsetInput + 2 * i] & 0xff) | (pcm16bitBytes[offsetInput + 2 * i + 1] << 8)); // no & 0xff at the end to keep the sign
                }


            }
        }

Regards,Madhu

Madhu Bose
  • 371
  • 4
  • 16
  • @ Madhu Bose Thanks for the above coding.it works.getting null pointer exception in public boolean processData(final float[] data, final int numSamples) { int numSamplesRequired = channels * frameSize; if (numSamples != numSamplesRequired) { throw new IllegalArgumentException("SpeexEncoder requires " + numSamplesRequired + " samples to process a Frame, not " + numSamples ); } // encode the bitstream if (channels==2) { Stereo.encode(bits, data, frameSize); } encoder.encode(bits, data); return true; } – user3912813 Aug 07 '14 at 10:34
  • Thanks user3912813, Am glad it helped :) .Request you to accept my answer . – Madhu Bose Aug 07 '14 at 11:04
  • @ Madhu Bose Please help me with previous coding.. Please – user3912813 Aug 07 '14 at 11:12
  • Not getting you ? previous coding?? – Madhu Bose Aug 07 '14 at 11:14
  • Are you taking about the below issue which you are facing?http://stackoverflow.com/questions/25181006/handling-arraystoreexception – Madhu Bose Aug 07 '14 at 11:16