Results 1 to 6 of 6
  1. #1
    xpl0rerchr is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Initializing an array of class type variables.

    So, this is the Student class and its constructor.

    Java Code:
    public class Student {
        
        public enum Status {ACTIVE, INACTIVE};
        
        private String name;
        private int aem;
        private int year;
        private Status status = Status.ACTIVE;
        Course[] coursesReg;
        private int numCoursesReg = 0;
        private final int MAX_COURSES = 54;  
        
        
        Student(String name, int aem, int year) {
            this.name = name;
            this.aem = aem;
            this.year = year;
        }
    }
    And this is the UniClass class, its constructor and its addStudent function.

    Java Code:
    public class UniClass {
        
        private int classYear;
        private int initId;
        Student[] students;       // This is the array of Student objects I'm having problems with.
        private int numStudents;
        private final int MAX_STUDENTS = 150;
        
        UniClass(int year, int aem) {
            classYear = year;
            initId = aem;
            numStudents = 0;
        }
    
        boolean addStudent(String name) {
            int aem = initId;
            if (students.length == MAX_STUDENTS) {
                return false;
            }
            for (int i = 0; i < MAX_STUDENTS; i++) {
                if (students[i] == null) { 
                    students[i] = new Student(name, aem, classYear);    // This is where I'm getting the NullPointerException.
                    initId++;
                    numStudents++;
                    return true;
                }
            }
            return false;
        }
    }
    Now, as I'm trying to add a new student in addStudent, I'm getting a NullPointerException. Why would this be? I've already created an instance of UniClass.
    And I had previously (now I've deleted that part) tried to set each cell in the "students" array to null, and I was also getting the NullPointerException thing. Should I be initializing that array somehow differently than I tried, or is something else wrong?
    PS. I'm a begginer at Java, if that's not obvious already. I have a lot of experience with C, though.

    Your help is much appreciated!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    2,834
    Rep Power
    6

    Default Re: Initializing an array of class type variables.

    When do you initialize the students array?

    Recommended reading: http://docs.oracle.com/javase/tutori...ts/arrays.html
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    xpl0rerchr is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: Initializing an array of class type variables.

    Quote Originally Posted by KevinWorkman View Post
    When do you initialize the students array?

    Recommended reading: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    As I said, "(...) I had previously (now I've deleted that part) tried to set each cell in the "students" array to null, and I was also getting the NullPointerException thing. Should I be initializing that array somehow differently than I tried, or is something else wrong?".

    ΕDIT: You were right, I hadn't initialized it the way arrays are supposed to be initialized. I guess I'm still too used to the for loop used in C for this purpose.
    Last edited by xpl0rerchr; 03-28-2012 at 04:08 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    10,438
    Rep Power
    16

    Default Re: Initializing an array of class type variables.

    No, Kevin means when do you initialise this:
    Java Code:
    Student[] students;
    This simply declares a variable, but 'students' is not pointing at anything...it is null.
    Please do not ask for code as refusal often offends.

  5. #5
    xpl0rerchr is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: Initializing an array of class type variables.

    Quote Originally Posted by Tolls View Post
    No, Kevin means when do you initialise this:
    Java Code:
    Student[] students;
    This simply declares a variable, but 'students' is not pointing at anything...it is null.
    You are right, I realised just a while ago what he really meant. Thank you for clarifying this.
    I did the following:
    Java Code:
    Student[] students = new Student[MAX_STUDENTS];

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,918
    Rep Power
    16

    Default Re: Initializing an array of class type variables.

    Moved from NetBeans.

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. Initializing an array before running a thread
    By AnimeKitty in forum New To Java
    Replies: 1
    Last Post: 01-31-2012, 01:10 PM
  2. Initializing a class from a JSP page
    By dsagrera in forum New To Java
    Replies: 2
    Last Post: 09-19-2011, 11:17 AM
  3. Replies: 2
    Last Post: 08-12-2011, 08:48 PM
  4. Error initializing class instances
    By fr0s1yjack in forum New To Java
    Replies: 1
    Last Post: 06-22-2011, 03:58 PM
  5. Initializing variables using constructors
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •