Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi everyone! I writed project called TeacherRegistrationSystemConsoleApp. I have one problem and do not solve this problem. How to solve this problem. if you want to see the problem than run this code any IDEA.

package main;

import beans.Student;
import beans.Teacher;
import store.Teacherstore;

import java.util.Scanner;

public class Main {
    @SuppressWarnings("InfiniteLoopStatement")
    public static void main(String[] args) {
        while (true) {
            System.out.println("Please choose action from menu:\n" +
                    "1. Register Teacher:\n" +
                    "2. Delete Teacher:\n" +
                    "3. Find Teacher:\n" +
                    "4. Show all Teacher:\n" +
                    "5. Add your classrom to student:\n" +
                    "6. Delete from your classrom students:\n" +
                    "7. Find the Student in your classroom\n" +
                    "8. Show my student in my classroom:\n" +
                    "9. Exit menu:\n");
            int menuId = new Scanner(System.in).nextInt();
            if (menuId == 1) {
                System.out.println("How many teacher will registered? ");
                int countTeacher = new Scanner(System.in).nextInt();//2
                Teacherstore.teachers = new Teacher[countTeacher];
                for (int i = 0; i < Teacherstore.teachers.length; i++) {
                    System.out.println("Write info detail person: " + (i + 1));

                    System.out.println("Enter id:");
                    int id = new Scanner(System.in).nextInt();

                    System.out.println("Enter name:");
                    String name = new Scanner(System.in).nextLine();

                    System.out.println("Enter surname:");
                    String surname = new Scanner(System.in).nextLine();

                    System.out.println("Enter age:");
                    int age = new Scanner(System.in).nextInt();

                    System.out.println("Enter salary:");
                    double salary = new Scanner(System.in).nextDouble();
                    Teacher teacher = new Teacher(id, name, surname, age, salary);
                    Teacherstore.teachers[Teacherstore.currentTeacher++] = teacher;
                    System.out.println("Teacher succesfully registred: " + teacher);
                    System.out.println();
                }
            } else if (menuId == 2) {
                System.out.println("Write id to delete following Teacher: ");
                for (int i = 0; i < Teacherstore.teachers.length; i++) {
                    if (Teacherstore.teachers[i] != null) {
                        System.out.println(Teacherstore.teachers[i]);
                    }
                }
                int teacherDelete = new Scanner(System.in).nextInt();
                for (int i = 0; i < Teacherstore.teachers.length; i++) {
                    if (Teacherstore.teachers[i] != null && Teacherstore.teachers[i].getId() == teacherDelete) {
                        System.out.println("Teacher successfully deleted: " + Teacherstore.teachers[i]);
                        Teacherstore.teachers[i] = null;
                    }
                }
                for (int i = 0; i < Teacherstore.teachers.length; i++) {
                    if (Teacherstore.teachers[i] != null) {
                        System.out.println("Teachers remaining in the database: " + Teacherstore.teachers[i]);
                    }
                }

            } else if (menuId == 3) {
                System.out.println("Write id,name,surname or salary to find Teacher: ");
                String findKey = new Scanner(System.in).nextLine();
                for (int i = 0; i < Teacherstore.teachers.length; i++) {
                    if (Teacherstore.teachers[i] != null) {
                        if (Teacherstore.teachers[i].getId().toString().contains(findKey) ||
                                Teacherstore.teachers[i].getName().contains(findKey) ||
                                Teacherstore.teachers[i].getSurname().contains(findKey) ||
                                Teacherstore.teachers[i].getAge().toString().contains(findKey) ||
                                Teacherstore.teachers[i].getSalary().toString().contains(findKey)) {
                            System.out.println("Founded teacher:" + Teacherstore.teachers[i]);
                        }
                    }
                }
            } else if (menuId == 4) {
                System.out.println("Teachers remaining in the database: ");
                for (int i = 0; i < Teacherstore.teachers.length; i++) {
                    if (Teacherstore.teachers[i] != null) {
                        System.out.println(Teacherstore.teachers[i]);
                    }
                }
                System.out.println();
            } else if (menuId == 5) {
                System.out.println("How many student add your classroom: ");
                int countStudents = new Scanner(System.in).nextInt();
                Teacher.studentsTeacher = new Student[countStudents];
                for (int i = 0; i < Teacher.studentsTeacher.length; i++) {
                    System.out.println("Write info detail student: " + (i + 1));

                    System.out.println("Enter id: ");
                    int id = new Scanner(System.in).nextInt();

                    System.out.println("Enter name: ");
                    String name = new Scanner(System.in).nextLine();

                    System.out.println("Enter surname: ");
                    String surname = new Scanner(System.in).nextLine();

                    System.out.println("Enter age: ");
                    int age = new Scanner(System.in).nextInt();

                    System.out.println("Enter scholarship: ");
                    double scholarship = new Scanner(System.in).nextDouble();
                    Student student = new Student(id, name, surname, age, scholarship);
                    Teacher.studentsTeacher[i] = student;
                    System.out.println("Student succesfully add the classrom: " + student);
                    System.out.println();
                }

            } else if (menuId == 6) {
                System.out.println("Which student do you want to delete from your class?");
                for (int i = 0; i < Teacher.studentsTeacher.length; i++) {
                    if (Teacher.studentsTeacher[i] != null) {
                        System.out.println(Teacher.studentsTeacher[i]);
                        break;
                    }
                }
                String deleteStudent = new Scanner(System.in).nextLine();
                for (int i = 0; i < Teacher.studentsTeacher.length; i++) {
                    if (Teacher.studentsTeacher[i] != null && Teacher.studentsTeacher[i].getId().toString().contains(deleteStudent)) {
                        System.out.println("Student successfully deleted from your classrom: " + Teacher.studentsTeacher[i]);
                        Teacher.studentsTeacher = null;
                        break;
                    } else if (Teacher.studentsTeacher[i] != null && Teacher.studentsTeacher[i].getName().contains(deleteStudent)) {
                        System.out.println("Student successfully deleted from your classrom: " + Teacher.studentsTeacher[i]);
                        Teacher.studentsTeacher = null;
                        break;
                    } else if (Teacher.studentsTeacher[i] != null && Teacher.studentsTeacher[i].getSurname().contains(deleteStudent)) {
                        System.out.println("Student successfully deleted from your classrom: " + Teacher.studentsTeacher[i]);
                        Teacher.studentsTeacher = null;
                        break;
                    } else if (Teacher.studentsTeacher[i] != null && Teacher.studentsTeacher[i].getAge().toString().contains(deleteStudent)) {
                        System.out.println("Student successfully deleted from your classrom: " + Teacher.studentsTeacher[i]);
                        Teacher.studentsTeacher = null;
                        break;
                    } else if (Teacher.studentsTeacher[i] != null && Teacher.studentsTeacher[i].getScholarship().toString().contains(deleteStudent)) {
                        System.out.println("Student successfully deleted from your classrom: " + Teacher.studentsTeacher[i]);
                        Teacher.studentsTeacher = null;
                        break;
                    }
                }
            } else if (menuId == 8) {
                System.out.println("Students in your classrom: ");
                for (int i = 0; i < Teacher.studentsTeacher.length; i++) {
                    if (Teacher.studentsTeacher[i] != null) {
                        System.out.println(Teacher.studentsTeacher[i]);
                    }
                }
                System.out.println();
            }

        }
    }
}

What I have tried:

I tried solve the NullPointerException.
Posted
Updated 11-Mar-21 1:05am
Comments
Richard Deeming 11-Mar-21 6:18am    
If you can't even be bothered to describe the problem, why should anyone be bothered to try to help you?

Click the green "Improve question" link and update your question to include a clear and complete description of the problem you are trying to solve. Include the full details of any errors.
[no name] 11-Mar-21 6:30am    
seriously I can not able to describe my problem, just only try to run and you can see the problem.
Richard Deeming 11-Mar-21 6:32am    
If you are unable to describe the problem with your code, then you are on the wrong course. Especially if you think it's so simple that anyone who runs your code will immediately see the problem!

Try harder, or switch to a different subject.
[no name] 11-Mar-21 7:22am    
sir, you are agressif man, I already solve this problem. thank you again so much.

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and your IDE will help you here. Run your program in the debugger and when it fails, the IDE will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, it will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
[no name] 11-Mar-21 7:22am    
thank you so much about more detail information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900