I am working on a project where I am building a student object from student java class. The student.java class specifies a method of:

public void setCourses(ArrayList<Course> courses) {
        this.courses = courses;
    }

So I have built the following two methods in my main.java class file:

public static void collectCourseInformation() {
        ArrayList<Course> courses = new ArrayList<Course>();
        boolean cont = true;
        while (cont) {
            // Input Course Information
            String course = JOptionPane.showInputDialog("Course Name:  ");
            String grade = JOptionPane.showInputDialog("Grade:  ");
            String status = JOptionPane.showInputDialog("Status:  ");
            Double gradeDouble = Double.parseDouble(grade);
            Course myCourse = new Course();
            myCourse.setCourse(course);
            myCourse.setGrade(gradeDouble);
            myCourse.setStatus(status);
            courses.add(myCourse);

            int confirm = JOptionPane.showConfirmDialog(null, "Do you want to add more courses to this student?");
            if (confirm > 0) {
                cont = false;
            }
        }
    }

    public static void collectStudentInformation() {
        ArrayList<Student> list = new ArrayList<Student>();

        boolean cont = true;
        while (cont) {
            // Input Student Record Details
            String name = JOptionPane.showInputDialog("Name:  ");
            String major = JOptionPane.showInputDialog("Major:  ");
            String age = JOptionPane.showInputDialog("Age:  ");
            String gpa = JOptionPane.showInputDialog("GPA:  ");
            String ssn = JOptionPane.showInputDialog("Social Security Number:  ");

            // Convert strings to usable data
            int ageint = Integer.parseInt(age);
            Double gpadouble = Double.parseDouble(gpa);
            
            collectCourseInformation();

            // Create the myStudent object and add it to the arraylist.
            Student myStudent = new Student();
            myStudent.setCourses(courses);
            myStudent.setName(name);
            myStudent.setMajor(major);
            myStudent.setAge(ageint);
            myStudent.setGpa(gpadouble);
            myStudent.setSsn(ssn);
            list.add(myStudent);

            int confirm = JOptionPane.showConfirmDialog(null, "Do you want to continue?");
            if (confirm > 0) {
                cont = false;
            }
        }
        int search = JOptionPane.showConfirmDialog(null, "Do you want to search?");
        if (search == 0) {
            String searchname = JOptionPane.showInputDialog("Which name?");
            searchStudentInformation(list, searchname);
        }

The problem is with the line 43. I am essentially creating an array list name myStudent and then populating student data and the collectStudentInformation method then calls the collectCourseInformation to gather the students courses into objects and then fill an arraylist object named courses. I am trying to insert that arraylist object (courses) into the object for myStudent which will then be added to an arraylist named list.

It doesn't want to let me add the courses arraylist object to the student object.

Any ideas on what I am doing wrong. Please note that I am a newbie to Java and I apologize if it's something incredibly stupid that I'm overlooking. LOL Any help would be appreciated.

You declared the variable courses from within the method collectCourseInformation. Therefore it doesn't exist outside of that method. Read about variable scope in Java.

As mentioned in previous reply, the courses variable is local to the method.

You can change the collectCourseInformation method to return the course:

public static Course collectCourseInformation() {
        ArrayList<Course> courses = new ArrayList<Course>();
        .....
        return courses;
}

Then change line 39 to:

Course courses = collectCourseInformation();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.