为什么我在将字符串转换为int数组时得到一个空指针?

Sac*_*ing 1 java

我的主要方法:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String string1;
    string1 = input.next();

    LargeInteger firstInt = new LargeInteger(string1);

    System.out.printf("First integer: %s \n", firstInt.display());
}
Run Code Online (Sandbox Code Playgroud)

LargeInteger类:

public class LargeInteger {

    private int[] intArray;

    //convert the strings to array
    public LargeInteger(String s) {
        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10); // in base 10
        }
    }

    //display the strings
    public String display() {
        String result = "";

        for (int i = 0; i < intArray.length; i++) {
            result += intArray[i];
        }
        return result.toString();
    }
}
Run Code Online (Sandbox Code Playgroud)

Vin*_*nie 7

您没有实例化您的数组.你需要这样的东西:

   private int[] intArray = new int[SIZE];
Run Code Online (Sandbox Code Playgroud)

其中size是数组的长度.