NullPointerException() problem
Hello,
I'm trying to implement Nine Men's Morris game. For drawing the little ovals representing the moves, I'm using the Element class. This is used as a matrix in MorrisElement class. But when I'm trying to draw one element, NullPointerException() is throwed. When I was implementing the MorrisElement class, I didn't use try-catch. After the first run - without result - I put message into the NullPointerException part, and this message is shown when the program is running.
I'm new in Java, and haven't already met with this kind of problem, and I have no idea what to do to resolve it, and to draw the little ovals as it has to be.
Here are the classes:
//Element class
public class Element {
int xCoord, yCoord;
String color;
boolean isDrawn;
public Element(){
}
public void setX(int x){
xCoord = x;
}
public void setY(int y){
yCoord = y;
}
public void setColor(String col){
color = col;
}
public void drawn(boolean bool){
isDrawn = bool;
}
public int getX(){
return xCoord;
}
public int getY(){
return yCoord;
}
public boolean ifDrawn(){
return isDrawn;
}
}
//The MorrisElement class, using the Element class as a matrix
public class MorrisElement {
Element[][] element = new Element[6][6];
public MorrisElement(){
//(0,0)
try{
element[0][0].setX(82);
element[0][0].setY(30);
element[0][0].drawn(true);
} catch (NullPointerException e){
System.out.println("ex");
}
/* //(0,3)
element[0][3].setX(306);
element[0][3].setY(30);
element[0][3].drawn(true);
//(0,6)
element[0][6].setX(531);
element[0][6].setY(30);
element[0][6].drawn(true);
//(1,1)
//and so on with the other coords */
}
//The class where I'm trying to draw:
import java.awt.*;
import javax.swing.*;
public class MorrisDraw extends JPanel {
int x,y;
MorrisElement me = new MorrisElement();
public MorrisDraw() {
ImagePanel imgPanel = new ImagePanel(new ImageIcon("morrisBackground.jpg").getImage());
add(imgPanel);
System.out.println("hey");
}
public void paintChildren(Graphics g) {
}
public void paintComponent(Graphics g) {
super.paintChildren(g);
g.setColor(Color.blue);
for (int i=0; i<6; i++)
for (int j=0; j<6; j++){
if (me.element[i][j].isDrawn){
x = me.element[i][j].getX();
y = me.element[i][j].getY();
g.fillOval(x, y, 25, 25);
}
}
}
}
Thank you for the help,
Molly