-4

package com.company;

import static com.company.ProjectCONSTANTS.*;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    Student iscClass[] = new Student[MAX];


    Student st = new Student();


    String fNameInput = null, lNameInput = null;
    int yearInput = 0;
    int monthInput = 0;
    int dayInput = 0;
    int counter = 0;

    boolean inputOk;
    boolean correct = false;
    String uInput01;


    Scanner sc = new Scanner(System.in);


    System.out.println("This program will collect and store 10 people's full name and their date of birth.");
    System.out.println("Would you like to continue? (input yes or no)");

    uInput01 = sc.nextLine();
    uInput01 = uInput01.toLowerCase();


    while (!uInput01.equals("yes") && (!uInput01.equals("no"))) {
        System.out.println("Please input yes or no: ");
        uInput01 = sc.nextLine();
        uInput01 = uInput01.toLowerCase();

    }

    if (uInput01.equals("yes")) {
        inputOk = true;
    } else {
        inputOk = false;
        correct = true;
    }


    while (inputOk && counter <= MAX) {

        Scanner s = new Scanner(System.in);

        System.out.println("\nData for person " + counter + ":");


        // first name
        System.out.println("\nenter first name: ");
        fNameInput = s.nextLine();
        st.setFirstName(fNameInput);
        System.out.println("First name: " + fNameInput);

        // last name
        System.out.println("\nenter last name: ");
        lNameInput = s.nextLine();
        st.setLastName(lNameInput);
        System.out.println("Last name: " + lNameInput);

        // year
        System.out.println("\nenter the year of birth: ");

        // check for int
        while (!s.hasNextInt()) {
            System.out.println("please enter a number" + "\nenter again: ");
            s.next();
        }

        // store data into iInput
        yearInput = s.nextInt();


        // year not in between min_year and max_year?
        if (yearInput < MIN_YEAR || yearInput > MAX_YEAR) {

            do {
                System.out.println("year must be in between " + MIN_YEAR + " and " + MAX_YEAR + "\n");
                System.out.println("Please enter again: ");

                while (!s.hasNextInt()) {
                    System.out.println("please enter a number: ");
                    s.next();
                }
                yearInput = s.nextInt();
                if ((yearInput < MIN_YEAR) || (yearInput > MAX_YEAR)) {
                    inputOk = false;
                } else {
                    inputOk = true;
                }
            } while (!inputOk);
        }

        // store data into getYear
        st.setYear(yearInput);
        System.out.println("Year of birth: " + yearInput);

        // ask for month
        System.out.println("\nenter the month of birth: ");

        // check for int
        while (!s.hasNextInt()) {
            System.out.println("please enter a number" + "\nenter again: ");
            s.next();
        }


        // store data into iInput
        monthInput = s.nextInt();

        // month not in between min_month and max_month?
        if (monthInput < MIN_MONTH || monthInput > MAX_MONTH) {

            do {
                System.out.println("month must be in between " + MIN_MONTH + " and " + MAX_MONTH + "\n");
                System.out.println("Please enter again: ");

                while (!s.hasNextInt()) {
                    System.out.println("please enter a number: ");
                    s.next();
                }
                monthInput = s.nextInt();
                if ((monthInput < MIN_MONTH) || (monthInput > MAX_MONTH)) {
                    inputOk = false;
                } else {
                    inputOk = true;
                }
            } while (!inputOk);
        }

        // store data into getMonth
        st.setMonth(monthInput);
        System.out.println("Month of birth: " + monthInput);


        // ask for day
        System.out.println("\nenter the day of birth: ");

        //check for int
        while (!s.hasNextInt()) {
            System.out.println("please enter a number" + "\nenter again: ");
            s.next();
        }

        // store data into iInput
        dayInput = s.nextInt();

        // day not in between min_day and max_day?
        if ((dayInput < MIN_DAY) || (dayInput > MAX_DAY)) {

            do {
                System.out.println("day must be in between " + MIN_DAY + " and " + MAX_DAY);
                System.out.println("\nPlease enter again: ");

                while (!s.hasNextInt()) {
                    System.out.println("Please enter a number: ");
                    s.next();
                }

                dayInput = s.nextInt();

                if ((dayInput < MIN_DAY) || (dayInput > MAX_DAY)) {
                    inputOk = false;
                } else {
                    inputOk = true;
                }
            } while (!inputOk);
        }

        // store data into getDay
        st.setDay(dayInput);
        System.out.println("Day of birth: " + dayInput);

        // display temp data
        st.display();

        //confirmation
        System.out.println("\nIs the data above correct? (input yes or no)");
        uInput01 = sc.nextLine();
        uInput01 = uInput01.toLowerCase();

        while (!uInput01.equals("yes") && (!uInput01.equals("no"))) {
            System.out.println("Please input yes or no: ");
            uInput01 = sc.nextLine();
            uInput01 = uInput01.toLowerCase();

        }
        if (uInput01.equals("yes")) {
            counter++;

        } else {
            System.out.println("Let's try again!");
            correct = true;

        }


    }


    if (!correct) {
        for (int i = 0; i <= MAX; i++) {

            iscClass[i].setFirstName(fNameInput);
            iscClass[i].setLastName(lNameInput);
            iscClass[i].setDay(dayInput);
            iscClass[i].setMonth(monthInput);
            iscClass[i].setYear(yearInput);

            correct = true;
        }


    }

}

}

error occurs on the last couple lines of code; when trying to store data (fNameInput, lNameInput, etc.) in icsClass array, the code breaks, saying that there is an:

<Exception in thread "main" java.lang.NullPointerException
at com.company.Main.main(Main.java:217).>

This assignment from my computer science class is due by midnight; I would appreciate some help please and thank you

ossc
  • 1
  • 1

1 Answers1

0

The issue with your array is that you haven't properly instantiated the array of objects. Student iscClass[] = new Student[MAX]; creates an array to hold student objects but it doesn't fill the array with objects, so calling set methods wouldn't work. You will have to create objects using the new keyword, insert them in the array, and then use your set methods; Here's a guide: object arrays in java.

Suggestions:

Some parts of your code were if and while statements with repeated code; try taking that common functionality and simplifying it to one block that captures that functionality, either that or try using a method for any repeated behaviour. Try not to keep your methods(or your main) too long(you can divide common functionality into several methods), and always read all your notes for answers to issues and before you attempt assignments.

Alias Cartellano
  • 366
  • 1
  • 3
  • 12