[英]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.