有人可以帮助我吗?我是java编程新手,我是否陷入了一个bug很长一段时间这是错误消息: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; }}
1 回答
有只小跳蛙
TA贡献1824条经验 获得超8个赞
你在下面的行中得到 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])); } }
你必须删除 if 语句或其他与你的逻辑相关的东西
和其他事情 Integer.valueOf(args[i+1]) 导致 ArrayIndexOutOfBoundsException。数组从 0 索引开始
添加回答
举报
0/150
提交
取消