Results 1 to 6 of 6
  1. #1
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default Array List help please

    I need help with another Array, I've created a miniature version of my code, and the same error is there. It compiles fine in Bluej.

    A contains an array, C is the Array object. B is to create a new C and add it to A.

    Problem:
    - B: call addToArrayInA(1, "string")
    - then get Null error in the same method, 2nd line.
    Java Code:
     myC.add(newC);
    What do I do here, if I go A.add(newC); it will cause non static variable error...


    Java Code:
    import java.util.ArrayList;
    public class A
    {
           ArrayList<C> c;
    
        /**
         * Constructor for objects of class A
         */
        public A()
        {
            c = new ArrayList<C>();
        }
    
        public void add(C neww) {
            c.add(neww);
        }
        public C get(int i) {
            C get = c.get(i);
            return get;
        }
    }
    Java Code:
    public class B
    {
        private A myC;
    
        public B()
        {
            
        }
    
        public void addToArrayInA(int one, String two) {
            C newC = new C(one, two);
            A.add(newC);
        }
        
        public C getC() {
            int i = 0;
            C cc = myC.get(i);
            return cc;
        }
    }
    Java Code:
    public class C
    {
        private int o;
        private String t;
        public C(int one, String two)
        {
            o = one;
            t = two;
        }
        
    }
    Last edited by keo; 10-17-2010 at 06:52 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You call myC.add(newC) but myC is of type C, not A.

    Perhaps you mean

    Java Code:
    private A myA;
    // initialise myA somewhere not shown
    
    public void addToArray(int one, String two) {
        C newC =new C(one, two);
        myA.add(newC);
    }
    
    public C getC() {
        return cc = myA.get(0)
    }

    If not, then code that's minimal but enough to illustrate the problem and actually runnable would be good.

  3. #3
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default

    I've edited my original post...

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    It shouldn't be "A.add(newC)", but "myC.add(newC)". See my earlier post (where I called the variable myA) and note the comment there about how myA will have to be initialised somewhere.

    [Edit]

    I've just reread the OP. That null pointer exception you are seeing is because you haven't initialised myC somewhere. You can do that either in the line where you declare it, or in the B constructor.
    Last edited by pbrockway2; 10-17-2010 at 07:15 AM.

  5. #5
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default

    I tried changing it, but am still not sure what to do, can you edit that code in the first post, So I can compare it with my code please?

    EDIT: Oh I understand what is wrong now. but How would I initialize an empty one because its already an arraylist?

    Java Code:
        private A myC;
    
        public B()
        {
            myC = new ArrayList<C>(10); //initialise with 10 slots?
            myC = ...??
        }
    Thank you!

    Edit. Figured it out! its myC = new C;
    Last edited by keo; 10-17-2010 at 12:06 PM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Something like that should do. But note: myC is of type A not C.

Similar Threads

  1. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  2. Array List help
    By Weejee37 in forum New To Java
    Replies: 4
    Last Post: 10-27-2009, 01:32 AM
  3. Array List
    By mashyum in forum Advanced Java
    Replies: 2
    Last Post: 07-28-2009, 07:47 AM
  4. Array List
    By mprentice84 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 03:12 AM
  5. array list help
    By dorno83 in forum New To Java
    Replies: 9
    Last Post: 11-07-2008, 10:56 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
  •