Results 1 to 6 of 6
  1. #1
    Diplo is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default 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);
            }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    check whether newStudent is null by putting s.o.p
    Ramya:cool:

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Diplo View Post
    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);
            }
    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);
    kind regards,

    Jos

  4. #4
    Diplo is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    Many thanks Jos that worked perfectly.

    As a matter of interest, what is an s.o.p?

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    Hi Jos,
    Even I missed the arraylist initialized part....Great!!!!!!!1
    Ramya:cool:

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Diplo View Post
    As a matter of interest, what is an s.o.p?
    Some people who are too lazy to type use s.o.p. for System.out.println( ... )

    kind regards,

    Jos

Similar Threads

  1. Constructing the Interface!
    By myskynim in forum New To Java
    Replies: 3
    Last Post: 11-19-2009, 09:51 AM
  2. Constructing simple Servlet
    By herbozo2003 in forum Java Servlet
    Replies: 1
    Last Post: 03-02-2009, 11:35 AM
  3. Hey! ArrayList problem here
    By Samgetsmoney in forum New To Java
    Replies: 31
    Last Post: 02-20-2009, 12:39 AM
  4. Need help with constructing code
    By Nine0joe in forum New To Java
    Replies: 6
    Last Post: 05-09-2008, 02:14 AM
  5. ArrayList problem
    By khamuruddeen in forum New To Java
    Replies: 7
    Last Post: 12-22-2007, 05:46 AM

Posting Permissions

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