mbunkows
asked on
NullPointerException
This seems like a really dumb question
(but ill as it anyway)
The easiest way to describe it would just be to give you the
code:
import java.awt.*;
import java.awt.event.*;
public class WandGui extends Frame {
String wands[];
Checkbox checkbox[];
WandGui(String filename) {
super("Wand Selection");
wands= getWandFile(filename);
this.setLayout(new BorderLayout());
this.setBackground(Color.l ightGray);
Font labelFont= new Font("SansSerif",Font.PLAI N,14);
Panel p1= new Panel();
p1.setLayout(new GridLayout(0,1,10,10));
CheckboxGroup checkbox_group= new CheckboxGroup();
Checkbox[] checkbox= new Checkbox[wands.length];
Label label = new Label("Choose a wand from the List below:");
label.setFont(labelFont);
p1.add(label);
for (int i=0;i<wands.length;i++) {
checkbox[i]= new Checkbox("Wand " + wands[i],checkbox_group,fa lse);
checkbox[i].setFont(labelF ont);
p1.add(checkbox[i]);
}
Panel p2= new Panel();
p2.setLayout(new FlowLayout(FlowLayout.CENT ER,10,10)) ;
ButtonListener buttonlistener= new ButtonListener();
Button okbutton= new Button("OK");
okbutton.addActionListener (buttonlis tener);
okbutton.setActionCommand( "ok");
p2.add(okbutton);
Button cancelbutton= new Button("Cancel");
cancelbutton.addActionList ener(butto nlistener) ;
cancelbutton.setActionComm and("cance l");
p2.add(cancelbutton);
this.add(BorderLayout.WEST ,new Label(" "));
this.add(BorderLayout.EAST ,new Label(" "));
this.add(BorderLayout.CENT ER,p1);
this.add(BorderLayout.SOUT H,p2);
}
public static void main(String args[]) {
WandGui wg= new WandGui("file.txt");
wg.pack();
wg.setVisible(true);
}
private String[] getWandFile(String filename) {
String wands[]={"AAA","BBB","CCC" ,"DDD","EE E","FFF"};
return wands;
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEven t e) {
if (e.getActionCommand().equa ls("ok")) {
System.out.println(checkbo x); //comes up as null here
for (int i=0;i<checkbox.length;i++) {
System.out.println("STATE: " + checkbox[i].getState());
if (checkbox[i].getState())
System.out.println("Pickin g " + wands[i]);
}
}
System.exit(0);
}
}
}
Notice in the event code I try to access the checkboxes that I initialized in the constructor...
why doesnt it work? and what do I need to do differently?
(but ill as it anyway)
The easiest way to describe it would just be to give you the
code:
import java.awt.*;
import java.awt.event.*;
public class WandGui extends Frame {
String wands[];
Checkbox checkbox[];
WandGui(String filename) {
super("Wand Selection");
wands= getWandFile(filename);
this.setLayout(new BorderLayout());
this.setBackground(Color.l
Font labelFont= new Font("SansSerif",Font.PLAI
Panel p1= new Panel();
p1.setLayout(new GridLayout(0,1,10,10));
CheckboxGroup checkbox_group= new CheckboxGroup();
Checkbox[] checkbox= new Checkbox[wands.length];
Label label = new Label("Choose a wand from the List below:");
label.setFont(labelFont);
p1.add(label);
for (int i=0;i<wands.length;i++) {
checkbox[i]= new Checkbox("Wand " + wands[i],checkbox_group,fa
checkbox[i].setFont(labelF
p1.add(checkbox[i]);
}
Panel p2= new Panel();
p2.setLayout(new FlowLayout(FlowLayout.CENT
ButtonListener buttonlistener= new ButtonListener();
Button okbutton= new Button("OK");
okbutton.addActionListener
okbutton.setActionCommand(
p2.add(okbutton);
Button cancelbutton= new Button("Cancel");
cancelbutton.addActionList
cancelbutton.setActionComm
p2.add(cancelbutton);
this.add(BorderLayout.WEST
this.add(BorderLayout.EAST
this.add(BorderLayout.CENT
this.add(BorderLayout.SOUT
}
public static void main(String args[]) {
WandGui wg= new WandGui("file.txt");
wg.pack();
wg.setVisible(true);
}
private String[] getWandFile(String filename) {
String wands[]={"AAA","BBB","CCC"
return wands;
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEven
if (e.getActionCommand().equa
System.out.println(checkbo
for (int i=0;i<checkbox.length;i++)
System.out.println("STATE:
if (checkbox[i].getState())
System.out.println("Pickin
}
}
System.exit(0);
}
}
}
Notice in the event code I try to access the checkboxes that I initialized in the constructor...
why doesnt it work? and what do I need to do differently?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
someone slap me :)
Thanks