Results 1 to 6 of 6
- 03-28-2012, 03:50 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
Initializing an array of class type variables.
So, this is the Student class and its constructor.
And this is the UniClass class, its constructor and its addStudent function.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; } }
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.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; } }
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!
- 03-28-2012, 03:59 PM #2
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.htmlHow to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-28-2012, 04:02 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
Re: Initializing an array of class type variables.
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.
- 03-28-2012, 04:08 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Initializing an array of class type variables.
No, Kevin means when do you initialise this:
This simply declares a variable, but 'students' is not pointing at anything...it is null.Java Code:Student[] students;
Please do not ask for code as refusal often offends.
- 03-28-2012, 04:10 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
- 04-10-2012, 10:03 AM #6
Similar Threads
-
Initializing an array before running a thread
By AnimeKitty in forum New To JavaReplies: 1Last Post: 01-31-2012, 01:10 PM -
Initializing a class from a JSP page
By dsagrera in forum New To JavaReplies: 2Last Post: 09-19-2011, 11:17 AM -
Initializing and array of object via their constructors
By aianta in forum New To JavaReplies: 2Last Post: 08-12-2011, 08:48 PM -
Error initializing class instances
By fr0s1yjack in forum New To JavaReplies: 1Last Post: 06-22-2011, 03:58 PM -
Initializing variables using constructors
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 08:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks