gpt4 book ai didi

java - 模运算符空指针异常

转载 作者:行者123 更新时间:2023-11-29 05:05:34 25 4
gpt4 key购买 nike

这是我在 Android Studio 中的 java fragment 。

public static void makepack(Card[] pack, int[] a, int[] size) {

for (int i = 1; i < 14; i++) {
Random r = new Random();
int Low = 1;
int High = 53;
int k = r.nextInt(High - Low) + Low;

// 1st 13 are diamonds, then hearts, then clubs and then spades
if (!inarray(a, k, size[0])) {
pack[i - 1].num = (k % 13) + 1;
pack[i - 1].type = (k / 13) + 1;
a[size[0]] = k;
size[0]++;
} else {
i--;
}
}

}

我收到以下错误:

"C:\Program Files\Java\jdk1.8.0_05\bin\java" -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:\Program Files\Android\Android Studio\bin" -Dfile.encoding=windows-1252 -classpath "C:\Users\nhar\AppData\Local\Android\sdk\platforms\android-21\android.jar;C:\Users\nhar\AppData\Local\Android\sdk\platforms\android-21\data\res;C:\Users\nhar\AndroidStudioProjects\MindicoatCardGame\app\build\intermediates\classes\debug;C:\Users\nhar\AppData\Local\Android\sdk\extras\android\m2repository\com\android\support\support-annotations\22.1.1\support-annotations-22.1.1.jar;C:\Users\nhar\AndroidStudioProjects\MindicoatCardGame\app\build\intermediates\exploded-aar\com.android.support\support-v4\22.1.1\jars\libs\internal_impl-22.1.1.jar;C:\Users\nhar\AndroidStudioProjects\MindicoatCardGame\app\build\intermediates\exploded-aar\com.android.support\support-v4\22.1.1\res;C:\Users\nhar\AndroidStudioProjects\MindicoatCardGame\app\build\intermediates\exploded-aar\com.android.support\support-v4\22.1.1\jars\classes.jar;C:\Users\nhar\AndroidStudioProjects\MindicoatCardGame\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\22.1.1\res;C:\Users\nhar\AndroidStudioProjects\MindicoatCardGame\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\22.1.1\jars\classes.jar;C:\Program Files\Android\Android Studio\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.example.nihar.mindicoatcardgame.MainFunction
Exception in thread "main" java.lang.NullPointerException
at com.example.nihar.mindicoatcardgame.MainFunction.makepack(MainFunction.java:222)
at com.example.nihar.mindicoatcardgame.MainFunction.main(MainFunction.java:244)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

进程结束,退出代码为 1异常出现在 (%13) 行。知道我做错了什么吗?

最佳答案

唯一可能导致该行出现 NPE 的情况是 pack[i - 1] 是否为 null。

这就是您需要解决的问题。模数没问题。

关于您的评论,您可能想要这个...

if (!inarray(a, k, size[0])) {
pack[i - 1] = new Card();
pack[i - 1].num = (k % 13) + 1;
pack[i - 1].type = (k / 13) + 1;
a[size[0]] = k;
size[0]++;
} else {
i--;
}

更好的方法是为 Card 提供一个采用 num 和 type 的构造函数,然后您可以将三个 pack[i - 1] 行更改为一个。

关于java - 模运算符空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30511061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com