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

    Default NullPointerException() problem


    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(){
    } catch (NullPointerException e){
    /* //(0,3)
    //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());

    public void paintChildren(Graphics g) {

    public void paintComponent(Graphics g) {
    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,


  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    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

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