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

  • SEARCH
  • Home
  • 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 7658099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:09:26+00:00 2026-05-31T13:09:26+00:00

I am getting a NullPointerException when the getCave method is called when it tries

  • 0

I am getting a NullPointerException when the getCave method is called when it tries to return the 2D array. I have not been able to find a solution online. I can get the program to run without the exception by replacing the return with a new Cave that is not an array but that would not fit my needs. Here is a simplified version of my code:

import java.util.Random;


public class Board {

    public static final int DEFAULT_ROWS = 10;
    public static final int DEFAULT_COLS = 10;


    Cave[][] caveArray = new Cave[DEFAULT_ROWS+2][DEFAULT_COLS+2];


    public Board(int rows, int cols){

        Random rand = new Random();


                for (int j = 1; j < (cols+1); j++) {

                    for (int i = 1; i < (rows+1); i++) {

                        Cave temp;
                        temp = new Cave(i, j);

                        int rnum = rand.nextInt(100)+1;


                        if (rnum > 50) {
                                caveArray[i][j]=temp;
                                caveArray[i][j].makeBlocked(); 
                        }


                        else if(rnum <=50) {
                                caveArray[i][j]=temp;
                                caveArray[i][j].makeOpen();
                        }
                    }
                }           
    }

    public Cave getCave(int r, int c){

        return caveArray[r][c];
    }

}

here is the caller:

private void newGame() {
    // Set up the game board.
    gameBoard = new Board(DEFAULT_ROWS, DEFAULT_COLS);

    // Set up the 3 characters.
    characters = new ArrayList<Character>();

    // Add the adventurer (always in the top left).
    characters.add(new Adventurer(gameBoard.getCave(0, 0)));
    selected = 0; // Initially select the adventurer.
}

which calls:

public class Adventurer extends Character{

Adventurer(Cave initLoc) {
    super(initLoc);


}

which calls:

public abstract class Character implements CaveWorker{

protected Cave location;

public Character(Cave initLoc){
    location = initLoc;

    location.setOccupied(true);
}
  • 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-31T13:09:26+00:00Added an answer on May 31, 2026 at 1:09 pm

    The only explanation that I can offer without observing a stack trace (those are really helpful, more times than not) is that if you attempt to index into caveArray[0][c], or even caveArray[r][0] there’s not going to be anything there.

    You have two options – either use the fact that arrays will start at index 0 (it’s not so bad), or preemptively place a Cave object in row 0 and column 0 that serves no real purpose. Aligning with (0,0) would be the easier choice, though.

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

Sidebar

Related Questions

Im not sure why im getting NullPointerException. The method getQuestion() currently points to a
I recently posted a question about getting a NullPointerException whenever I called an array
Im getting nullpointerexception for the following code: I have SMS of more than 160
I am trying to Set Location but getting NullPointerException , i have Provided android.permission.ACCESS_COARSE_LOCATION
I am getting a nullpointerexception when I create a large array from a file.
I have and image (img1.png) in my drawable-hdpi folder, but im getting NullPointerException(?) when
I have a about screen that has some buttons, but I keep getting NullPointerException
I'm getting NullPointerException :s when using sstable2json in Cassandra 0.6.0-beta3: $ bin/sstable2json .../cassandra/data/system/LocationInfo-1-Data.db Exception
Hi I'm getting nullpointerexception at rs.next() or rs.getString(1) it is really weird that sometimes
i am writing code for btree algorithms. i am getting NullPointerException . why???? please

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.