I've been working on creating a periodic table for a computer science project. I'm trying to write a code that creates an array of buttons. The text on these buttons is determined by retrieving the name of the buttons corresponding element in my other array. After this is complete, based on what type of element it is, the button is coloured accordingly. The program compiles fine, but it does not run. Here is my code:
BufferedReader reader = null;
ElementsTest[] element = new ElementsTest[40];
try {
File file = new File("Elements 41-80.txt");
reader = new BufferedReader(new FileReader(file));
for (int counter = 0 ; counter < 40 ; counter++)
{
String name = reader.readLine();
int atomicNum = counter + 1;
String atomicWeight = reader.readLine();
String elementSymbol = reader.readLine();
String elementCharge = reader.readLine();
String fullElectronConfig = reader.readLine();
String shortElectronConfig = reader.readLine();
String elementState = reader.readLine();
String elementType = reader.readLine();
String density = reader.readLine();
String meltingPoint = reader.readLine();
String boilingPoint = reader.readLine();
String emptyLine = reader.readLine();
element[counter] = new ElementsTest (name, atomicNum, atomicWeight, elementSymbol, elementCharge, fullElectronConfig, shortElectronConfig, elementState, elementType, density, meltingPoint, boilingPoint);
}
} catch (IOException e) {
e.printStackTrace();
}
JButton[] buttonArray = new JButton[40];
for (int counter2 = 0 ; counter2 < 40 ; counter2++)
{
String currentSymbol = element[counter2].getElementSymbol();
buttonArray[counter2].setText(currentSymbol);
if (element[counter2].getElementType().equals("Metal"))
{
buttonArray[counter2].setBackground(Color.ORANGE);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Non-Metal"))
{
buttonArray[counter2].setBackground(Color.GRAY);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Noble Gas"))
{
buttonArray[counter2].setBackground(Color.BLACK);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Transition Metal"))
{
buttonArray[counter2].setBackground(Color.GREEN);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Lanthanide"))
{
buttonArray[counter2].setBackground(Color.YELLOW);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Alkali Metal"))
{
buttonArray[counter2].setBackground(Color.RED);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Alkaline Earth Metal"))
{
buttonArray[counter2].setBackground(Color.BLUE);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
if (element[counter2].getElementType().equals("Halogen"))
{
buttonArray[counter2].setBackground(Color.MAGENTA);
buttonArray[counter2].setOpaque(true);
buttonArray[counter2].setBorderPainted(false);
}
else {
buttonArray[counter2].setOpaque(true);
}
}
After pressing RUN I get this error:
java.lang.NullPointerException
at TestLayout.main(TestLayout.java:136)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
Any suggestions??