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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:31:49+00:00 2026-06-13T02:31:49+00:00

I am creating an object orientated mastermind game. I have all the classes and

  • 0

I am creating an object orientated mastermind game. I have all the classes and methods set-up and have tried them in a non object orientated programming style and they all work but now since I have them in object orientated style I get the null pointer error. It tells me where the error is occurring and I have tried to figure out what is the null value or what is wrong but I cannot figure it out. I have also tried taking out the point where the null happens just to get another null exception at a similar type expression. So I believe that I have the syntax wrong for invoking methods or such but do not know how to fix it or if it’s the cause of the real error.

The second code block is where the error occurs if you want to jump straight to it.

I know I’ve posted a lot of stuff here so if you need any clarification I’ll gladly help. The main focus is just the null error so if there is some other error that you see just ignore it unless it impedes on solving the null error.

I separated each class for easier reading.


public class GameTester {

public static void main(String[] args) {

    MasterMind m = new MasterMind();
    m.playGame();
    }
}

public class MasterMind 
{
private Master theMaster;
private Player thePlayer;

public void mastermind() {
    theMaster = new Master();
    thePlayer = new Player();
}

public void playGame() {
    System.out.println("WELCOME TO CODEBREAKER... Let's Play!\n");
    System.out.println("Guess a 4-letter code with letters A, B, C, and D\n");

    theMaster.createCode(); //heres where the null exception is said to occur

    while(true) {
        thePlayer.makeGuess(); //if i remove the call above this becomes null error
        int x = theMaster.totalCorrect(thePlayer.getGuess());
        if( x == 4) {
            System.out.println("\nGOT IT!!!\n");
        }
        else {
            System.out.printf("MISSED! %d out of 4. TRY AGAIN... \n", x);
        }       
    }
 }

  import java.util.Random;
  public class Master 
  {
  private char[] Code = new char[4];

public Master()
{

}

public void createCode()
{
     Random R = new Random();
        char[] setting ={'A', 'B', 'C', 'D'}; 
        int rx;

        for(int i=0; i<=3; i++)
        {
            rx = R.nextInt(4);
            Code[i] = setting[rx];
        }           
}

public int totalCorrect(char[] theGuess)
{
    int x =0;

    if(Code[0] == theGuess[0]) {
        x++;
    }   
    if(Code[1] == theGuess[1]) {
        x++;
    }
    if(Code[2] == theGuess[2]) {
        x++;
    }
    if(Code[3] == theGuess[3]) {
        x++;
    }

    return x;
}
}

 import java.util.Scanner;
 public class Player { 

 private char[] Guess = new char[4];

public Player() {

}

public void makeGuess() {

    System.out.println("YOUR GUESS => ");
    Scanner input =new Scanner(System.in);
    String guess = input.next(); 
    char[] D = guess.toCharArray(); 

    for(int i=0; i<4; i++) {
        Guess[i] = D[i];
    }   
}

public char[] getGuess() {
        return Guess;
}   
}
  • 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-06-13T02:31:50+00:00Added an answer on June 13, 2026 at 2:31 am

    Using final on attribute declaration you enforce the life-cycle of the instances, and you don’t need a constructor:

    public class MasterMind 
    {
       private final Master theMaster = new Master();
       private final Player thePlayer = new Player();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In object oriented programming inheritance and virtual methods are the common scenario for creating
I recently found that dynamically creating object and methods in Ruby is quite a
I have astring directly coming form the database and I am creating object of
I am creating an object in python. I have a numpy array from an
I'm creating a object with JDO and let the GAE set the Key for
I have a couple questions about creating a object (2 values) and how to
I'm trying to learn object oriented programming more clearer by creating a database class
Programming languages like C# or Java feature static methods, despite being heavily object oriented.
Taken from wikipedia: In object-oriented computer programming, a factory is an object for creating
A friend and myself are creating a Goosebumps-style adventure game, where at each stage

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.