Results 1 to 2 of 2
Thread: NullPointerException() problem
- 02-02-2010, 03:13 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 1
- Rep Power
- 0
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
-
Ah, you are now learning the difference between an array of primitive, say an int array, and an array of objects, say an Element array. With the primitive array, all you have to do is declare the array and use it -- it's ready immediately for your use.
With an object array on the other hand, you have to declare the array first, and then you have to initialize the objects in the array before you can use them. Think of creating an object array as if you were creating a parking lot. You have to fill it with cars first before you can use a car.
Consider using nested for loops to go through your element 2D array and create new element objects as you iterate through the loops. Also, never "catch" null pointer exceptions, and consider using code tags (please see my signature). Much luck.Last edited by Fubarable; 02-02-2010 at 03:27 PM.
Similar Threads
-
NullPointerException problem
By Kris in forum New To JavaReplies: 4Last Post: 10-01-2009, 02:34 PM -
NullPointerException
By mjz in forum JDBCReplies: 1Last Post: 08-06-2009, 11:46 AM -
NullPointerException
By mensa in forum Java 2DReplies: 5Last Post: 05-03-2008, 11:19 PM -
nullPointerException problem
By conandor in forum NetworkingReplies: 1Last Post: 08-14-2007, 01:22 PM -
NullPointerException problem
By warship in forum AWT / SwingReplies: 5Last Post: 08-10-2007, 04:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks