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 6868009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:22:52+00:00 2026-05-27T03:22:52+00:00

I am creating a reversi game for my intro CS class. I found an

  • 0

I am creating a reversi game for my intro CS class.

I found an error in SearchN() that would cause it to give false playableN flags and created isSame() as a work around.

Now the game is crashing when I attempt a move.

I have the bug isolated to isPlayable() which causes the program to stop running without error message.

I thought it was because the program was searching out of bounds; however, when i run .isPlayable(0,0) it returns a NullPointerException (also something I don’t quite know how to get rid of).

So it must be some error with handling un-played spaces.

Anyone have any thoughts?

/**
 * a method to return the color of a tile
 *@params    x    y    tile coordinates 
 */
public Color getColor(int x, int y){
  try{
    return buttons[x][y].getBackground();
  }
  catch(ArrayIndexOutOfBoundsException e){
    return null;
  }
}

/**
 * a method to determine whether a tile has been played
 *@params    x    y    tile coordinates 
 */
public boolean isPlayed(int x, int y){
  if(this.isBlack(x,y) || this.isWhite(x,y)){
    return true;
  }else{ 
    return false;
  }
}


/**
 * a method to determine whether a tile has a color opposite to a given color
 *@params    x    y    c    tile coordinates and color to compare
 */
  public boolean isOpposite(int x, int y, Color c){
    if(this.isPlayed(x,y)){
      return(!(this.getColor(x,y).equals(c)));
    } else
      return false;                                    // this was giving the false playableN flag
  }

/**
 * a method to determine weather a tile has the same color as the one given
 *@params    x    y    c    tile coordinates and color to compare
 */
  public boolean isSame(int x, int y, Color c){
    if(this.isPlayed(x,y)){
      return(this.getColor(x,y).equals(c));
    }else{
      return false;
    }
  }

/**
 * a method used to check tiles north of an attempted play to verify legal moves
 *@params    x    y    c    tile coordinates and comparing color
 */
  public void searchN(int x, int y, int increment, Color c){
    if(increment>1 && (this.isSame(x-increment,y, c))){
      playableN = true;
      leadN = false;
    } else {
      if(this.isOpposite(x-increment,y,c)){
        leadN=true;
      }
    }
  }

/**
 * a method used to determine if a tile is playable
 *@params    x    y    tile coordinates
 */
  public boolean isPlayable(int x, int y){
    this.searchN(x,y,1,turnColor);
    // search 7 other directions
    while(leadN||leadNE||leadE||leadSE||leadS||leadSW||leadW||leadNW){ 
      int i = 2;
      if(leadN)
        this.searchN(x,y,i,turnColor);
        // search 7 other directions
      i++;
    }
    if(playableN||playableNE||playableE||playableSE||playableS||playableSW||playableW||playableNW)
      return true;
    else
      return false;
  }

** all tiles are black, white or the default tile color (green) and in a 2D array of JButtons displayed in gridLayout().

  • 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-27T03:22:53+00:00Added an answer on May 27, 2026 at 3:22 am

    I see two ways that a NullPointerException could be happening:

    You are getting an ArrayIndexOutOfBoundsException in getColor. In that case you are catching the exception and returning null.

    or

    getBackground is returning null.

    In either case getColor will return null, which causes a NullPointerException to be thrown when you call .equals in isOpposite or isSame.

    You should check the result of getColor before attempting to calls .equals on it. Then you should figure out why getBackground is returning null or ArrayIndexOutOfBoundsException is being thrown.

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

Sidebar

Related Questions

) I'm in a process of creating a memory game. My problem is that
I am creating a Word class and I am getting an error: TypeError: superclass
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I created a custom LocationGenerator class that uses CoreLocation and Reverse Geocoding, and generates
I'm creating a form in a Rails 3 app that has a dropdown field.
I need to set the user that is creating the post in the add
I am creating a reverse proxy and I am trying to find the optimal
Creating a patch is very easy in SubVersion, With Tortoise, you right-click and select
Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups.
Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=

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.