Results 1 to 6 of 6
Thread: Array List help please
- 10-17-2010, 05:22 AM #1
Member
- Join Date
- May 2010
- Posts
- 34
- Rep Power
- 0
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.
What do I do here, if I go A.add(newC); it will cause non static variable error...Java Code:myC.add(newC);
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 05:52 AM.
- 10-17-2010, 05:45 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 10-17-2010, 06:04 AM #3
Member
- Join Date
- May 2010
- Posts
- 34
- Rep Power
- 0
I've edited my original post...
- 10-17-2010, 06:13 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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 06:15 AM.
- 10-17-2010, 08:59 AM #5
Member
- Join Date
- May 2010
- Posts
- 34
- Rep Power
- 0
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?
Thank you!Java Code:private A myC; public B() { myC = new ArrayList<C>(10); //initialise with 10 slots? myC = ...?? }
Edit. Figured it out! its myC = new C;Last edited by keo; 10-17-2010 at 11:06 AM.
- 10-17-2010, 08:00 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Similar Threads
-
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
Array List help
By Weejee37 in forum New To JavaReplies: 4Last Post: 10-27-2009, 12:32 AM -
Array List
By mashyum in forum Advanced JavaReplies: 2Last Post: 07-28-2009, 06:47 AM -
Array List
By mprentice84 in forum New To JavaReplies: 2Last Post: 02-20-2009, 02:12 AM -
array list help
By dorno83 in forum New To JavaReplies: 9Last Post: 11-07-2008, 09:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks