Java Android应用程序中的NullPointerException

Java Android应用程序中的NullPointerException,java,nullpointerexception,bytearray,wav,Java,Nullpointerexception,Bytearray,Wav,我正在尝试创建一个应用程序,它将使用AndroidPlot库绘制存储在raw中的WAV文件的波形。 然后,首先我要做的是得到波形的字节数组。 我猜它是以两个字节成对存储的,我尝试获取每个字节的振幅,并将其存储在int数组中,该数组稍后将转换为数字数组。我暂时不想尝试字节数组的前100个样本。 这是我目前掌握的代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(

我正在尝试创建一个应用程序,它将使用AndroidPlot库绘制存储在raw中的WAV文件的波形。 然后,首先我要做的是得到波形的字节数组。 我猜它是以两个字节成对存储的,我尝试获取每个字节的振幅,并将其存储在int数组中,该数组稍后将转换为数字数组。我暂时不想尝试字节数组的前100个样本。 这是我目前掌握的代码:

    @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);


}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
编号[]n=新编号[51];
int[]数组=新的int[51];
试一试{
byte[]payload=IOUtils.toByteArray(this.getResources().openRawResource(R.raw.radio));

对于基于源代码的(inti=0;i,我们只能猜测,但一个可能的原因似乎是
有效负载
变量声明


调试这部分代码的一种简单方法是为
getResources()
openrawsource()的输出声明不同的变量
。如果这些方法中的任何一个返回
null
,或者
R.raw.radio
参数的值为
null
,则将抛出
NullPointerException

根据源代码,只能猜测,但可能的原因似乎是
有效负载
变量声明


调试这部分代码的一种简单方法是为
getResources()
openrawsource()的输出声明不同的变量
。如果这些方法中的任何一个返回
null
,或者
R.raw.radio
参数的值为
null
,则将抛出
NullPointerException

您将
null
传递给
LineAndPointFormatter
的构造函数,而不是
PointLabelFormatter的实例颂歌>

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

创建一个PointLabelFormatter并将其传入以解决此错误。

您正在将
null
传递给
LineAndPointFormatter
的构造函数,而不是
PointLabelFormatter

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

创建一个PointLabelFormatter并将其传入以解决此错误。

如果您有stacktrace的答案,请检查您的
PixelUtils
类或
LineAndPointFormatter
,可能您需要在构造函数中传递一些内容,而不是
null

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):由以下原因引起:java.lang.NullPointerException
02-06 18:45:30.946:E/AndroidRuntime(2642):在android.util.TypedValue.applyDimension(TypedValue.java:327)
02-06 18:45:30.946:E/AndroidRuntime(2642):在com.androidplot.util.PixelUtils.dpToPix(PixelUtils.java:103)
02-06 18:45:30.946:E/AndroidRuntime(2642):位于com.androidplot.xy.LineAndPointFormatter.a(LineAndPointFormatter.java:101)
02-06 18:45:30.946:E/AndroidRuntime(2642):位于com.androidplot.xy.LineAndPointFormatter.(LineAndPointFormatter.java:63)

如果你的答案是stacktrace,请检查你的
PixelUtils
类或
LineAndPointFormatter
,也许你需要在构造函数中传递一些东西,而不是
null

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):由以下原因引起:java.lang.NullPointerException
02-06 18:45:30.946:E/AndroidRuntime(2642):在android.util.TypedValue.applyDimension(TypedValue.java:327)
02-06 18:45:30.946:E/AndroidRuntime(2642):在com.androidplot.util.PixelUtils.dpToPix(PixelUtils.java:103)
02-06 18:45:30.946:E/AndroidRuntime(2642):位于com.androidplot.xy.LineAndPointFormatter.a(LineAndPointFormatter.java:101)
02-06 18:45:30.946:E/AndroidRuntime(2642):位于com.androidplot.xy.LineAndPointFormatter.(LineAndPointFormatter.java:63)

如果您使用的是Plotstuff pixelUtils,则应首先调用init方法

 /**
 * 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();

}

如果您使用的是Plotstuff pixelUtils,那么应该首先调用init方法

 /**
 * 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();

}

哪一行引发异常?我们可以看到所有错误消息吗?请提供stacktrace。您应该澄清引发异常的那一行。例如,它是您声明有效负载数组的那一行吗?它应该是字节[]有效载荷..但可能我做得不对,我只是一个全新的编程哪一行引发异常?我们能看到所有的错误消息吗?请提供stacktrace。你应该澄清引发异常的那一行。例如,你是在哪一行声明有效载荷数组的?它应该是字节[]有效载荷..但可能我做得不对,我只是个新手programming@paviflo-您收到的是相同的错误,还是只是收到了不同的空指针异常?请检查错误消息,确保行号引用的代码相同。@paviflo-您收到的是相同的错误,还是只是收到了不同的错误空指针异常?检查错误消息,确保行号引用的代码相同。