0

I have a class called Student with data members:

private String name;
private long idNumber;

With correct getter and setter methods.

I need to create a tester class called StudentTest which includes the following methods:

public static Student[] createArray()

public static void populateArray(Student[] array)

public static void displayArray(Student[] array)

I am getting a "cannot find symbol" error when I try to compile my tester class.

Here is my tester class:

// Scanner class from the Java API
import java.util.Scanner;

public class StudentTester {
    public static void main(String[] args) {
        // Call methods
        Student[] studentList = createArray();
        populateArray(studentList);
        displayArray(studentList);

    } // end main

    // Create Array Method
    public static Student[] createArray() {
        // Declaration & Creation of Scanner object
        Scanner input = new Scanner(System.in);

        // Create Array
        System.out.println("Enter size of array: ");
        int i = input.nextInt();
        Student[] studentList = new Student[i];
        return studentList;
    }       

    // Populate Array Method 
    public static void populateArray(Student[] array) {
        for(int i = 1; i < studentList.length; i++) {
            studentList[i] = new Student();
            Scanner userInput = new Scanner(System.in);
            // Get Input for Name 
            System.out.println("Enter Student Name: ");
            studentList[i].setName = userInput.nextLine();
            // Get Input for Id Number
            System.out.println("Enter Student ID Number: ");
            studentList[i].setIdNumber = userInput.nextInt();
        }
    }

    // Display Array Method
    public static void displayArray(Student[] array) {
        System.out.println("Array Contents");
        for(int j = 0; j < studentList.length; j++) {
            System.out.println(studentList[j].getName() + " " + studentList[j].getIdNumber());
        }
    }

} // end class

Please can anyone help me out, I am quite new to java.

My Student class

public class Student {

    // Data Members
    private String name;
    private int idNumber;

    // Constructor
    public Student() {
        name = "Unassigned";
        idNumber = 0;
    }

    // Getters
    public String getName(){
        return name;
    }   

    public int getIdNumber(){
        return idNumber;
    }

    // Setters
    public void setName(String name){
        this.name = name;
    }

    public void setIdNumber(int idNumber){
        this.idNumber = idNumber;
    }


} // end class

The symbol that can't be found is to do with the array itself studentList

Cian
  • 5
  • 4
  • 3
    Post your `Student` class, and the **exact** error message(s). – Elliott Frisch Nov 17 '15 at 02:31
  • 1
    Without further information, I would guess that you need to `import` your `Student` class in the testing code. – Tim Biegeleisen Nov 17 '15 at 02:31
  • 1
    Yes, post the complete unedited error message. Also search on this error as it is a common one for newbies, and the solutions are often the same. – Hovercraft Full Of Eels Nov 17 '15 at 02:32
  • studentList is a variable declared in the main method but you are trying to access it in the populateArray method. Review scope and try again. I think you mean to populate the array named array in the populateArray method. – Vincent Ramdhanie Nov 17 '15 at 02:34
  • I've posted my Student class. I did try researching the error for quite a while now. I changed array to studentList. Error message I'm getting is this: StudentTester.java:32: error: cannot find symbol studentList[i].setName = userInput.nextLine(); ^ symbol: variable setName location: class Student StudentTester.java:35: error: cannot find symbol studentList[i].setIdNumber = userInput.nextInt(); ^ symbol: variable setIdNumber location: class Student 2 errors – Cian Nov 17 '15 at 02:44

1 Answers1

0

populateArray (and displayArray) are trying to use studentList, but you named it array in the arguments. Rename it, like

public static void populateArray(Student[] studentList) { // <-- not array.

public static void displayArray(Student[] studentList) { // <-- and here.

or use array instead of studentList in your methods.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Ah thanks man, I am still getting an error now though:StudentTester.java:32: error: cannot find symbol studentList[i].setName = userInput.nextLine(); ^ symbol: variable setName location: class Student StudentTester.java:35: error: cannot find symbol studentList[i].setIdNumber = userInput.nextInt(); – Cian Nov 17 '15 at 02:40
  • That is **not** how you call a setter. `studentList[i].setName(userInput.nextLine());`, next time post all of your code. – Elliott Frisch Nov 17 '15 at 02:49
  • Apologies for such silly mistakes. It compliles now, but i think my `displayArray` method is wrong. When I'm running the program I get this: Exception in thread "main" java.lang.NullPointerException at StudentTester.displayArray(StudentTester.java:43) at StudentTester.main(StudentTester.java:9) – Cian Nov 17 '15 at 03:08
  • Because you started at index `1` in `populateArray`, in `displayArray` you start at index `0`. What do you think you have at index `0` in `studentList`? – Elliott Frisch Nov 17 '15 at 03:12
  • Nevermind, I got it now. First time asking a question on this site, amazingly quick and helpful reponses. – Cian Nov 17 '15 at 03:14