line 1: ArrayList planes_array[] = null;
line 10: planes_array[i]=null;
// since planes_array is null, planes_array[i] is null
You need to initialise planes_array on line 1
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Like any Array
String[] example = new String[5];
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
When you initialise your array you need to specify how big it is (arrays are fixed size). So
ArrayList planes_array[] = new ArrayList[99];
creates an array of 99 reference variables, each able to hold a reference to an ArrayList.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
My idea is a bit different since what I suggest is that you don't initialize the ArrayList at the place where the NullPointerException is being thrown at all. Simply have it this way:
ArrayList planes_array[] = null;
//planes_array[0] = new ArrayList();
for(int i=0; i<planes.size(); i++)
{
ArrayList tmp_array = new ArrayList();
tmp_array.add("1");
tmp_array.add("asdfaf");
tmp_array.add("12");
//no initialization because you cannot anticipate size here
//and no need to initialize with a size you won't fully use
planes_array[i]=tmp_array;
}
Thanks!
dangari
Junior Poster in Training
73 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
You still need to initialise the array however other wise
planes_array[i]
is essentially
null[i]
and arrays do not have size() method, they have a length attribute.
Edit: Please, please, please. If you are going to post code as a "solution" then at least make sure that it is compilable (typos we forgive) and that it is actually plausible.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
I used the term size in reference to the i index below
planes_array[i];
not calling it a method.
The other thing is that if one doesn't initialize an array as below:
String [] j;
String [] k = new String{"ken","jimmy"};
j= k;
The program would still run (though this is highly discouraged).
A better way would be:
String [] j;
String [] k = new String{"ken","jimmy"};
for(int i=0;i<k.length;i++)
{
j[i] = new String();//initialize the elements of the array
j[i]=k[i];
}
//instead of j= k;
That's all I'm saying..
dangari
Junior Poster in Training
73 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
Now we've sorted the NPE, can I ask about the strategy that you're taking here?
If the purpose is to populate a JTable, then it's a good idea to look at the various constructors for JTable before choosing data formats. For example, you have an array of ArrayLists, which will need further manipulations before JTable will display them, but if you go for the almost identical Vector of Vectors you can plug them straight into one of the JTable constructors.
ps Just read dangari's post - suggest you ignore it and allow masijade to deal with him.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
I used the term size in reference to the i index below
planes_array[i];
not calling it a method.
The other thing is that if one doesn't initialize an array as below:
String [] j;
String [] k = new String{"ken","jimmy"};
j= k;
The program would still run (though this is highly discouraged).
That's all I'm saying..
The thing is, the OP wants an array of ArrayLists not an ArrayList of arrays, so the "size()" call in the for loop is wrong, as you have an array at that point and not an ArrayList. And, if you use that
planes_array[i]
without initialising the array with at least
ArrayList planes_array[] = new ArrayList[someNumber];
youwill get an NPE. So, once again, my comments from the last post apply, and none of your comments in this post apply.
Thank you, please come again.
Edit: BTW this
String [] j;
String [] k = new String{"ken","jimmy"};
for(int i=0;i<k.length;i++)
{
j[i] = new String();//initialize the elements of the array
j[i]=k[i];
}
also throws an NPE at the line
j[i] = new String();//initialize the elements of the array
and would at the line
j[i]=k[i];
as well if it could make it that far for the same reason that the array j isnot initialised. Actually, in this case, you will get compiler messages because you haven't initialised it at all. The OP did initialise his, but to null.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
The thing is, the OP wants an array of ArrayLists not an ArrayList of arrays, so the "size()" call in the for loop is wrong, as you have an array at that point and not an ArrayList. And, if you use that
planes_array[i]
without initialising the array with at least
ArrayList planes_array[] = new ArrayList[someNumber];
youwill get an NPE. So, once again, my comments from the last post apply, and none of your comments in this post apply.
Thank you, please come again.
You will do well to see my point is on arrays not ArrayLists..see edited post above
dangari
Junior Poster in Training
73 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
Edit: I know this, but you seemingly don't as the things you are saying works, work for ArrayLists, not arrays, and you are using ArrayList methods on arrays rather than the corresponding array fields. So what is your point?
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
The thing is, the OP wants an array of ArrayLists not an ArrayList of arrays, so the "size()" call in the for loop is wrong, as you have an array at that point and not an ArrayList. And, if you use that
planes_array[i]
without initialising the array with at least
ArrayList planes_array[] = new ArrayList[someNumber];
youwill get an NPE. So, once again, my comments from the last post apply, and none of your comments in this post apply.
Thank you, please come again.
Edit: BTW this
String [] j;
String [] k = new String{"ken","jimmy"};
for(int i=0;i<k.length;i++)
{
j[i] = new String();//initialize the elements of the array
j[i]=k[i];
}
also throws an NPE at the line
j[i] = new String();//initialize the elements of the array
and would at the line
j[i]=k[i];
as well if it could make it that far for the same reason that the array j isnot initialised. Actually, in this case, you will get compiler messages because you haven't initialised it at all. The OP did initialise his, but to null.
I'm happy you got my point is on Arrays andYes, You are right masijade My bad.
My case only applies if after initialisation of the array
String [] j = new String[5];
you still get an NPE at an element of the array like
j[2] = "H";
then the following will help eradicate the NPE
j[2] = new String();
j[2]="H";
dangari
Junior Poster in Training
73 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
String [] k = new String{"ken","jimmy"};
doesn't even compile (Java 1.6u21).
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
String [] k = new String{"ken","jimmy"};
doesn't even compile (Java 1.6u21).
You are right James...but refer to my latest post.
dangari
Junior Poster in Training
73 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
You are right James...but refer to my latest post.
Yes, that looks like gibberish too. Have you actually tried any of the code you are posting?
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Yes, that looks like gibberish too. Have you actually tried any of the code you are posting?
Ignorance is no defense.
This one I actually got it from a Java guru at Sun if I'm not wrong on why elements of arrays that have been initialized can still give an NPE. Try it first before blabbing
dangari
Junior Poster in Training
73 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
Just to confirm: you said:
if j[2] = "H"; gives an NPE then j[2] = new String(); j[2]="H"; will help eradicate the NPE
Do you have a runnable test case to demonstrate that? I'll gladly apologise if you can show that you are right.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Just to confirm: you said:
Do you have a runnable test case to demonstrate that? I'll gladly apologise if you can show that you are right.
Just go to this link and see one good example: http://www.irt.org/script/4018.htm
dangari
Junior Poster in Training
73 posts since Aug 2009
Reputation Points: 10
Solved Threads: 3
That doesn't show anything supporting your claim.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073