Null Pointer Exception

Hi.

I was making an Applet that would input two numbers in two

text-boxes and display their sum in a third text-box when the

button was clicked.

I was trying to use a Grid Layout, using 16 blank Labels to fill

the spaces...

Here's the code:

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

publicclass Adderextends Applet

implements ActionListener

{

TextField text1, text2, text3;

Button button1;

Label label1;

Label spacer[];/*spacer2, spacer3, spacer4, spacer5,

spacer6, spacer7, spacer8, spacer9, spacer10,

spacer11, spacer12, spacer15, spacer14, spacer15,

spacer16 ;*/

publicvoid init()

{

setLayout(new GridLayout(9,3));

spacer =new Label[16];

add(spacer[0]);

text1 =new TextField(10);

add(text1);

add(spacer[1]);

add(spacer[2]);

label1 =new Label("+");

add(label1);

add(spacer[3]);

add(spacer[4]);

text2 =new TextField(10);

add(text2);

add(spacer[5]);

//blank line

add(spacer[6]);

add(spacer[7]);

add(spacer[8]);

add(spacer[9]);

button1 =new Button("=");

add(button1);

add(spacer[10]);

button1.addActionListener(this);

//blank line

add(spacer[11]);

add(spacer[12]);

add(spacer[13]);

add(spacer[14]);

text3 =new TextField(10);

add(text3);

add(spacer[15]);

}

publicvoid actionPerformed(ActionEvent e)

{

if(e.getSource() == button1 )

{

int sum = Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText());

text3.setText(String.valueOf(sum));

}

}

}

I want to use an array here, to save the typing effort.

However, the Applet is throwing a null pointer exception.

Please ask if anything is not clear.

Any help is welcome...

[3019 byte] By [horizon981a] at [2007-10-3 6:02:17]
# 1
this does not create the labelsspacer = new Label[16];this doeslabel1 = new Label("+");
Michael_Dunna at 2007-7-15 0:44:28 > top of Java-index,Java Essentials,New To Java...
# 2
spacer = new Label[16];This creates an array with enough space to hold 16 Labels. It DOES NOT create the 16 Labels for you!
floundera at 2007-7-15 0:44:28 > top of Java-index,Java Essentials,New To Java...
# 3

> spacer = new Label[16];

>

> This creates an array with enough space to hold 16

> Labels. It DOES NOT create the 16 Labels for you!

So, If I want 1000 Labels in my Applet, I must declare 1000 variables like

Label label1, label2,..., label1000?

Can't I use this array of 16 Label space somehow?

horizon981a at 2007-7-15 0:44:28 > top of Java-index,Java Essentials,New To Java...
# 4

> > spacer = new Label[16];

> >

> > This creates an array with enough space to hold 16

> > Labels. It DOES NOT create the 16 Labels for you!

>

> So, If I want 1000 Labels in my Applet, I must

> declare 1000 variables like

> Label label1, label2,..., label1000?

>

> Can't I use this array of 16 Label space somehow?

No, you can create new Label[1000]; but you will have an array containing 1000 nulls each of which could potentially store a reference to a Label. There's nothing unless you initialize.

aniseeda at 2007-7-15 0:44:28 > top of Java-index,Java Essentials,New To Java...
# 5

> > spacer = new Label[16];

> >

> > This creates an array with enough space to hold 16

> > Labels. It DOES NOT create the 16 Labels for you!

>

> So, If I want 1000 Labels in my Applet, I must

> declare 1000 variables like

> Label label1, label2,..., label1000?

>

> Can't I use this array of 16 Label space somehow?

You can use te array. The new Label[16] creates an array with 16 references to Label. All those references are initially null--they don't point to label objects yet. Usually the next step is to loop over the array, calling new Label() and assigning the resulting reference for each element.

jverda at 2007-7-15 0:44:28 > top of Java-index,Java Essentials,New To Java...
# 6

Thanks a lot!!

Here's the revised code that works!

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

public class Adder extends Applet

implements ActionListener

{

TextField text1, text2, text3;

Button button1;

Label label1;

Label spacer[];

public void init()

{

setLayout(new GridLayout(9,3));

spacer = new Label[16];

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

spacer[i] = new Label();

add(spacer[0]);

text1 = new TextField(10);

add(text1);

add(spacer[1]);

add(spacer[2]);

label1 = new Label("+");

add(label1);

add(spacer[3]);

add(spacer[4]);

text2 = new TextField(10);

add(text2);

add(spacer[5]);

//blank line

add(spacer[6]);

add(spacer[7]);

add(spacer[8]);

add(spacer[9]);

button1 = new Button("=");

add(button1);

add(spacer[10]);

button1.addActionListener(this);

//blank line

add(spacer[11]);

add(spacer[12]);

add(spacer[13]);

add(spacer[14]);

text3 = new TextField(10);

add(text3);

add(spacer[15]);

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == button1 )

{

int sum = Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText());

text3.setText(String.valueOf(sum));

}

}

}

horizon981a at 2007-7-15 0:44:28 > top of Java-index,Java Essentials,New To Java...
# 7

Cool! Congrats!

A couple of points:

* It's generally considered better practice to always use braces around the bodies of if, for, etc., even if it's only a single line. It makes it easier to spot what's in the body and makes it less likely that you'll forget to add them if you add more lines later.

* Now that you've learned how to create and populate an arrray, I'm afraid I have to tell you that it appears that this particular array is useless. You don't seem to be doing anything with the elements after creation other than adding them to your applet. If that's all you're doing--if you never need to refer to them again after calling add(), then you could get rid of the array completely and just do this: add(text1);

add(new Label());

* I don't do GUIs, so I don't know, but it sems to me that there might be a better way to lay this out than to use Labels for spacers. I know there are layout managers, but I don't know if they're in awt or Swing, and I do know that they can be kind of hard to figure out.

* You might want to use Swing instead of awt. I think there's a Swing version of Applet--probably JApplet.

jverda at 2007-7-15 0:44:28 > top of Java-index,Java Essentials,New To Java...
# 8

> * I don't do GUIs, so I don't know, but it sems to me

> that there might be a better way to lay this out than

> to use Labels for spacers. I know there are layout

> managers, but I don't know if they're in awt or

> Swing, and I do know that they can be kind of hard to

> figure out.

>

> * You might want to use Swing instead of awt. I think

> there's a Swing version of Applet--probably JApplet.

Yes there is a JApplet, and yes Swing might be good to use for this.

An alternative to GridLayout is GridBagLayout. It looks hard to use at first (and I guess there is a bit of a learning curve), but you can do almost any layout with it, and it is certainly appropriate for a Calculator. Its main advantage over GridLayout is that you can have columns or rows of different sizes from each other. You also don't need to fill in empty cells with some dummy component such as your spacers.

Also, have a look at the Box class which is good for making spacers of various shapes and sizes - particularly when you want one that will expand and contract as the window changes size.

Finally, one caveat with the GridBagLayout - if you want any components to expand to fit the available space rather than bunching in the middle, you need to set the weightx and/or weighty of the GridBagConstraints that you use when adding at the component.

TimRyanNZa at 2007-7-15 0:44:28 > top of Java-index,Java Essentials,New To Java...