Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Contain

  Asked By: Madison    Date: Jul 07    Category: Java    Views: 6197
  

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:625)
at java.awt.Container.add(Container.java:307)
at Checkerboard.<init>(Checkerboard.java:31)
at Checkerboard.main(Checkerboard.java:95)

Can any1 help me understand what is wrong with this code ?
Here is the Code......................................................

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Checkerboard extends Frame implements ActionListener
{
TextField checkerArray[];
Panel mainPad = new Panel();
Panel checkerRows = new Panel();
Panel fieldRow = new Panel();
TextField firstText = new TextField(5);
TextField scndText = new TextField(5);
TextField thirdText = new TextField(5);
int start, stop, step;
String temp;

public Checkerboard()
{
BufferedReader dataIn = new BufferedReader(new
InputStreamReader(System.in));
checkerArray = new TextField[16];
Button Clear = new Button("Clear");
Button Go = new Button("Go");
start = 0;
stop = 0;
step = 0;

setLayout(new BorderLayout());

checkerRows.setLayout(new GridLayout(4,4));
for(int f=0; f<checkerArray.length; f++)
checkerRows.add(checkerArray[f]);

fieldRow.setLayout(new GridLayout(2,3));
fieldRow.add(firstText, BorderLayout.SOUTH);
fieldRow.add(scndText, BorderLayout.SOUTH);
fieldRow.add(thirdText, BorderLayout.SOUTH);
fieldRow.add(Clear,BorderLayout.CENTER);
fieldRow.add(Go,BorderLayout.CENTER);

mainPad.add(checkerRows);
mainPad.add(fieldRow);
add(mainPad);


addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent
e)
{
System.exit(0);
}
}
);
}
public void init()
{
for(int i = 0; i<=16; i++)
{
checkerArray[i].setText("i");
checkerArray[i].setEditable(false);
}

for(int f = 0; f < checkerArray.length; f++)
checkerArray[f].setBackground(Color.magenta);

for(int i = 0; i < checkerArray.length; i++)
checkerArray[i].addActionListener(this);

}

public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if(arg == "Go")
{
temp = firstText.getText();
start = Integer.parseInt(temp);
temp = scndText.getText();
stop = Integer.parseInt(temp);
temp = thirdText.getText();
step = Integer.parseInt(temp);

}
if(arg == "Clear")
{
firstText.setText("");
scndText.setText("");
thirdText.setText("");
}

}

public static void main(String args[])
{
Checkerboard checkerFrame = new Checkerboard();

checkerFrame.setBounds(50,100,300,400);
checkerFrame.setTitle("Checker Board");
checkerFrame.setVisible(true);
}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Jared Adams     Answered On: Jul 07

Try making the for loop as follows, otherwise, initialize the individual
elements of the checkerArray before sending in to the checkerRows. The
problem is you initialize the array. that is fine. But, you havenot
initialized the individual objects (for eg., checkerArray[0]) which you pass
in as parameter.

for(int f=0; f<checkerArray.length; f++)
{
checkerArray[f] = new TextField();
checkerRows.add(checkerArray[f]);
}