Results 1 to 6 of 6
- 12-07-2009, 02:49 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
Problem with constructing an ArrayList
Hi all. I'm in my first year at university doing computer science and having a little trouble with an assigment. I'm trying to construct an ArrayList containing a number of Students which are in a College. (Students and College are both classes) However, the ArrayList<Student> seems to come up null, meaning the students aren't being added.
Obviously there is more code and I will post if requested but I think I'm just making a fairly basic error. Thanks in advance for any help.
Java Code:import java.util.Random; import java.util.ArrayList; public class College { private ArrayList<Student> sArr; private Library aLibrary; private Random random; public College(int nStudents, int nBooks) { aLibrary = new Library(nBooks); ArrayList<Student> sArr = new ArrayList<Student>(nStudents); for (int j = 0; j < nStudents; j++) { String name = "Student" + j; Student newStudent = new Student(name, aLibrary); sArr.add(newStudent); }
- 12-07-2009, 03:01 PM #2
check whether newStudent is null by putting s.o.p
Ramya:cool:
- 12-07-2009, 03:45 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
You have two sArr variables: one is a member variable and it is null (by definition); the other one is a local variable, local to your constructor. You obviously don't want that so you should change that line to:
Java Code:sArr = new ArrayList<Student>(nStudents);
Jos
- 12-07-2009, 04:32 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
Many thanks Jos that worked perfectly.
As a matter of interest, what is an s.o.p?
- 12-07-2009, 04:40 PM #5
Hi Jos,
Even I missed the arraylist initialized part....Great!!!!!!!1Ramya:cool:
- 12-07-2009, 04:52 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Constructing the Interface!
By myskynim in forum New To JavaReplies: 3Last Post: 11-19-2009, 10:51 AM -
Constructing simple Servlet
By herbozo2003 in forum Java ServletReplies: 1Last Post: 03-02-2009, 12:35 PM -
Hey! ArrayList problem here
By Samgetsmoney in forum New To JavaReplies: 31Last Post: 02-20-2009, 01:39 AM -
Need help with constructing code
By Nine0joe in forum New To JavaReplies: 6Last Post: 05-09-2008, 03:14 AM -
ArrayList problem
By khamuruddeen in forum New To JavaReplies: 7Last Post: 12-22-2007, 06:46 AM
Bookmarks