0

Job Expression A kindergarten in Athens that has classes for infants and toddlers wants to create an application to keep a history of its students per school year. The system supports the following functions:

  1. Print all students who have attended or have attended the station

  2. Registration of a new student in the current school year

  3. Delete a student from the current school year
  4. Search and print the classes (students and teachers) based on the definitions set by the user (which class eg infant and the year of study eg "2017-2018"
  5. Search for classes taken by each school teacher
  6. Search for teacher data via AMKA

Modeling

Each student is described by: Unique Code (id)

Full name

Date of birth

The objects created are stored in a static type table Student 100 seats to be available for recall.

Each teacher is described by: Unique Code (id) Full name AMKA

The objects created are stored in a static type table Teacher 100 seats to be available for recall.

Each school year is described by:

The year of study, for example "2019-2020"

Type table Student in each position of which a student is also stored (Student type variable) who is studying in the preschool class this year. (This table is not the static table mentioned above. This is a variable snapshot as it describes the class of preschoolers each school year) And The teacher of the preschool class ( Teacher type variable) And Type table Student in each position of which a student is also stored (Student type variable) who is studying this year in the infant class. (This table is the static mentioned above. This is a variable snapshot as it describes the class of infants each school year) And The infant class teacher ( Teacher type variable) And The size of the tables with the number of students may change from year to year, but may not change during the year. This means that when a school year is created, the size of these tables must be set. It is not necessary for the classes to be complete by students but in no case can a number greater than the size of the corresponding tables be studied in each class. That's why we store each student in available positions in the table (vacancy means that it has null content) The objects created are stored in a static type table SchoolYear 100 seats to be available for recall. Functionality The program consists of the 3 classes described in the previous section and a class responsible for starting and running the program which includes the main method and prints the following options menu:

1. Print all students who have attended school
2. Registration of a new student in the current school year
3. Delete a student from the current school year
4. Search for school year data
5. Search for classes taken by each school teacher
6. Search for teacher information via AMKA 


I have a feeling that this error comes from the Teacher "empty" constructor i made in order to create object3. If that's so how am i supposed to create an object of the class Teacher without using the parameters of the other constructor i've made? Since by creating an object3, i don't want to create an actual teacher, but to use it for other purposes such as calling Teacher methods etc. I'm sorry if i frustrated you but I am a beginner!!
Thank you for your time!

the error i get is:

        Exception in thread "main" java.lang.ExceptionInInitializerError
            at Ergasia.YearBook.main(YearBook.java:20)
        Caused by: java.lang.NullPointerException
            at Ergasia.Teacher.<init>(Teacher.java:19)
            at Ergasia.Teacher.<clinit>(Teacher.java:13)
            ... 1 more


        ```public class Teacher {```

        private int teacherId, teacherAmka,teacherCounter=0;
        private String teacherName;
        static int count=0; 

        int teacherIds[] = new int[100];
        String teacherNames[] = new String[100];
        int teacherAmkas[] = new int[100];

        static Teacher teacherPronipio2018_2019 = new Teacher("George Mill", 565637);
        static Teacher teacherNipio = new Teacher("John Snow", 564319);
        static Teacher teacherPronipio2019_2020 = new Teacher("Jacob Jonson", 564459);

        private static Teacher teacherData[]= new Teacher[100];{

        teacherData[0] = teacherPronipio2018_2019;
        teacherData[1] = teacherNipio;
        teacherData[2] = teacherPronipio2019_2020;}



        public Teacher getTeacherData(int i) {
            return teacherData[i];

        }
        public Teacher[] getTeacherData() {
            return teacherData;
        }


        public Teacher(String teacherName, int teacherAmka) {

            teacherData[count]=this;
            count++;
            this.teacherId = count;
            this.teacherName = teacherName;
            teacherAmkas[teacherCounter++] = teacherAmka;

        }


        public Teacher() {

        }
        public String toString() {
            return teacherId + " " + teacherName + " " + teacherAmka;
        }

        public String getTeacherName(Teacher teacherData) {
            return teacherName;
        }

        public int getAmkaNumber(int i) {
            return teacherAmkas[i];
        }

    }


and the class with main:



    ```public class YearBook {```

        public static void main(String[] args) {

            Scanner sc = new Scanner(System.in);
            int choice;
            int choice2,amkaNumber;
            int studentId,yearOfBirth;
            String studentName,answer;
            boolean flag = false;


            Student object2 = new Student();
            Student studentData[] = object2.getStudentData();

            Teacher object3 = new Teacher();

            Teacher teacherData[] = object3.getTeacherData();
            Teacher teacherPronipio2018_2019 = teacherData[0];
            Teacher teacherNipio = teacherData[1];
            Teacher teacherPronipio2019_2020 = teacherData[2];

            SchoolYear schoolYearObject = new SchoolYear();
            SchoolYear years[] = schoolYearObject.years; 

            Student[] pronipio2018_2019 = new Student[100];
            Student[] pronipio2019_2020 = new Student[100];
            Student[] nipio2018_2019 = new Student[100];
            Student[] nipio2019_2020 = new Student[100];

            for (int c = 0; c<100 ; c++) {
                if (schoolYearObject.schoolYear2018_2019.getPronipio(c) != null) {
                pronipio2018_2019[c]=schoolYearObject.schoolYear2018_2019.getPronipio(c);
                }
                if (schoolYearObject.schoolYear2018_2019.getNipio(c)!= null) {
                    nipio2018_2019[c] =  schoolYearObject.schoolYear2018_2019.getNipio(c);
                }
                if (schoolYearObject.schoolYear2019_2020.getPronipio(c) != null) {
                    pronipio2019_2020[c]=schoolYearObject.schoolYear2019_2020.getPronipio(c);
                }
                if (schoolYearObject.schoolYear2018_2019.getNipio(c)!= null) {
                    nipio2019_2020[c] =  schoolYearObject.schoolYear2019_2020.getNipio(c);
                }
            }


            do {

            System.out.println("Menu:");
            System.out.println("1. Show all the Students");
            System.out.println("2. Register a new Student in current School year");
            System.out.println("3. Delete a Student from current School year");
            System.out.println("4. Search Data for current School year");
            System.out.println("5. Search every teacher's class");
            System.out.println("6. Search teacher by AMKA number");
            System.out.println("7.Exit");

            do {
                System.out.print("Please pick one of the choices (1-6): ");
                choice = sc.nextInt();
                if (choice > 6 || choice < 1) {
                    System.out.println("The number you gave is invalid.");
                }
            }while (choice > 7 || choice < 1);

            if (choice == 1) {
                int i = 0;
                while (i <= 100) {
                    if (studentData[i] != null){ 
                    System.out.println(studentData[i].toString());
                    i++;
                    }
                }

            }else if (choice == 2) {
                System.out.println("Would you like to Register the student\nat pronipio or Nipio? ");
                System.out.print("Press 1 for Pronipio and 2 for Nipio: ");
                choice2 = sc.nextInt();
                while (choice2 != 1 && choice2 != 2) {
                    System.out.println("The number you gave is invalid.");
                    choice2 = sc.nextInt();
                } 

                if (choice2 == 1) {
                    int i=0;
                        while ( i < 100 && flag == false) {
                            if (pronipio2019_2020[i] == null) {
                                flag = true;
                                System.out.println("Please Give Name and Year of Birth: ");
                                studentName = sc.nextLine();
                                yearOfBirth = sc.nextInt();
                                Student newStudent = new Student(studentName,yearOfBirth);
                                pronipio2019_2020[i] = newStudent;
                                i++;
                                int j=0;
                                while(j < 100 && flag == false) {
                                    if (studentData[j] == null ) {
                                        studentData[i] = newStudent;
                                        j++;
                                    }
                                }
                            }
                        }   
                }else {
                    int i=0;
                    while ( i < 100 && flag == false) {
                        if (nipio2019_2020[i] == null) {
                            flag = true;
                            System.out.println("Please Give 6-digit student Id,\nName and Year of Birth: ");
                            studentId = sc.nextInt();
                            studentName = sc.nextLine();
                            yearOfBirth = sc.nextInt();
                            Student newStudent = new Student(studentName,yearOfBirth);
                            nipio2019_2020[i] = newStudent;
                            i++;
                            int j=0;
                            while(j < 100 && flag == false) {
                                if (studentData[j] == null ) {
                                    studentData[i] = newStudent;
                                    j++;
                                }
                            }
                        }
                    }

                }
            }else if(choice == 3) {
                System.out.println("Please Give the 6-digit Id of the Student You Would Like to Delete: ");
                studentId = sc.nextInt();
                int i =0;
                flag = false;
                while (i < 100 && flag == false) {
                    if (object2.getStudentId(pronipio2019_2020[i]) == studentId) {
                        flag = true;
                        System.out.print("Are you sure you want to delete this student? Yes or No: ");
                            answer = sc.nextLine();
                            if(answer == "yes" || answer == "Yes") {
                                pronipio2019_2020[i] = null;
                            }else if(answer == "no" || answer == "No"){
                                System.out.println("OK");
                            }
                    }
                }
                if (flag == false){
                    while (i < 100 && flag == false) {
                        if (object2.getStudentId(nipio2019_2020[i]) == studentId) {
                            flag = true;
                            System.out.print("Are you sure you want to delete this student? Yes or No: ");
                                answer = sc.nextLine();
                                if(answer == "yes" || answer == "Yes") {
                                    nipio2019_2020[i] = null;
                                }else if(answer == "no" || answer == "No"){
                                    System.out.println("OK");
                                }
                        }
                    }
                }

            }else if(choice == 4) {
                flag = false;
                do {
                System.out.print("Please Pick a School Year (2018-2019 or 2019-2020): ");
                answer = sc.nextLine();
                if (answer == "2018-2019") {
                    flag = true;
                    System.out.print("Please Pick 1 for pronipio and 2 for nipio: ");
                    choice2 = sc.nextInt();
                    if (choice2 == 1) {
                        System.out.println(object3.getTeacherName(teacherPronipio2018_2019));
                    }else if (choice2 == 2) {
                        System.out.println(object3.getTeacherName(teacherNipio));
                    } 

                }else if (answer == "2019-2020") {
                    flag = true;
                    System.out.print("Please Pick 1 for pronipio and 2 for nipio: ");
                    choice2 = sc.nextInt();
                    if (choice2 == 1) {
                        System.out.println(object3.getTeacherName(teacherPronipio2019_2020));
                    }else if (choice2 == 2) {
                        System.out.println(object3.getTeacherName(teacherNipio));
                    } 
                }else
                    System.out.println("There are no data for the year you searched for");

                }while (flag == false);

            }else if(choice == 5) {
                for(int i = 0 ; i <= 2 ; i++) {
                    for(int j = 0 ; j < 4  ; j++) {
                        if(j==0 || j==1) {
                            int c1 = 0;
                            for(int c2=0 ; c2 <= 1 ; c2++ ) {
                                if(teacherData[i] == schoolYearObject.getTeacher(years[c1], c2) && teacherData[i]!=null) {
                                    System.out.print(teacherData[i] + " 2018-2019 ");
                                    if (i==0) {
                                        System.out.println("Pronipio");
                                    }else if(i==1) {
                                        System.out.println("Nipio");
                                    }
                                }
                            }

                        }else if(j==2 || j==3) {
                            int c1 = 0;
                            for(int c2=2 ; c2 >= 1 ; c2-- ) {
                                if(teacherData[i] == schoolYearObject.getTeacher(years[c1], c2) && teacherData[i] != null) {
                                    System.out.print(teacherData[i] + " 2019-2020 ");
                                    if (i==2) {
                                        System.out.println("Pronipio");
                                    }else if(i==1) {
                                        System.out.println("Nipio");
                                    }
                                }
                            }

                        }
                    }
                }

            }else if(choice == 6) {

                System.out.println("Please give a6-digit AMKA number");
                amkaNumber = sc.nextInt();
                flag = false;
                for(int i=0; i<100; i++) {
                    if (object3.getAmkaNumber(i) == amkaNumber) {
                        flag = true;
                        if (object3.getTeacherData(i)!=null){
                        System.out.println(object3.getTeacherData(i));
                        }
                    }
                }
                if (flag == false) {
                    System.out.println("Invalid teacher AMKA number!");

                } 
            }

            }while(choice != 7);


            sc.close();
        }

    }







Lepla
  • 7
  • 4
  • So, what variable is null on the offending line as indicated by the stack trace, `Teacher.java:19`? What code is on that line? – Hovercraft Full Of Eels May 03 '20 at 17:08
  • this is line 19: teacherData[0] = teacherPronipio2018_2019; and on line 13 i created the instance teacherPronipio2018_2019 like this: static Teacher teacherPronipio2018_2019 = new Teacher("George Mill", 565637); – Lepla May 03 '20 at 17:13
  • You're using a static initializer block, the block of code within the `{ ...}` braces that contains the offending code, for some unknown and probably very bad reason -- don't do this. In fact none of your fields or methods should be static other than the main method. – Hovercraft Full Of Eels May 03 '20 at 17:15
  • The Teacher class should not hold an array of Teacher nor static Teacher variables. Instead that code should be elsewhere, perhaps within a separate School class, and the Teacher variables should be instance variables (non-static). Your problems look to be stemming from a bad program design. – Hovercraft Full Of Eels May 03 '20 at 17:17
  • ok thank you for that! The only thing is that when i remove the word static i get the stack overflow error on that same line.. uh so confused The program has to include 4 classes according to my exercise and i have already made them, so i can't make another one.. all the arrays that i have created are asked to be like that – Lepla May 03 '20 at 17:18
  • Yes, you get a stackoverflow *because the Teacher class should not be holding Teacher variables*, not in this way. ***Again***, that code belongs elsewhere. You need to re-think your entire program design as it is wrong. – Hovercraft Full Of Eels May 03 '20 at 17:22
  • You will want to [edit] your original post and update it with this key information. Then comment back to notify us of the change/improvement in the question. Key information should not be buried in comments. – Hovercraft Full Of Eels May 03 '20 at 17:39

0 Answers0