[英]NullPointerException at int array in Java Project
I am creating a method which will take int n as a parameter, convert a previously initialized String into an integer array, add 1 to each index n times, create a new char array from each increment of the int array via casting, then convert all n char arrays into new strings and print them to the screen. 我正在创建一个将int n作为参数的方法,将先前初始化的String转换为整数数组,将每个索引加1 n次,通过转换从int数组的每个增量创建一个新的char数组,然后将所有将char数组转换为新的字符串并将其打印到屏幕上。
Netbeans throws a NullPointerException in my current project, and I'm not sure why. Netbeans在当前项目中抛出NullPointerException,我不确定为什么。 Could someone explain why this error is present and what I ought to do to fix it? 有人可以解释为什么出现此错误,我应该怎么做才能解决它?
Relevant Class: https://www.dropbox.com/s/gv02i1traulg8kp/StringShifter.java 相关类别: https : //www.dropbox.com/s/gv02i1traulg8kp/StringShifter.java
Class containing Main Method: https://www.dropbox.com/s/ymon96ovv4c2lnf/CodeBreaker.java 包含Main方法的类: https : //www.dropbox.com/s/ymon96ovv4c2lnf/CodeBreaker.java
Error: 错误:
Exception in thread "main" java.lang.NullPointerException 线程“主”中的异常java.lang.NullPointerException
at Project5.StringShifter.shift(StringShifter.java:35) 在Project5.StringShifter.shift(StringShifter.java:35)
at Project5.CodeBreaker.main(CodeBreaker.java:18) 在Project5.CodeBreaker.main(CodeBreaker.java:18)
Your problem is here: 您的问题在这里:
int[] a = null;
char[] b = null;
int r = 0;
for (int i = 0; i <= text.length(); i++) {
a[i] = text.charAt(i);
}
a is set to point to null, and then you try to assign a value to a[i] (which currently does not have a place in memory), which will give you a NullPointer. 将a设置为指向null,然后尝试为a [i](当前在内存中没有位置)分配一个值,这将为您提供NullPointer。
Look at your code: 查看您的代码:
int[] a = null;
char[] b = null;
int r = 0;
for (int i = 0; i <= text.length(); i++) {
a[i] = text.charAt(i);
}
a
is null - you've explicitly set it to null
. a
为null-您已将其显式设置为null
。 So 所以
a[i] = text.charAt(i);
will fail with a NullReferenceException
- it's bound to. 将失败,并绑定NullReferenceException
。 You'd need to initialze a
to refer to an array, eg 您需要初始化a
以引用数组,例如
int[] a = new int[text.length()];
You also need to change your upper bound - as when i
is text.length()
, text.charAt(i)
will throw an exception. 您还需要更改上限-因为当i
是text.length()
, text.charAt(i)
会引发异常。 So you want: 所以你要:
int[] a = new int[text.length()];
for (int i = 0; i < text.length(); i++) {
a[i] = text.charAt(i);
}
It's not clear why you want an int[]
at all though. 目前尚不清楚为什么要使用int[]
。 It would be simpler to use: 使用起来会更简单:
char[] chars = text.toCharArray();
for (int i = 0; i < text.length(); i++) {
chars[i] = (char) (chars[i] + 1);
}
Also: 也:
n
instead of 1 多次遍历循环没有意义-只需加n
而不是1 You define int[] a = null
and never initialize it. 您定义int[] a = null
且从不对其进行初始化。
When you try to access it in a[i] = text.charAt(i);
当您尝试以a[i] = text.charAt(i);
you get the NullPointerException
. 您得到NullPointerException
。
You can solve you issue adding a
initialization: 你能解决您发出加a
初始化:
int[] a = new int[text.length];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.