Hey guys, im getting a null pointer exception... but i dont know what is wrong.

Im trying to get data from a hashmap, and place it in a swing tabel.

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");

            planes_array[i]=null;    <--- null pointer on this row
            planes_array[i]=tmp_array;
        }

Do you guys know what the problem is? or do you need more info? (what info?)

Thx

line 1: ArrayList planes_array[] = null;
line 10: planes_array=null;
// since planes_array is null, planes_array is null

You need to initialise planes_array on line 1

Okay, but how?

i tried this:

ArrayList planes_array[] = new ArrayList();
ArrayList planes_array[] = new ArrayList()[];

Like any Array

String[] example = new String[5];

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.

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!

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.

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..

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.

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];

you will 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 is not 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.

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];

you will 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

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?

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];

you will 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 is not 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 and Yes, 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";
String [] k = new String{"ken","jimmy"};

doesn't even compile (Java 1.6u21).

String [] k = new String{"ken","jimmy"};

doesn't even compile (Java 1.6u21).

You are right James...but refer to my latest post.

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?

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

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.

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

That doesn't show anything supporting your claim.

That doesn't show anything supporting your claim.

I rest my case coz my point was to show that even though you have initialized an array there are cases where NPEs are thrown on its elements and this is one way to deal with that.If it's a contest you were looking for then I'm sorry I'm not your man.I am here to learn and help wherever possible, not to compete with anyone (real good programmers don't argue, they discuss)..or you could prove your prowess by answering my problem titled "Email a file and have it saved in a certain location"..then you'll be the man not empty arguments!!
Bye James

OK, bye dangari.
For anyone else confused by this exchange, here's the full explanation:

The cases where an NPE can be thrown on an array that has been created (allocated) but not initialised are where you attempt to use the array element before anything is assigned to it. This is the case shown in the link you pasted. The case that you posted yourself is one where an assignment is made to the array element, not where it is used. In that case (1) an NPE won't be thrown as a result of the assignment and (2) performing 2 consecutive assignments as you suggest will make no difference to that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.