有人可以帮我吗?我是Java编程的新手,很长时间以来一直陷在错误中
这是错误消息: java.lang.ExceptionInInitializerError
由以下原因引起:java.lang.RuntimeException:无法编译的源代码-需要标识符
at pkg18148600a_assignment1.Main.<clinit>(Main.java:12)
线程“ main” Java中的异常结果:1
这是我的代码
public static void main(String[] args) {
Member member[] = new Member[6];
int sequence[] = {0,1,2,3,4,5};
int temp;
for (int i = 0; i < args.length; i++){
if (i % 2 == 0){
member[i] = new Member(args[i], Integer.valueOf(args[i+1]));
}
}
int k = 0;
boolean swap;
do{
swap = false;
if (member[k % 7].compareTo(member[(k+1) % 7]) > 0){
temp = sequence[k % 7];
sequence[k % 7] = sequence[(k+1) % 7];
sequence[(k+1) % 7] = temp;
swap = true;
}
k++;
} while(swap);
for (int i = 0; i < args.length; i++)
System.out.printf("%s, %d\n", member[i].getName(), member[i].getAge());
}
public static class Member implements Comparable<Member> {
private String name;
private int age;
public Member(String name, int age){
this.name = name;
this.age = age;
}
public Member(){
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
@Override
public int compareTo(Member a) {
if (age == a.age){
return 0;
}else if (age > a.age){
return 1;
}else return -1;
}
}
答案 0 :(得分:-1)
u获取NullPointerException
member[k % 7].compareTo(member[(k+1) % 7]
因为您在这里只是新的偶数索引
for (int i = 0; i < args.length; i++){
if (i % 2 == 0){
member[i] = new Member(args[i], Integer.valueOf(args[i+1]));
}
}
如果陈述或其他与您的逻辑有关的事情,您必须删除
和其他原因Integer.valueOf(args [i + 1])导致ArrayIndexOutOfBoundsException。数组从0索引开始