Array of JPanel and Array of JTextField?

Hi,

I try to create array of jpanel and inizialize it in "for" , but when run it give me this error message:

"Exception in thread "main" java.lang.NullPointerException"

at the row: "panInsCantante.add(panInsNCC);"

why?

(ps. i tried in another code to create array of jtextfield and it give me same error message..)

here part of code :

//....

JPanel [] panInsCantante =new JPanel[3];

for(int i=0;i<=N;i++){

JPanel panInsNCC=new JPanel();

panInsNCC.setLayout(new GridLayout(3,1) );

panInsNCC.add(campoTesto1);

panInsNCC.add(campoTesto2);

panInsNCC.add(campoTesto3);

//panInsCantante[i] = new JPanel();

//panInsCantante[i].setLayout( new GridLayout(1,1) );

panInsCantante[i].add(panInsNCC);

//....

}

[1179 byte] By [leonard_shelbya] at [2007-11-27 10:37:11]
# 1

I'm not clear on what you want to do. I do see that you have shot yourself in the foot by commenting out the ilne of code that would construct your panInsCantante panel:

//panInsCantante[i] = new JPanel();

Why'd you comment that out?

petes1234a at 2007-7-28 18:46:25 > top of Java-index,Desktop,Core GUI APIs...
# 2

if i comment that out , so...

//....

JPanel [] panInsCantante = new JPanel[3];

for(int i=0;i<=N;i++){

JPanel panInsNCC= new JPanel();

panInsNCC.setLayout( new GridLayout(3,1) );

panInsNCC.add(campoTesto1);

panInsNCC.add(campoTesto2);

panInsNCC.add(campoTesto3);

panInsCantante[i] = new JPanel();

panInsCantante[i].setLayout( new GridLayout(1,1) );

panInsCantante[i].add(panInsNCC);

//....

}

i have error message:

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3"

at the row: "panInsCantante [i] = new JPanel();

"

....

Message was edited by:

leonard_shelby

leonard_shelbya at 2007-7-28 18:46:25 > top of Java-index,Desktop,Core GUI APIs...
# 3

that's because the for loop is wrong. It should check for:

i < N

not

i <= N

petes1234a at 2007-7-28 18:46:25 > top of Java-index,Desktop,Core GUI APIs...
# 4

Do this bit of code as an example:

for (int i = 0; i <= 3; i++)

{

System.out.println(i);

}

How many times is this loop called?

The correct way to iterate through a 3 item array is like so:

for (int i = 0; i < 3; i++)

{

System.out.println(i);

}

Also, remember that an array of 3 blobs goes from blob[0] to blob[2]. If you try to call blob[3] (the fourth item in the arrray), you'd be out of bounds.

petes1234a at 2007-7-28 18:46:25 > top of Java-index,Desktop,Core GUI APIs...
# 5

yes, it's right

i continue my work now...

thank you

leonard_shelbya at 2007-7-28 18:46:25 > top of Java-index,Desktop,Core GUI APIs...
# 6

a question (theoric...)

what's the difference between ..

this instance :

JPanel [] panInsCantante = new JPanel[3];

and this instance of panInsCantante? :

panInsCantante[i] = new JPanel();

ps. why use both them?

Message was edited by:

leonard_shelby

leonard_shelbya at 2007-7-28 18:46:25 > top of Java-index,Desktop,Core GUI APIs...
# 7

> a question (theoric...)

A VERY important question I may add.

> what's the difference between ..

>

> this instance :

>JPanel [] panInsCantante = new

> JPanel[3];

This declares and initializes the array itself, nothing more. No JPanels have been initialized as yet, just the array. It's as if you have now built the shelves to hold the books, but you have no books up there yet...

> and this instance of panInsCantante? :

> panInsCantante[i] = new JPanel();

and this initializes each JPanel in the array as you loop through the array. .... and now you have filled the shelves with the books and can use the books.

You will need them both.

In all likelihood, the reason for your confusion is that all of your previous arrays were arrays of primative types: int, double, float, and char. In this situation, you don't need to go through the step of initializing the variable.

But now you are faced for the first time with an array of reference type, an array of Objects. This is a different situation and requires the steps that we have gone through.

Message was edited by:

petes1234

petes1234a at 2007-7-28 18:46:25 > top of Java-index,Desktop,Core GUI APIs...