简体   繁体   English

将字符串转换为int数组时,为什么会得到空指针?

[英]why am i getting a null pointer when converting string to int array?

My main method: 我的主要方法:

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());
}

LargeInteger class: 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();
    }
}

You did not instantiate your array. 您没有实例化数组。 You need something like: 您需要类似:

   private int[] intArray = new int[SIZE];

where size is the length of your array. 其中size是数组的长度。

private int[] intArray;

Member variables are null by default, so you need to initialize this. 成员变量默认情况下为null ,因此您需要对其进行初始化。

Most likely you want it the same size as your string: 您最有可能希望它的大小与字符串相同:

public LargeInteger(String s) {
    intArray = new int[s.length()]; // Create the actual array before you try to put anything in it
    for (int i = 0; i < s.length(); i++) {
        intArray[i] = Character.digit(s.charAt(i), 10); // in base 10
    }
}

Or you should use a container that resizes itself, like ArrayList . 或者,您应该使用可自行调整大小的容器,例如ArrayList

You are not initialize the array intArray, that way you are getting error, here is the complete program 您没有初始化数组intArray,那样您会出错,这是完整的程序

import java.util.Scanner;

class  TestForNull {

    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());
    }

}

and this is LargeInteger 这是LargeInteger

public class LargeInteger {

    private int[] intArray;
    //convert the strings to array
    public LargeInteger(String s) {
        intArray = new int[s.length()];
        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();
        }   
}

Diferent approach through Integer.parseInt Integer.parseInt("yourInt"); 通过Integer.parseInt的不同方法Integer.parseInt(“ yourInt”);

To achieve your goal: 为了实现您的目标:

String a = "12345667788" //sample
String b = "";
int [] vecInt = new int[a.length()];  // The lack of initialization was your mistake as the above stated
for(int i=0; i< a.length(); i++)
{
   b = a.substring(0,1);
   a= a.substring(1);
   vecInt[i] = Integer.parseInt(b);
}

Please be aware of Double, long have far higher range then Integer which might be enough in your case to avoid an array! 请注意Double,长距离的范围远大于Integer,在您的情况下,这足以避免数组!

you forgot to initialize the array intArray 你忘了初始化数组intArray

I would recommend to use a java.util.List 我建议使用java.util.List

You forgot to initialize the array. 您忘记了初始化数组。 You have written it in constructor and the variables declared in method or constructor needs to be initialize at the same time. 您已经在构造函数中编写了它,并且在方法或构造函数中声明的变量需要同时初始化。

Note : Implementing your logic in Constructor is not recommended unless and until you dont have any other choice. 注意:除非您没有其他选择,否则不建议在Constructor中实现逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不知道为什么在Java中将2D数组转换为1D数组时为什么会出现空指针异常 - Not sure why I am getting a null pointer exception when converting 2D array to 1D array in Java 为什么会出现空指针异常 - Why am I getting a null pointer exception 为什么我会收到Null指针异常? - why am i getting Null Pointer Exception? 为什么当我尝试访问我的字符串数组时会得到“null”? - Why am I getting 'null' when I try to access my String Array? 向 ArrayList 添加项目时,为什么会出现空指针异常 - Why am I getting null pointer exception when I add an item to ArrayList 为什么在检索 Bundle 参数时会出现空指针异常? - Why am I Getting a Null Pointer Exception When I Retrieve Bundle Arguments? 从Firebase读取用户数据时,为什么会出现Null指针异常? - Why am I getting a Null Pointer Exception when reading user data from Firebase? 为什么我得到空指针异常,即使println有值显示? - Why I am getting null pointer exception even though there is value show when println? 将字符串的ArrayList从一个活动传递到另一个活动时,为什么会出现空指针异常? - Why am I getting a null pointer exception when passing an ArrayList of strings from one activity to another? 为什么在Java中使用Map时会出现空指针异常? - Why am i getting null pointer exception when using map in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM