0

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();


}

}

5
  • 1
    Isn't it just if (id == null) { throw new .... } ? Commented Jun 10, 2020 at 18:02
  • 1
    And it seems like you'd want that before you try to use id. Commented Jun 10, 2020 at 18:03
  • it has the same behaviour, no exception is throwed Commented Jun 10, 2020 at 18:06
  • 1) Please, add code where students is created/read, currently it's impossible to understand what's in that variable. 2) containsKey(id == null) is actually the same as containsKey(True), if students is just a set of id's, you can replace it with containsKey(null) Commented Jun 10, 2020 at 18:27
  • I added what you requested Commented Jun 10, 2020 at 18:37

3 Answers 3

2

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Horrible suggestion! Why should NPE be thrown for a missing student? NPE should strictly be thrown for null references. You should suggest to create a custom business exception and throw the same instead of misusing NPE.
@ArvindKumarAvinash you are right. We can do a lot with above code. However Motive was to solve the problem and guide the user to move ahead as per his/her written code
1

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;
}

Comments

0

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");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.