paviflo
paviflo

Reputation: 125

NullPointerException in Android App

I'm trying to create an app that will draw with the AndroidPlot library the waveform of a WAV file stored in raw. Then, first of all what I do is to get the byte array of the waveform. As I guess it is stored in pairs of 2 bytes, I try to get the amplitude of each of them and I store it in an int array which will be later converted into a Number array. I jut want to try the first 100 samples of the byte array for the moment. This is the code that I have so far:

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Number[] n = new Number[51];
    int[] array = new int[51];

    try {
        byte [] payload = IOUtils.toByteArray(this.getResources().openRawResource(R.raw.radio));
        for (int i = 0; i <= 100; i+=2) {
            // convert byte pair to int
           int j=i/2; 
           int audioSample = (int) ((payload[i+1] & 0xff) << 8) | (payload[i] & 0xff)/ 32767;
            array[j]=audioSample;
            n[j] = (Number)array[j];
            j++;
        }
    } catch (NotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

     XYSeries series1 = new SimpleXYSeries(
                Arrays.asList(n),          // SimpleXYSeries takes a List so turn our array into a List
                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                "Series1");

    LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);


    // add a new series' to the xyplot:
    plot.addSeries(series1, series1Format);

    // reduce the number of range labels
    plot.setTicksPerRangeLabel(3);
    plot.getGraphWidget().setDomainLabelOrientation(-45);


}

When I try to compile the code I have a NullPointerException error. I guess is something wrong with tha arrays. Could you please help me? I am quite new on Java

02-06 18:45:30.946: E/AndroidRuntime(2642): FATAL EXCEPTION: main
02-06 18:45:30.946: E/AndroidRuntime(2642): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.representaaudio/com.example.representaaudio.MainActivity}: java.lang.NullPointerException
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.os.Looper.loop(Looper.java:137)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at java.lang.reflect.Method.invokeNative(Native Method)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at java.lang.reflect.Method.invoke(Method.java:511)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at dalvik.system.NativeStart.main(Native Method)
02-06 18:45:30.946: E/AndroidRuntime(2642): Caused by: java.lang.NullPointerException
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.util.TypedValue.applyDimension(TypedValue.java:327)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at com.androidplot.util.PixelUtils.dpToPix(PixelUtils.java:103)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at com.androidplot.xy.LineAndPointFormatter.a(LineAndPointFormatter.java:101)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at com.androidplot.xy.LineAndPointFormatter.<init>(LineAndPointFormatter.java:63)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at com.androidplot.xy.LineAndPointFormatter.<init>(LineAndPointFormatter.java:74)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at com.example.representaaudio.MainActivity.onCreate(MainActivity.java:76)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.app.Activity.performCreate(Activity.java:5104)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-06 18:45:30.946: E/AndroidRuntime(2642):     ... 11 more

Upvotes: 1

Views: 687

Answers (4)

Enes
Enes

Reputation: 2225

If you are using Plotstuff pixelUtils, you should call init method first

 /**
 * Recalculates scale value etc.  Should be called when an application starts or
 * whenever the screen is rotated.
 */
public static void init(Context ctx) {
    //DisplayMetrics dm = ctx.getResources().getDisplayMetrics();
    //SCALE = dm.density;
    //X_PIX = dm.widthPixels;
    //Y_PIX = dm.heightPixels;
    metrics = ctx.getResources().getDisplayMetrics();

}

Upvotes: 3

Laurent B
Laurent B

Reputation: 2220

Well you have your answer with the stacktrace, check your PixelUtils class or the LineAndPointFormatter, maybe you need to pass something instead of null in the constructor.

02-06 18:45:30.946: E/AndroidRuntime(2642): Caused by: java.lang.NullPointerException
02-06 18:45:30.946: E/AndroidRuntime(2642):     at android.util.TypedValue.applyDimension(TypedValue.java:327)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at com.androidplot.util.PixelUtils.dpToPix(PixelUtils.java:103)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at com.androidplot.xy.LineAndPointFormatter.a(LineAndPointFormatter.java:101)
02-06 18:45:30.946: E/AndroidRuntime(2642):     at com.androidplot.xy.LineAndPointFormatter.<init>(LineAndPointFormatter.java:63)

Upvotes: 1

Sam Dufel
Sam Dufel

Reputation: 17598

You're passing null to the constructor for LineAndPointFormatter, instead of an instance of PointLabelFormatter

LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);

Create a PointLabelFormatter and pass it in to resolve this error.

Upvotes: 1

PNS
PNS

Reputation: 19895

Based on the source code, one can only guess, but a possible cause seems to be the payload variable declaration

A simple approach to debug that part of the code would be to declare different variables for the output of getResources() and of openRawResource(). If any of these methods returns null, or if the value of the R.raw.radio parameter is null, then a NullPointerException will be thrown.

Upvotes: 0

Related Questions