大家好!我是Java的新手,尤其是OOP。我受命编写一个有关篮球和排球队注册的三节课程序。这是一个简单的算法,有一件事我无法理解。我有三个班级,“会员”,“团队”和“测试”(我的主要工作方向)。每当我运行程序时,我都会不断收到NullPointerException。这是我的代码。
团队班级
package MyLibs;
/**
*
* @author anonymous
*/
public class Team {
public String name;
public Member [] members;
public int memberCnt;
public int maxMember;
public int minAge;
public int maxAge;
public Team(String name, int maxMember, int minAge, int maxAge) {
this.name = name;
this.maxMember = maxMember;
this.minAge = minAge;
this.maxAge = maxAge;
}
public void addMember(Member member){
members[memberCnt] = member;
memberCnt++;
}
public boolean checkQualification(Member member){
if (member.age < minAge || member.age > maxAge)
return false;
else
return true;
}
}
会员等级
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MyLibs;
/**
*
* @author anonymous
*/
public class Member {
public String name;
public int age;
public Member(String name, int age) {
this.name = name;
this.age = age;
}
}
主类
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MyApp;
import java.util.Scanner;
import MyLibs.*;
/**
*
* @author anonymous
*/
public class Test {
public static void main(String[] args) {
String bBall_name;
int bBall_age;
String vBall_name;
int vBall_age;
boolean res;
int choice;
Scanner scan = new Scanner(System.in);
System.out.println("Select Team");
System.out.println("-----------------");
System.out.println("[1] Basketball");
System.out.println("[2] Volleyball");
System.out.println("[3] Exit");
System.out.println("-----------------\n");
do{
System.out.print("Enter choice: ");
choice = scan.nextInt();
scan.skip("\n");
switch (choice){
case 1:
System.out.println("\nYou choose Basketball!\n");
System.out.print("Name: ");
bBall_name = scan.nextLine();
System.out.print("Age: ");
bBall_age = scan.nextInt();
Team basketball = new Team(bBall_name, 5, 18, 30);
Member bBall_mem = new Member(bBall_name, bBall_age);
if(basketball.memberCnt == basketball.maxMember){
System.out.println("\nBasketball team is no longer accepting applicants.\n");
break;
}
res = basketball.checkQualification(bBall_mem);
if(res == true){
System.out.println("\nWelcome to the Basketball team!\n");
basketball.addMember(bBall_mem);
}else{
System.out.println("\nSorry, you are not qualified!\n");
break;
}
break;
case 2:
System.out.println("\nYou choose Volleyball!\n");
System.out.print("Name: ");
vBall_name = scan.nextLine();
System.out.print("Age: ");
vBall_age = scan.nextInt();
Team volleyball = new Team(vBall_name, 3, 14, 20);
Member vBall_mem = new Member(vBall_name,vBall_age);
if (volleyball.memberCnt == volleyball.maxMember){
System.out.println("Volleyball team is no longer accepting applicants.\n");
break;
}
res = volleyball.checkQualification(vBall_mem);
if(res == true){
System.out.println("\nWelcome to the Volleyball team!\n");
volleyball.addMember(vBall_mem);
}else{
System.out.println("\nSorry, you are not qualified!\n");
break;
}
break;
}
}while(choice != 3);
}
}
错误发生在
basketball.addMember(bBall_mem);
和
volleyball.addMember(vBall_mem);
我不知道为什么总是出现该错误。我已经检查过了,我不认为它为空。请放心,这确实是我的第一次。谢谢大家为我提供帮助。
PS>我也是这里的新手:)
答案 0 :(得分:-1)
您不会在Team类中初始化Member数组。
只需在您的Team类构造函数中添加以下行:
this.members = new Member[maxMember];
或者像其他人建议的那样,您最好为此使用列表而不是数组