0

My code might look weird. I just started learning. I want to store the values that are in the "person" object in the "icsClass" array. I keep getting this error.

Error: Exception in thread "main" java.lang.NullPointerException at com.company.Main.main(Main.java:74)

public static void main(String[] args) {

        Student[] icsClass = new Student[MAX];
        Scanner s = new Scanner(System.in);

        int counter = 0;
        int year;
        int month;
        int day;

        System.out.println("This program will store 10 student's information.");
        System.out.println("Do you want to continue? (Yes/No)");
        String a = s.next();

        if (a.equals("Yes") || a.equals("yes")) {


            while (counter < MAX) {

                Student person = new Student();

                System.out.println("Please enter your first name:");
                person.setfName(s.next());

                System.out.println("Please enter your last name:");
                person.setlName(s.next());


                System.out.println("Please enter your year of birth:");
                year = (s.nextInt());
                while ((year < MIN_YEAR) || (year > MAX_YEAR)) {
                    System.out.println("Error: Year of birth must be between 1895 and 2021");
                    System.out.println("Please re-enter your year of birth:");
                    year = s.nextInt();
                }
                person.setYear(year);


                System.out.println("Please enter your month of birth:");
                month = (s.nextInt());
                while ((month < MIN_MONTH) || (month > MAX_MONTH)) {
                    System.out.println("Error: Month of birth must be between 1 and 12");
                    System.out.println("Please re-enter your month of birth:");
                    month = s.nextInt();
                }
                person.setMonth(month);


                System.out.println("Please enter your day of birth:");
                day = s.nextInt();
                while ((day < MIN_DAY) || (day > MAX_DAY)) {
                    System.out.println("Error: Day of birth must be between 1 and 31");
                    System.out.println("Please re-enter your day of birth:");
                    day = s.nextInt();
                }
                person.setDay(day);

                counter = counter + 1;

                if (counter == MAX) {
                    person.display();
                    System.out.println("Is the information above correct? (Yes/No)");
                    String b = s.next();
                    if ((Objects.equals(b, "Yes")) || (Objects.equals(b, "yes"))) {
                        icsClass[counter].setfName(person.getfName());
                        icsClass[counter].setlName(person.getlName());
                        icsClass[counter].setYear(person.getYear());
                        icsClass[counter].setDay(person.getDay());
                        icsClass[counter].setMonth(person.getMonth());
                    }
                }
            }
            //for (int i = 0; i < MAX; i++) {
            //   icsClass[i].display();
            //}
        }
    }
}
cigien
  • 57,834
  • 11
  • 73
  • 112
Faruk
  • 1
  • The short answer is that you need to create a `Student` object and assign it to `icsClass[counter]` before you attempt to use what is in `icsClass[counter]` (which is initially `null`!) – Stephen C Oct 16 '21 at 01:07
  • How do I assign an object to an array? – Faruk Oct 16 '21 at 01:16
  • See: [Create an array of references](https://stackoverflow.com/questions/15064457/create-an-array-of-references). Your question suggests that you're just now learning the most basic concepts of Java programming. Since this site is not geared to work in place of an introduction to programming website, and since best to get this information from tutorials, you will probably want to start there. You can find links to great Java tutorials and other language resources in the [java info](https://stackoverflow.com/tags/java/info) section of the [tag:java] tag. – Hovercraft Full Of Eels Oct 16 '21 at 01:21
  • Here `Student[] icsClass = new Student[MAX];` you are creating an array that accepts only Student object, the catch is this array has the size MAX and it is actually empty before using `icsClass[counter]` you have to assign a new instance of Student to it. – Jorge Campos Oct 16 '21 at 01:28
  • @Faruk - In general: `some_array[some_index] = some_object`. I think you should be reading your lecture notes, or a textbook, or a tutorial site, or something. – Stephen C Oct 16 '21 at 01:28
  • This is what my teacher told me to code. I have no idea about how arrays work and he didn't teach anything either. He only gave me instructions but I don't understand them. – Faruk Oct 16 '21 at 01:46

0 Answers0