简体   繁体   中英

Why throwing exception is not working when parameter is null?

I created a method which remove the students from a HashMAP. it should throw an exception when the id is null. Did somebody know why it is not working?

public void deleteStudent(String firstName, String lastName, String phoneNumber, String birthDate, PersonGender gender, String id)  {
        Student student = new Student(firstName, lastName, phoneNumber, birthDate, gender, id);

        if (students.containsKey(id)) {
            students.remove(id);
        }
        if (students.containsKey(id == null)) {
              throw new NullPointerException("The student does not exist");
        }
    }

public class Application { public static void main(String[] args) {

    StudentRepository myStudent = new StudentRepository();
    myStudent.addStudent("St","Rt","0742", "1993.03.04", PersonGender.MALE, "1930303");
    myStudent.addStudent("Sr","Ro","0742", "1994.03.04", PersonGender.MALE, "1940304");
    myStudent.addStudent("Se","Rb","0742", "1995.03.04", PersonGender.MALE, "1950305");
    myStudent.addStudent("Sm","Re","0742", "1996.03.04", PersonGender.MALE, "1950306");
    myStudent.deleteStudent("Str","Rob","0742", "1992.03.04", PersonGender.MALE, "null");
    myStudent.addStudent("Sr","Ro","0742", "1994.03.04", PersonGender.MALE, "1940304");
    myStudent.displayStudents();


}

}

Try:

public void deleteStudent(String firstName, String lastName, String phoneNumber, String birthDate, PersonGender gender, String id)  {
        Student student = new Student(firstName, lastName, phoneNumber, birthDate, gender, id);

        if (id == null) {
              throw new NullPointerException("ID is null");
        }

        if (students.containsKey(id)) {
            students.remove(id);
        }
        else {
            throw new NullPointerException("The student does not exist");
        }

    }

Note:

  1. Assuming if students do not contain key then throw an exception. Hence added else statement.
  2. Also, checked passed id initially. if it is not found then throw an exception. You can modify it as per your requirement.

You should check the correctness of input parameters before using them. This follows a typical guard pattern .

my_function(some params) {
  if (precondition not met) { //the guard
    error handling;
  }
  business logic;
}

As your Student.id is in String format, you should check for null by using equals() .
Like this:

public void deleteStudent(String firstName, String lastName, String phoneNumber, String birthDate, PersonGender gender, String id)  {
    Student student = new Student(firstName, lastName, phoneNumber, birthDate, gender, id);

    if (students.containsKey(id)) {
        students.remove(id);
    }
    if (id.equals("null")) {
        throw new NullPointerException("The student does not exist");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM