Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:59:34+00:00 2026-05-29T08:59:34+00:00

So I’m a computer science student and a fledgling programmer in Java. Someone asked

  • 0

So I’m a computer science student and a fledgling programmer in Java. Someone asked me to help them on an assignment where they have to create a pretty basic minesweeper program. This program doesn’t utilize flagging mines at all, but aside from that it is functionally the same as any other mine sweeper game.

I’m running into a NullPointerException when I try to run the program. I’ve researched what this could mean and now know that this should really be a NoObjectException or DereferenceException, but I’m still no closer to solving the issue.

This exception arises when the makeField method of the Tile class is called. Also, I’m really trying to wrap my head around proper inheritance, static vs. non-static, public vs. private, and how all of these inter-relate, so I’m sorry if this is a total noob question.

So, I have a main file, a Tile super class, and two sub-classes of the tile class- Bomb and Flat. Bomb is a tile with a bomb in it, and Flat is any tile that isn’t a bomb.

public class MineSweeperMain{
public static void main(String[] args)
{
    Scanner kybd = new Scanner(System.in);
    int dimension;
    Tile[][] gameBoard;

    System.out.print("Enter the dimension of the board you would like to play on:\t");
    dimension = kybd.nextInt();

    gameBoard = Tile.makeField(dimension);
    Tile.printField(gameBoard, dimension);
}

}

//

public class Tile {

static Random rand = new Random();

boolean isBomb;
boolean isRevealed;
int posX, posY;
int noOfAdjacentMines;

public Tile()
{
    isRevealed = false;
}

public static int detectMines(Tile[][] board, int dimensions)
{
    int detectedMines = 0;
    for(int i = 0; i < dimensions; i++)
    {
        for(int j = 0; j < dimensions; j++)
        {
            if(board[i][j].isBomb)
                detectedMines++;
        }
    }
    return detectedMines;
}

public static Tile[][] makeField(int dimensions)
{   
    int rowOfMines = dimensions / 3;
    int randomInRow;

    Tile[][] Board = new Tile[dimensions][dimensions];

    for(int i = 0; i < dimensions; i++)
        for(int j = 0; j <= rowOfMines; j++)
        {
        randomInRow = rand.nextInt(dimensions);
        Board[i][randomInRow] = new Bomb();
        }

    for(int i = 0; i < dimensions; i++)
        for(int j = 0; j < dimensions; j++)
        {
            if(!Board[i][j].isBomb)
                Board[i][j] = new Flat();
        }
    return Board;               
}

public static void printField(Tile[][] board, int dimensions)
{
    for(int i = 0; i <= dimensions; i++)
    {
        for (int j = 0; j <= dimensions; j++)
        {
            if(i ==0)
                System.out.print(i + " ");
            else if(j == 0)
                System.out.print(j + " ");
            else
            {
                if(board[i-1][j-1].isRevealed && !board[i-1][j-1].isBomb)
                    System.out.print(board[i-1][j-1].noOfAdjacentMines + " ");  
                else
                    System.out.print("# ");
            }
        }
    }
}

}

//

public class Flat extends Tile{

public Flat()
{
    noOfAdjacentMines = 0;
    isBomb = false;
    isRevealed = false;
}
}

//

public class Bomb extends Tile{
public Bomb()
{
    isBomb = true;
    isRevealed = false;
}

}

//

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:59:35+00:00Added an answer on May 29, 2026 at 8:59 am

    Your issue is in the second loop of the makeField method I think.

    When you check if(!Board[i][j].isBomb) That particular value is going to be null because you have not filled your array fully yet. There are a couple random bombs placed by the first loop, but the rest of the values are null.

    I would recommend reversing your loops. First loop through everything and make the whole board out of Flats without checking anything.

    Then in your second loop, you will just overwrite a couple Flats with Bombs

    The other solution is just to make this tiny modification and check for null:

    if(null == Board[i][j] || !Board[i][j].isBomb)

    Note that if you take this solution, the null check must be FIRST. This is because of something called short-circuting.

    The reason I recommend my first solution though of switching the loops is because it eliminates 1 extra comparison, not huge deal, but you never know…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.