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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:49:50+00:00 2026-05-31T23:49:50+00:00

I am doing a piece of coursework in Java using BlueJ. We need to

  • 0

I am doing a piece of coursework in Java using BlueJ. We need to add new features to the text based game, Zuul. I have decided to start working on an inventory and item system. I am having trouble working out the best way to do this so I just winged it. Here is my code. Sorry I haven’t gotten round to commenting everything yet. The game compiles but I get an exception in the console when I run the game.

Error:

java.lang.NullPointerException
    at Game.createPlayer(Game.java:15)
    at Game.<init>(Game.java:7)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:740)

Game Class (This is equivalent of the Main class in Java, this is where the game is run from):

import java.util.*;

public class Game
{
    public Game()
    {
        createPlayer();
        createItems();
    }

    private Entity localPlayer;

    public void createPlayer(){
        Player localPlayer = new Player("Player Name", 0, 0, 0, 0, 0);
        localPlayer.equipArmour("Helm", armourDB.get("Helm")); // This is where I think I have gone wrong
    }

    // Create global hashmap variables
    private HashMap<String, Weapon> weaponsDB;
    private HashMap<String, Armour> armourDB;
    private HashMap<String, Supplement> supplementDB;

    public void createItems(){
        // Create weapons
        weaponsDB = new HashMap<String, Weapon>();

        Weapon weaponFists = new Weapon("Fists", "Weapon", 0, 0, 0, 0, 0, "Melee");
        Weapon weaponSword = new Weapon("Sword", "Weapon", 0, 0, 0, 0, 0, "Melee");
        Weapon weaponBow = new Weapon("Bow", "Weapon", 0, 0, 0, 0, 0, "Ranged");
        Weapon weaponDagger = new Weapon("Dagger", "Weapon", 0, 0, 0, 0, 0, "Melee");

        weaponsDB.put("Fists", weaponFists);
        weaponsDB.put("Sword", weaponSword);
        weaponsDB.put("Bow", weaponBow);
        weaponsDB.put("Dagger", weaponDagger);

        // Create armour
        armourDB = new HashMap<String, Armour>();

        Armour armourBreastplate = new Armour("Breatplate", "Chest", 0, 0, 0, 0, 0);
        Armour armourHelm = new Armour("Helm", "Head", 0, 0, 0, 0, 0);

        armourDB.put("Breastplate", armourBreastplate);
        armourDB.put("Helm", armourHelm);

        // Create supplements
        supplementDB = new HashMap<String, Supplement>();

        Supplement supplementHealthPotion = new Supplement("Health Potion", 0, 0);
        Supplement supplementPowerPotion = new Supplement("Power Potion", 0, 0);

        supplementDB.put("Health Potion", supplementHealthPotion);
        supplementDB.put("Power Potion", supplementPowerPotion);
    }
}

Entity Class (Construction for the player class and enemy class):

import java.util.*;

public class Entity
{
    private boolean entityStatus;

    private String entityName;
    private int entityHealth;
    private int entityPower;
    private int entityHealthRegen;
    private int entityPowerRegen;
    private int entityAttackPower;

    private HashMap<String, Armour> entityEquipment;
    private ArrayList<Item> entityInventory;    

    public Entity(
        String paramEntityName,
        int paramEntityHealth,
        int paramEntityPower,
        int paramEntityHealthRegen,
        int paramEntityPowerRegen,
        int paramEntityAttackPower)
    {
        entityStatus = true;

        entityName = paramEntityName;
        entityHealth = paramEntityHealth;
        entityPower = paramEntityPower;
        entityHealthRegen = paramEntityHealthRegen;
        entityPowerRegen = paramEntityPowerRegen;
        entityAttackPower = paramEntityAttackPower;

        entityEquipment = new HashMap<String, Armour>(); // Set all possible equipment slots to null on initial run
        entityEquipment.put("Head", null);
        entityEquipment.put("Shoulders", null);
        entityEquipment.put("Chest", null);
        entityEquipment.put("Hands", null);
        entityEquipment.put("Legs", null);
        entityEquipment.put("Feet", null);
        entityEquipment.put("Weapon", null);

        entityInventory = new ArrayList<Item>();
    }

    public boolean getEntityStatus(){
        return entityStatus;
    }

    public String getEntityName(){
        return entityName;
    }

    public int getEntityHealth(){
        return entityHealth;
    }

    public int getEntityPower(){
        return entityPower;
    }

    public int getEntityHealthRegen(){
        return entityHealthRegen;
    }

    public int getEntityPowerRegen(){
        return entityPowerRegen;
    }

    public int getEntityAttackPower(){
        return entityAttackPower;
    }

    // Equips the player with an item into the equipment slot
    public void equipArmour(String paramEquipmentSlot, Armour paramArmourName){
        entityEquipment.put(paramEquipmentSlot, paramArmourName);
    }

    public void printInventory(){
        System.out.println("Something");
    }
}

I think the main problem is that I cannot wrap my head around the use of hashtags, I need to see a live example to see how it works. Can anyone help? If you need anything else from me, let me know.

  • 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-31T23:49:51+00:00Added an answer on May 31, 2026 at 11:49 pm

    Well this is the problem:

    armourDB.get("Helm")
    

    You haven’t initialized armourDB at that point. If you call createItems() before createPlayer() it should be okay for that particular line. But you still won’t be initializing the isntance variable called localPlayer. You’ll only be assigning a value to the local variable declared in createPlayer.

    It’s not really clear what you’re trying to achieve to be honest, but those are the first two problems…

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

Sidebar

Related Questions

I'm doing an 8 piece puzzle game in java, and the assignment commands that
I have a piece of code like this: HashSet<Object> Set = new HashSet<Object>(); //add
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
I have a piece of text and I've got to parse usernames and hashes
Doing a small project I have received some Java code that I should rewrite
I'm using this piece of code I found ( http://impnerd.com/wordpress-hack-add-post-images-to-your-homepage ) to display the
I have multiple C programs each doing the same piece of functionality. I want
I have a pretty simplistic piece of software I'm doing. I'm wanting to make
Essentially what i am doing is finding a piece of text in a textfile.
I have a piece of text that resembled the following: ==EXCLUDE #lots of lines

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.