6 Replies - 244 Views - Last Post: 11 May 2013 - 05:03 PM Rate Topic: -----

#1 CaseyJava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-March 13

Null Pointer Exception

Posted 11 May 2013 - 04:16 PM

It's our last project and I'm writing code for the minesweeper game. I think I have everything pretty much there, but getting a null pointer exception error which says "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at board.paint(board.java:220)" There are also many lines below that, but don't think thats pertinent. So I named my images 0.bmp, 1.bmp, 2.bmp etc. I'll just paste the part of my code with the variables, the board class and the paint class. This should be enough to maybe determine why I'm running into problems.
public class board extends JPanel{
	
	private final int NUM_IMAGES = 13;
    private final int CELL_SIZE = 15;

    private final int COVER_FOR_CELL = 10;
    private final int MARK_FOR_CELL = 10;
    private final int EMPTY_CELL = 0;
    private final int MINE_CELL = 9;
    private final int COVERED_MINE_CELL = MINE_CELL + COVER_FOR_CELL;
    private final int MARKED_MINE_CELL = COVERED_MINE_CELL + MARK_FOR_CELL;

    private final int DRAW_MINE = 9;
    private final int DRAW_COVER = 10;
    private final int DRAW_MARK = 11;
    private final int DRAW_WRONG_MARK = 12;

    private int[] field;
    private boolean inGame;
    private int mines_left;
    private Image[] img;
    private int mines = 40;
    private int rows = 16;
    private int cols = 16;
    private int all_cells;
    private JLabel statusbar;


    public void Board(JLabel statusbar) {

        this.statusbar = statusbar;

        img = new Image[NUM_IMAGES];

        for (int i = 0; i < NUM_IMAGES; i++) {
            img[i] =
                    (new ImageIcon(this.getClass().getResource((i)
                        + ".bmp"))).getImage();
        }

        setDoubleBuffered(true);

        addMouseListener(new minesadapter());
        newGame();
    }
      public void paint(Graphics g) {

        int cell = 0;
        int uncover = 0;

        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
            	
                cell = field[(i * cols) + j];

                if (inGame && cell == MINE_CELL)
                    inGame = false;

                if (!inGame) {
                    if (cell == COVERED_MINE_CELL) {
                        cell = DRAW_MINE;
                    } else if (cell == MARKED_MINE_CELL) {
                        cell = DRAW_MARK;
                    } else if (cell > COVERED_MINE_CELL) {
                        cell = DRAW_WRONG_MARK;
                    } else if (cell > MINE_CELL) {
                        cell = DRAW_COVER;
                    }


                } else {
                    if (cell > COVERED_MINE_CELL)
                        cell = DRAW_MARK;
                    else if (cell > MINE_CELL) {
                        cell = DRAW_COVER;
                        uncover++;
                    }
                }

                g.drawImage(img[cell], (j * CELL_SIZE),
                    (i * CELL_SIZE), this);
            }
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Null Pointer Exception

#2 CaseyJava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-March 13

Re: Null Pointer Exception

Posted 11 May 2013 - 04:28 PM

and the actual area of the java.lang.NullPointerException would be line 55 on the code I pasted, Not line 220 like which is what I pasted. The line that says
cell = field[(i * cols) + j];

Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: Null Pointer Exception

Posted 11 May 2013 - 04:38 PM

I don't see you initializing the array. You declare it here
 private int[] field;



But in what you've shown us you never create the array. That would cause a nullpointer exception.
Was This Post Helpful? 1
  • +
  • -

#4 CaseyJava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-March 13

Re: Null Pointer Exception

Posted 11 May 2013 - 04:46 PM

View Postjon.kiparsky, on 11 May 2013 - 04:38 PM, said:

I don't see you initializing the array. You declare it here
 private int[] field;



But in what you've shown us you never create the array. That would cause a nullpointer exception.


lol, wow. Thanks
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: Null Pointer Exception

Posted 11 May 2013 - 04:47 PM

No problem. I love the easy ones... :)
Was This Post Helpful? 0
  • +
  • -

#6 CaseyJava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 22-March 13

Re: Null Pointer Exception

Posted 11 May 2013 - 04:53 PM

View Postjon.kiparsky, on 11 May 2013 - 04:47 PM, said:

No problem. I love the easy ones... :)/>


So I put field = new int[] but I don't know what value to put in the brackets. I tried 169 because its a 13 by 13 board but then I get the array index out of bounds error. Because the value is taking an x and a y number, I'm not exactly sure how to represent this number in the brackets.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: Null Pointer Exception

Posted 11 May 2013 - 05:03 PM

i* cols +j is a good way to represent a (i,j) in a one-dimensional array. I'm not sure off hand why you're getting out of bounds, but if you need to address field[169] then you have to have an array of at least 170 length, since arrays are zero-indexed.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1