Java 空指针错误数组缩短方法

Java 空指针错误数组缩短方法,java,exception,pointers,null,Java,Exception,Pointers,Null,我有下面的代码,通过将元素复制到一个新数组并跳过每一个元素来缩短一个数组。然而,我一直得到一个空指针异常错误 public void shorten() { // put your code here if( samples.length % 2 == 0){ double [] temp = new double[samples.length / 2]; } else if( samples.length % 2 != 0){ d

我有下面的代码,通过将元素复制到一个新数组并跳过每一个元素来缩短一个数组。然而,我一直得到一个空指针异常错误

public void shorten()
{
    // put your code here
    if( samples.length % 2 == 0){
        double [] temp = new double[samples.length / 2];
    }
    else if( samples.length % 2 != 0){
        double [] temp = new double[samples.length / 2 - 1];
    }

    Arrays.fill(temp, 1.0);
    int j = 0;
    for(int i=0; i<= temp.length; i++){
        temp[i] = samples[j];

        j = j + 2;

    }
    samples = temp;
}
public void shorten()
{
//把你的代码放在这里
if(samples.length%2==0){
双精度[]温度=新双精度[samples.length/2];
}
else if(samples.length%2!=0){
双精度[]温度=新双精度[samples.length/2-1];
}
阵列填充(温度,1.0);
int j=0;

对于(inti=0;i,除了空指针之外,还有另一个错误


i除了空指针之外,还有一个错误


i此代码的每个块:

if( samples.length % 2 == 0){
    double [] temp = new double[samples.length / 2];
}
else if( samples.length % 2 != 0){
    double [] temp = new double[samples.length / 2 - 1];
}
定义一个只有一行作用域的
temp
变量(隐藏这些行的
temp
类变量(我假设您有)并保持不变)

如果调用函数时,
temp
类变量是
null
,那么在这些行之后,它仍然是
null
。您需要类似以下内容:

if( samples.length % 2 == 0){
    temp = new double[samples.length / 2];
}
else { // samples.length % 2 != 0 is implied, since it's else
    temp = new double[samples.length / 2 + 1]; // corrected -1 to +1
}
我删除了声明新变量的
temp
之前的
double[]


此外,for循环检查需要是
i
,而不是
此代码的每个块:

if( samples.length % 2 == 0){
    double [] temp = new double[samples.length / 2];
}
else if( samples.length % 2 != 0){
    double [] temp = new double[samples.length / 2 - 1];
}
定义一个只有一行作用域的
temp
变量(隐藏这些行的
temp
类变量(我假设您有)并保持不变)

如果调用函数时,
temp
类变量是
null
,那么在这些行之后,它仍然是
null
。您需要类似以下内容:

if( samples.length % 2 == 0){
    temp = new double[samples.length / 2];
}
else { // samples.length % 2 != 0 is implied, since it's else
    temp = new double[samples.length / 2 + 1]; // corrected -1 to +1
}
我删除了声明新变量的
temp
之前的
double[]


此外,for循环检查需要是
i
,而不是
试试这个:我在需要的地方更改了代码

public void shorten()
{
    // put your code here
    double [] temp=null; // here I declare temp Array
    if( samples.length % 2 == 0){
        temp = new double[samples.length / 2];
    }
    else if( samples.length % 2 != 0){
         temp = new double[samples.length / 2 - 1];
    }

    Arrays.fill(temp, 1.0);
    int j = 0;
    for(int i=0; i< temp.length; i++){// here I removed "=" because Array index starts from 0 to length-1. 
        temp[i] = samples[j];

        j = j + 2;

    }
    samples = temp;
}
public void shorten()
{
//把你的代码放在这里
double[]temp=null;//这里我声明temp数组
if(samples.length%2==0){
温度=新的双精度[样本长度/2];
}
else if(samples.length%2!=0){
温度=新的双精度[samples.length/2-1];
}
阵列填充(温度,1.0);
int j=0;
对于(inti=0;i
试试这个:我在需要的地方更改了你的代码

public void shorten()
{
    // put your code here
    double [] temp=null; // here I declare temp Array
    if( samples.length % 2 == 0){
        temp = new double[samples.length / 2];
    }
    else if( samples.length % 2 != 0){
         temp = new double[samples.length / 2 - 1];
    }

    Arrays.fill(temp, 1.0);
    int j = 0;
    for(int i=0; i< temp.length; i++){// here I removed "=" because Array index starts from 0 to length-1. 
        temp[i] = samples[j];

        j = j + 2;

    }
    samples = temp;
}
public void shorten()
{
//把你的代码放在这里
double[]temp=null;//这里我声明temp数组
if(samples.length%2==0){
温度=新的双精度[样本长度/2];
}
else if(samples.length%2!=0){
温度=新的双精度[samples.length/2-1];
}
阵列填充(温度,1.0);
int j=0;
对于(inti=0;i
NPE在哪里?我不确定这将如何编译,因为在if/else语句中定义了temp。您是否在某个地方定义了另一个temp变量?您需要发布完整的异常跟踪。无论如何,
samples
可能为空。您从哪一行获得异常?我们不知道您指的是哪一个temp您的代码中的一个在if块外不可见。NPE在哪里?我不确定这将如何编译,因为在if/else语句中定义了temp。您是否在某个地方定义了另一个temp变量?您需要发布完整的异常跟踪。无论如何,
samples
可能为空。您从哪一行获得异常?我们会不知道你指的是哪种温度。如果block.woudl给出一个NPE,那么代码中的一个在外面是不可见的?我认为会是arrayindexoutofbounds我认为会是arrayindexoutofbounds我认为,
samples.length/2-1
应该是a+1。另外,
samples.length/2-1
应该是请改为+1。