Solved

NullPointerException

Posted on 1998-11-18
2
194 Views
Last Modified: 2010-03-30
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.lightGray);
          Font labelFont= new Font("SansSerif",Font.PLAIN,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,false);
               checkbox[i].setFont(labelFont);
               p1.add(checkbox[i]);
          }
         
          Panel p2= new Panel();
          p2.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
         
          ButtonListener buttonlistener= new ButtonListener();
         
          Button okbutton= new Button("OK");
          okbutton.addActionListener(buttonlistener);
          okbutton.setActionCommand("ok");
          p2.add(okbutton);
          Button cancelbutton= new Button("Cancel");
          cancelbutton.addActionListener(buttonlistener);
          cancelbutton.setActionCommand("cancel");
          p2.add(cancelbutton);
         
         
          this.add(BorderLayout.WEST,new Label("  "));
          this.add(BorderLayout.EAST,new Label("  "));
          this.add(BorderLayout.CENTER,p1);
          this.add(BorderLayout.SOUTH,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","EEE","FFF"};
         
          return wands;
     }
     
     class ButtonListener implements ActionListener  {
          public void actionPerformed(ActionEvent e)  {
               if (e.getActionCommand().equals("ok"))  {  
                    System.out.println(checkbox);    //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("Picking " + 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?

0
Comment
Question by:mbunkows
2 Comments
 
LVL 5

Accepted Solution

by:
msmolyak earned 50 total points
ID: 1227717
Look at the first and the last lines of the code below. The first line declares a instance variable while the last declares the local to constructor variable with the same name. The former one is never initialize. Remove the initializer from the last line.

Checkbox checkbox[];
         
         WandGui(String filename)  {

              super("Wand Selection");
               
              wands= getWandFile(filename);
               
               
              this.setLayout(new BorderLayout());
              this.setBackground(Color.lightGray);
              Font labelFont= new Font("SansSerif",Font.PLAIN,14);
               
              Panel p1= new Panel();
              p1.setLayout(new GridLayout(0,1,10,10));
               
              CheckboxGroup checkbox_group= new CheckboxGroup();
              Checkbox[] checkbox= new Checkbox[wands.length];      
0
 

Author Comment

by:mbunkows
ID: 1227718
ARRGGGHHHHH
someone slap me  :)

Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Systems talking to each other 5 123
JDeveloper 12c for 32 bit 4 68
Spring Framework HTTPSession management 1 22
session migration servlets 2 24
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now