NullPointerException,其方法具有接口返回类型

时间:2014-03-08 23:27:17

标签: java interface nullpointerexception

我有一个像这样的界面 -

public interface IntSequence {
 int length();
 int get(int index);
 void set(int index, int value);
  /**
  * Returns a contiguous subsequence of size "size" which starts from
  * the index "index" and is backed by the sequence;
  * that is, changing it through {@link IntSequence#set(int, int)}
  * affects the original sequence as well.
  * @param index the starting position of the subsequence
  * @param size the subsequence size
  * @return a sequence of ints
  */
 IntSequence subSequence(int index, int size);
}

一个类实现它就像这样 -

import java.util.ArrayList;
public class IntArray implements IntSequence {

    int[] a;
    int use;
    ArrayList<Integer> valuelist1 = new ArrayList<>();

    public IntArray(int size) {
        a = new int[size];
    }

    @Override
    public int length() {
        return a.length;
    }

    @Override
    public int get(int index) {
        use = a[index];
        return use;
    }

    @Override
    public void set(int index, int value) {
        a[index] = value;
    }

    public IntArray(int index, int size, int[] array) {
        for(int i = index; i <= (size + index); i++)
        {
            if(i >= array.length)
                break;
            else
                valuelist1.add(array[i]);
        }
    }

    @Override
    public IntSequence subSequence(int index, int size) {
        IntSequence resultseq = new IntArray(index, size, a);
        return resultseq;
    }

    public static void main(String[] args) {
        IntSequence a = new IntArray(5);
        a.set(0, 0);
        a.set(1, 10);
        a.set(2, 20);
        a.set(3, 30);
        a.set(4, 40);


        System.out.println("Initial array");
        System.out.println("size: " + a.length());
        for (int i = 0; i < 5; ++i)
            System.out.println("a[" + i + "]: " + a.get(i));

        System.out.println("Creating subarray (2, 2)");
        IntSequence s = a.subSequence(2, 2);

        System.out.println("s.size: " + s.length());

        System.out.println("Multiplying subarray's last element");
        s.set(1, s.get(1) * 10);
        System.out.println("Subarray after modification:");
        for (int i = 0; i < s.length(); ++i)
            System.out.println("s[" + i + "]: " + s.get(i));

        System.out.println("Array after modification:");
        for (int i = 0; i < 5; ++i)
            System.out.println("a[" + i + "]: " + a.get(i));

        a.subSequence(0, 1).subSequence(0, 1).subSequence(0, 1).set(0, -10);
        System.out.println("First element changed to: " + a.get(0));
    }


}

问题 - 我的问题在于IntSequence subSequence(int index, int size)方法,因为我不允许更改subSequence的类型以及我为此创建的解决方案构造函数IntArray(int index, int size, int[] array),在subSequence中使用它并返回它返回一个null值,因此当我尝试运行程序时会得到一个NullPointerException。

感谢您查看我的问题!

2 个答案:

答案 0 :(得分:1)

你没有像第一个那样在第二个构造函数中初始化int[] a的值:

public IntArray(int size) {
    //int[] a is initialized
    a = new int[size];
}
public IntArray(int index, int size, int[] array) {
    //int[] a is never initialized
    for(int i = index; i <= (size + index); i++) {
        if(i >= array.length)
            break;
        else
            valuelist1.add(array[i]);
    }
}

我建议您只调用在第二个构造函数中初始化数组的构造函数:

public IntArray(int index, int size, int[] array) {
    //line below invokes constructor with 1 argument
    this(size);
    for(int i = index; i <= (size + index); i++) {
        if(i >= array.length)
            break;
        else
            valuelist1.add(array[i]);
    }
}

除了这个问题,你应该在你的第二个构造函数中添加int[] array的值int[] a(只是一个意见),并为你的变量使用更好的名称

答案 1 :(得分:0)

int [] a未初始化,如果在调用方法之前无法初始化变量,我建议使用覆盖可选参数的方法的重载版本,尽管取决于您有多少可选参数,这可能会变得混乱。

Java不支持默认参数