我正在为我正在上的一门计算机科学课程做作业,我试图在一行中声明3个String数组,但是它一直给我这个错误消息
java.lang.NullPointerException
我弄乱了一点,并通过更改
解决了问题。public static String[] offence,name,date = new String[8];
到
public static String[] offence,name = new String[8];
public static String[] date = new String[8];
为什么只有当我这样做时它才起作用?
PS,我是新手,所以请放轻松。
package pDatabaseApp;
import java.io.*;
public class PDatabaseMenu {
public static String[] offence,name = new String[8];
public static String[] date = new String[8];
public static void main(String[] args) throws IOException {
String line;
BufferedReader in;
in = new BufferedReader(new InputStreamReader(System.in));
boolean finnish = false, reenter = false;
while(finnish == false) {
int i;
for(i = 0; i <= 7; i++) {
System.out.println("enter a name");
name[i] = in.readLine();
System.out.println("enter a day");
String day = in.readLine();
System.out.println("enter a month");
String month = in.readLine();
System.out.println("enter a year");
String year = in.readLine();
date[i] = day + "/" + month + "/" + year;
String offenceEnter[] = new String[3];
String offenceType[] = {"Assault","Arson","Theft"};
int l;
for(l = 0; l <= 2; l++) {
System.out.println("is there offence " + offenceType[l]);
offenceEnter[l] = in.readLine();
if(offenceEnter[l] == "yes") {
offence[i] = offenceType[l];
}
}
}
}
in.close();
}
}
答案 0 :(得分:2)
记住代码
public static String[] offence,name,date = new String[8];
将仅初始化日期。因此,其他变量保持未初始化状态。结果,您得到了java.lang.NullPointerException
。
您需要类似
public static String[] offence= new String[8],name= new String[8],date = new String[8];
答案 1 :(得分:1)
按照上面的代码编写方式,数组攻击最终为null。如果要初始化所有三个变量,则应具有此变量。
public static String[] offence = new String[8],name = new String[8],date = new String[8];
答案 2 :(得分:0)
使用冒犯和姓名之间的逗号,
public static String[] offence,name = new String[8];
是以下操作的快捷方式:
public static String[] offence;
public static String[] name = new String[8];
这个问题非常密切相关: Initializing multiple variables to the same value in Java
答案 3 :(得分:0)
public static String[] offence,name,date = new String[8];
上面的代码暗示您将试图声明和初始化一个大小为8的String数组,并带有三个指向它的指针:犯罪,姓名和日期。对于可变对象(如数组),通常更有意义的是分别初始化每个对象。
话虽这么说,但我不确定为什么您的修改后的代码行得通,但是我认为它不会按照您希望的方式运行。我建议执行以下操作:
public static String[] offence, name, date;
offence = new String[8];
name = new String[8];
date = new String[8];
希望我能提供帮助!
答案 4 :(得分:0)
package pDatabaseApp;
import java.io.*;
public class PDatabaseMenu {
public static String[] offence = new String[8];
public static String[] name = new String[8];
public static String[] date = new String[8];
public static void main(String[] args) throws IOException {
String line;
BufferedReader in;
in = new BufferedReader(new InputStreamReader(System.in));
boolean finnish = false, reenter = false;
while(finnish == false) {
int i;
for(i = 0; i < 8; i++) {
System.out.println("enter a name");
name[i] = in.readLine();
System.out.println("enter a day");
String day = in.readLine();
System.out.println("enter a month");
String month = in.readLine();
System.out.println("enter a year");
String year = in.readLine();
date[i] = day + "/" + month + "/" + year;
String offenceEnter[] = new String[3];
String offenceType[] = {"Assault","Arson","Theft"};
int l;
for(l = 0; l < 3; l++) {
System.out.println("is there offence " + offenceType[l]);
offenceEnter[l] = in.readLine();
if(offenceEnter[l].Equals("yes"))//A for each is better but idk if you did that
{
offence[i] = offenceType[l];
}
}
}
}
in.close();
}
}