0

I am writing a minesweeper game in java and I a keep getting the "Exception in thread "main" java.lang.NullPointerException" error. I know this means that I'm trying to use a null variable, but I'm not sure why the variable hasn't been initialized.

Here is my code:

Minesweeper Class

import java.util.*;

public class MineSweeper {

   private int numrows = 4;
   private int numcols = 4;
   private int numcells = numrows*numcols;
   private int nummines = 4;
   private Cell[][] cells; 

   // set up scanner for input
   Scanner in = new Scanner(System.in);


   public MineSweeper() {

      cells = new Cell[numrows][numcols];
      int minesPlaced = 0;
      Random random = new Random();

      // checking if any more mines need to be placed
      while (minesPlaced < nummines) {

         // picking random row and column
         int x = random.nextInt(numrows);
         int y = random.nextInt(numcols);
         Cell c = cells[x][y];

         // placing mines

         if (c.getMined() == false) {  
            c.setMined(true);
            minesPlaced++;
         } // end if
      } // end while

      for (int i=0; i<numrows; i++) {
         for (int j=0; j<numcols; j++) {  
            Cell b = cells[i][j];
            int num = getAdjMineCount(i,j);
            b.setAdjCount(num);
         } // end for
      } // end for
   } // end constructor MineSweeper



   public int getAdjMineCount(int i, int j) {

      Cell a = cells[i-1][j-1];
      Cell b = cells[i-1][j];
      Cell c = cells[i-1][j+1];
      Cell d = cells[i][j-1];
      Cell e = cells[i][j+1];
      Cell f = cells[i+1][j-1];
      Cell g = cells[i+1][j];
      Cell h = cells[i+1][j+1];


      int mines = 0;
      /* if (getAdjCell(i,j,0) = isMined) mines++;
      if (getAdjCell(i,j,1) = isMined) mines++;
      if (getAdjCell(i,j,2) = isMined) mines++;
      if (getAdjCell(i,j,3) = isMined) mines++;
      if (getAdjCell(i,j,4) = isMined) mines++;
      if (getAdjCell(i,j,5) = isMined) mines++;
      if (getAdjCell(i,j,6) = isMined) mines++;      
      if (getAdjCell(i,j,7) = isMined) mines++; */

      if (a.getMined() == true) mines++;
      if (b.getMined() == true) mines++;
      if (c.getMined() == true) mines++;
      if (d.getMined() == true) mines++;
      if (e.getMined() == true) mines++;
      if (f.getMined() == true) mines++;
      if (g.getMined() == true) mines++;
      if (h.getMined() == true) mines++; 

      return mines;
   }



   public int getMarkCount() {

      int marks = 0;

      for (int i=0; i<numrows; i++) {
         for (int j=0; j<numcols; j++) {
            Cell c = cells[i][j];
            if (c.getMarked() == true) {
               marks++;
            } // end if
         } // end for
      } // end for

      return marks;
   } // end getMarkCount



   public void show() {

      for (int i=0; i<numrows; i++) {
         for (int j=0; j<numcols; j++) {  
            Cell c = cells[i][j];
            c.show();
         } // end for
      } //end for
   }



   public void uncoverAll() {

      for (int i=0; i<numrows; i++) {
         for (int j=0; j<numcols; j++) {  
            Cell c = cells[i][j];
            c.setUncovered();
         } // end for
      } // end for
   } // end uncoverAll



   public Cell getAdjCell(int r, int c, int direction) {

      // checking if cells are off the board
      if (r == 0 && (direction == 1 || direction == 2 || direction == 3))
         return null;
      if (r == 3 && (direction == 5 || direction == 6 || direction == 7))
         return null;
      if (c == 0 && (direction == 3 || direction == 4 || direction == 5))
         return null;
      if (c == 3 && (direction == 1 || direction == 0 || direction == 7))
         return null;

      // checking each direction
      if (direction == 0) return cells[r][c+1];
      else if (direction == 1) return cells[r-1][c+1];
      else if (direction == 2) return cells[r-1][c];
      else if (direction == 3) return cells[r-1][c-1];
      else if (direction == 4) return cells[r][c-1];
      else if (direction == 5) return cells[r+1][c-1];
      else if (direction == 6) return cells[r+1][c];
      else if (direction == 7) return cells[r+1][c+1];

      else return null;
   }



   public boolean allNonMinesUncovered() {

      int count = 0;

      for (int i=0; i<numrows; i++) {
         for (int j=0; j<numcols; j++) {
            Cell c = cells[i][j];
            if (c.getMined() == false && c.getCovered() == false) {
               count ++;
            } // end if
         } // end for
      } // end for 

      if (count == (numcells - nummines)) {
         return true; 
      }
      else 
         return false;
   } // end allNonMinesUncovered()



   public boolean allMinesMarked() {

      int count = 0;

      for (int i=0; i<numrows; i++) {
         for (int j=0; j<numcols; j++) {
            Cell c = cells[i][j];
            if (c.getMarked() == true && c.getMined() == true) {
               count++;  
            } // end if
         } // end for 
      } // end for

      if (count == nummines) {
         return true;
      }
      else
         return false;   
   } // end allMinesMarked()



   public void game() {

      // print prompt and take input
      System.out.print(">");
      String string1 = in.next();

      while (string1.length() != 0 ){

         // splitting newArray into command and arguments
         String[] newArray = string1.split("\\s");
         String command = newArray[0];
         int arg1 = Integer.parseInt(newArray[1]);
         int arg2 = Integer.parseInt(newArray[2]); 

         Cell c = cells[arg1][arg2];
         if (command == "show") { 
            show(); // prints the board
         }
         else if (command == "u") {
            if (c.getMarked() == true) {
               break; }
            else if (c.getMined() == true) {
               uncoverAll();
               System.out.println("You lose!"); }
            else {
               c.setUncovered();
               if (allMinesMarked() == true && allNonMinesUncovered() == true) {
                  System.out.println("You win!");
                  break;
               } // end if
            } // end else
         } // end else if for "u"
         else if (command == "m") {
            if (c.getCovered() == false) {
               break; }
            else if (c.getMarked() == true) {
               c.setMarked(true); }
            else {
               c.setMarked(true); 
               if (allMinesMarked() == true && allNonMinesUncovered() == true) {
                  System.out.println("You win!");
                  break;
               } // end if
            } // end else
         } // end else if for "m"
         else if (command == "q") {
            break; // exits while loop
         }
         else {
            System.out.println("Bad command.");
         }

         System.out.print(">");
         string1 = in.next();
      } // end while         
   } // end game()



   public static void play() {
         MineSweeper mine = new MineSweeper();
         mine.game();
   } // end play()

} //end class MineSweeper

Cell Class

    import java.util.*;  

    public class Cell { 

   private int row, col;
   private boolean covered;
   private boolean marked, mined;
   private int adjcount;

   public Cell(int r, int c) {

      row = r;
      col = c;
      covered = true;
      marked = false;
      mined = false;
      adjcount = 0;

   }

   public void show() {

      if (covered==true && marked==false)
      System.out.print("?");

      if (covered==true && marked==true)
      System.out.print("X");

      if (covered==false && mined==true)
      System.out.print("*");

      if (covered==false && mined==false && adjcount==0)
      System.out.print("_");

      if (covered==false && mined==false && adjcount > 0)
      System.out.print(adjcount);

   }

   public boolean getMined() {
      return mined;
   }
   public void setMined(boolean m) {
      mined = m;
   }
   public boolean getCovered() {
      return covered;
   }
   public void setUncovered() {
      covered = false;
   }
   public boolean getMarked() {
      return marked; 
   }
   public void setMarked(boolean m) {
      marked = m;
   }
   public int getAdjCount() {
      return adjcount;
   }
   public void setAdjCount(int c) {
      adjcount = c;
   }
   public int getRow() {
      return row; 
   }
   public int getCol() {
      return col; 
   }

}

Driver Class

public class Driver {

     public static void main(String[] args) {
         MineSweeper.play();
     } //end of main

} //end of class Driver

I'm getting the error on line 31 of class Minesweeper

if (c.getMined() == false) {

getMined() is returning the variable 'mined', but apparently 'mined' has not been initialized yet. I'm confused because I thought 'mined' was getting initialized to false when the 'cells' object was created through the Cell constructor. My code is nearly identical to my instructors final code so I'm not sure where my mistake is. Any ideas?

Stacktrace:

Exception in thread "main" java.lang.NullPointerException
    at MineSweeper.<init>(MineSweeper.java:31)
    at MineSweeper.play(MineSweeper.java:257)
    at Driver.main(Driver.java:4)
  • It'd be great if you gave us the exact stacktrace and position of exception – Andrew Li Sep 14 '16 at 21:52
  • 1
    Are you sure it is c.getMined() that is throwing the error? It looks to me like c would be null at this point since you haven't populated your array. – blue Sep 14 '16 at 21:54
  • `mined` is a boolean, not an object reference which could be null, and `Cell.getMined()` does not appear in your stack trace. Obviously it is `c` itself which is null. – user207421 Sep 14 '16 at 22:03

0 Answers0