Results 1 to 2 of 2
  1. #1
    mollybaba4 is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 04:27 PM.

Similar Threads

  1. NullPointerException problem
    By Kris in forum New To Java
    Replies: 4
    Last Post: 10-01-2009, 03:34 PM
  2. NullPointerException
    By mjz in forum JDBC
    Replies: 1
    Last Post: 08-06-2009, 12:46 PM
  3. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-04-2008, 12:19 AM
  4. nullPointerException problem
    By conandor in forum Networking
    Replies: 1
    Last Post: 08-14-2007, 02:22 PM
  5. NullPointerException problem
    By warship in forum AWT / Swing
    Replies: 5
    Last Post: 08-10-2007, 05:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •