gpt4 book ai didi

java - 在我的教师类(class)中,线程主错误中出现异常

转载 作者:太空宇宙 更新时间:2023-11-04 13:50:21 25 4
gpt4 key购买 nike

    // to create address object, and pass the address to faculty
// Must connect to other classes simultaneously

import java.util.ArrayList;

import javax.swing.JOptionPane;

public class Driver {
public static ArrayList<Student> student = new ArrayList<Student>();
public static ArrayList<Course> course = new ArrayList<Course>();
public static ArrayList<FacultyMember> faculty = new ArrayList<FacultyMember>();

public static void main(String[] args) {
String[] choices = { "0 Exit", "1 Add Student", "2 List of Students",
"3 Remove Student", "4 Create course", "5 List of Courses",
"6 Remove Course", "7 add instructor", "8 List of Instructors",
"9 Remove Instructor" };

student.add(new Student(308765433, "phillip", 2436, "Daly Street",
"Los Angeles", "CA", "USA"));
student.add(new Student(308765434, "Wilson Lucey", 2436,
"Broadway Street", "Los Angeles", "CA", "USA"));
student.add(new Student(308765432, "William", 2436,
"University Street", "Los Angeles", "CA", "USA"));

course.add(new Course(23545, "CS201"));
course.add(new Course(23546, "CS202"));
course.add(new Course(23547, "CS203"));

faculty.add(new FacultyMember(23477, "John"));
// faculty.add(new FacultyMember(23587, "Keenan"));
// faculty.add(new FacultyMember(236, "Parviz"));

int a = -1;
while (a < 0) {
int choice = JOptionPane.showOptionDialog(null, "Welcome", "Menu",
a, a, null, choices, a);
if (choice == 0) {
System.exit(0);
}
if (choice == 1) { // DONE
addStudent();

}
if (choice == 2) { // List all students inside array of students.
String a1 = "";
for (Student b : student) {
// b.toString();
a1 = a1 + b.toString() + "\n";

}
JOptionPane.showMessageDialog(null, a1);
}

if (choice == 3) {
removeStudent();
}
if (choice == 4) { // Show all courses inside array of courses
addCourse();
}
if (choice == 5) { // List of Course
String c1 = "";
for (Course b : course) {
// b.toString();
c1 = c1 + b.toString() + "\n";
}
JOptionPane.showMessageDialog(null, c1);
}
if (choice == 6) {
removeCourse();

}
if (choice == 7) {
addFaculty();
}
if (choice == 8) { // LIST OF FACULTY
String d1 = "";
for (FacultyMember d : faculty) {
// b.toString();
d1 = d1 + d.toString() + "\n";

}
JOptionPane.showMessageDialog(null, d1);
}
if (choice == 9) { // REMOVE FACULTY
removeFaculty();
}
}
}

private static void addStudent() {
int CIN = Integer.parseInt(JOptionPane.showInputDialog("Enter CIN: "));
String Name = JOptionPane.showInputDialog("Enter name");
// int street, String name, String city, String state, String country
int Street = Integer.parseInt(JOptionPane
.showInputDialog("Enter street #"));
String StreetName = JOptionPane.showInputDialog("Enter Street Name");
String City = JOptionPane.showInputDialog("Enter city");
String State = JOptionPane.showInputDialog("Enter state");
String Country = JOptionPane.showInputDialog("Enter country");

Student s1 = new Student(CIN, Name, Street, StreetName, City, State,
Country);

student.add(s1);
}

private static void addCourse() { // If

int courseID = Integer.parseInt(JOptionPane
.showInputDialog("Enter the id numer please!"));
String courseTitle = JOptionPane
.showInputDialog("Enter a course please!");
// String instructorName = JOptionPane
// .showInputDialog("Enter instructor for the course please!");
Course c1 = new Course(courseID, courseTitle); // CREATE OBJECT
course.add(c1);
}

// private static void Course(int courseID, String courseTitle, String term,
// String instructorName) {
//
// }

private static void addFaculty() {
int employeeID = Integer.parseInt(JOptionPane
.showInputDialog("Enter the employee id numer please!"));
String facultyName = JOptionPane
.showInputDialog("Enter the name of faculty please!"); // SAME
// AS
// STUDENTS
FacultyMember f1 = new FacultyMember(employeeID, facultyName);
faculty.add(f1);
}

public static void removeStudent() {

String a1 = "";
int i = 0;
for (Student b : student) {
// b.toString();
a1 = a1 + i++ + b.toString() + "\n";

}

int LineNumber = Integer.parseInt(JOptionPane.showInputDialog(null, a1
+ "Enter a row # to remove, please"));
student.remove(LineNumber);
for (int k = 0; k < student.size(); k++) {
System.out.println(student.get(k));

}
}

public static void removeCourse() {

String a1 = "";
int i = 0;
for (Course b : course) {
// b.toString();
a1 = a1 + i++ + b.toString() + "\n";
}

int LineNumber = Integer.parseInt(JOptionPane.showInputDialog(null, a1
+ "Enter a row # to remove, please"));
course.remove(LineNumber);
for (int k = 0; k < course.size(); k++) {
System.out.println(course.get(k));
}
}

public static void removeFaculty() {

String b1 = "";
int i = 0;
for (FacultyMember b : faculty) {
// b.toString();
b1 = b1 + i++ + b.toString() + "\n";

}
// JOptionPane.showMessageDialog(null,
// a1+"Enter a row # to remove, please");
//

int LineNumber = Integer.parseInt(JOptionPane.showInputDialog(null, b1
+ "Enter a row # to remove, please"));
faculty.remove(LineNumber);
for (int k = 0; k < faculty.size(); k++) {
System.out.println(faculty.get(k));

}
}
}
// }

public class Address {

/*
* An address has a street number, street name, city, state or province, and
* country.
*/
private int street;
private String name;
private String city;
private String state;
private String country;

public Address(int street, String name, String city, String state, String country) {
this.street = street;
this.name = name;
this.city= city;
this.state = state;
this.country = country;
}

public int getStreet() {
return street;
}

public void setStreet(int street) {
this.street = street;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCity(String city){
return city;
}

public void setCity(String city){
this.city=city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String toString() {
return name + " "+
"Street: "+street+ " "+
"City: "+city+ " , "+ " State: "+ state +
"Country: "+country;
}
}

public class Person {

/*
* A Person has a name and an Address (represented by an object of class
* Address, not a String). Note that the subclasses of Person inherit the
* fields and methods of Person. You may need to override some of the
* methods in the subclasses.
*/

protected String name;
protected Address address;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void setAddress(int street, String name, String city, String state,
String country) {

Address set1 = new Address(street, name, city, state, country);
this.address = set1;
}

public String toString() {
return "Name: " + name + " "+"Address: " + address.toString();
}
}


public class Course {

private int identifier;
private String courseTitle;

public Course(int identifier, String courseTitle){
this.identifier= identifier;
this.courseTitle= courseTitle;

}

public int getIdentifier() {
return identifier;
}

public String getCourseTitle() {
return courseTitle;
}

public String toString(){
return "Course ID: "+identifier + "Course title: "+ courseTitle;
}

public boolean contains(Course course) {
// TODO Auto-generated method stub
return false;
}


//setter to change the object values
//getters is to return some values we need

}

import java.util.ArrayList;
import java.util.List;

public class FacultyMember extends Person{
private int employeeID;
public ArrayList<Course>course = new ArrayList<Course>();

public FacultyMember(int employeeID, String nameIn){
this.name= nameIn;
this.employeeID = employeeID;
}
public int getID(){
return employeeID;
}
public void setID(int employeeID){
this.employeeID = employeeID;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public List<Course> getCourse(){
return course;

}
public void addCourse(Course course) {
this.course.add(course);
}

public String toString(){
return "EmployeeID: " + employeeID +super.toString() ;
}
}

编写一个驱动程序类来维护学生、类(class)和列表的列表 教员成员并有一个菜单,提供列出、创建和删除的方法 它们基于用户输入。驱动程序类还应该提供一种方法 学生添加和删除现有类(class)以及分配教职人员的方法 教授现有类(class)。当学生添加或添加新类(class)时,请勿创建新类(class) 当教职人员被分配教学时;让用户从中选择类(class) 列表。在开始编码之前考虑如何组织此输入。

包含一个可以从 main 调用的方法,该方法将使用您的方法来 添加和删​​除一些硬编码的测试数据(几个学生,几个教员 成员和几门类(class)。)这将让您对列表进行编码并测试 无需使用用户输入功能即可添加和删除项目的方法。

我的教职员工类(class)遇到困难,我收到此错误

Exception in thread "main" java.lang.NullPointerException
at Person.toString(Person.java:30)
at FacultyMember.toString(FacultyMember.java:33)
at Driver.main(Driver.java:83)

最佳答案

如前所述,您在 Person 类中的地址实例变量上调用 toString,但您从未实例化该变量,这意味着它具有对象的默认值:null。

如果尝试调用非实例化变量的成员,它将抛出 NullPointerException。

public String toString() {
return "Name: " + name + " "+"Address: " + address.toString();

}

删除对地址的 toString 调用,或者确保实例化它。

话虽如此,您的代码还有很多可以改进的地方:

代码如下:

if (choice == 6) {
removeCourse();

}
if (choice == 7) {
addFaculty();
}
if (choice == 8) { // LIST OF FACULTY
String d1 = "";
for (FacultyMember d : faculty) {
// b.toString();
d1 = d1 + d.toString() + "\n";

}
JOptionPane.showMessageDialog(null, d1);
}
if (choice == 9) { // REMOVE FACULTY
removeFaculty();
}

效率不高。我想我们都可以同意,如果 choice == 6,choice 永远不会(同时)具有值 7、8 或 9,但你仍然要测试它们。

改进这一点的一种方法是使用 else 语句:

if (choice == 6) {
removeCourse();

}else if (choice == 7) {
addFaculty();
} else if (choice == 8) { // LIST OF FACULTY
String d1 = "";
for (FacultyMember d : faculty) {
// b.toString();
d1 = d1 + d.toString() + "\n";

}
JOptionPane.showMessageDialog(null, d1);
}else if (choice == 9) { // REMOVE FACULTY
removeFaculty();
}

当您执行此操作时,如果它已经找到了先前测试中的值,则它不会检查 choice 是否具有其中一个值。因此,如果 choice == 6,则不会测试 7、8 或 9。

尽管这已经好很多了,但仍然不太容易阅读。 switch 语句可以在这里提供帮助:

switch(choice){
case 6: removeCourse();
break;
case 7: addFaculty();
break;
case 8: String d1 = "";
for (FacultyMember d : faculty) {
// b.toString();
d1 = d1 + d.toString() + "\n";}
JOptionPane.showMessageDialog(null, d1);
break;
case 9: removeFaculty();
break;
}

添加适当的缩进,您将更容易维护代码。

毫无疑问,您还有更多可以改进的地方,但这将为您提供一些开始。

关于java - 在我的教师类(class)中,线程主错误中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30365971/

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