I am creating a game project in BlueJ but the "drop" and "items" commands do not work. When I call either one of them in the game I get a "nullpointerexception" error message.
This is the drop method in the game class, which compiles but does not work:

/**
     * Drops an item
     */
    private void drop(Command command)
    {
        String theItem = command.getSecondWord();
        Item theItems = currentPlayer.getItem(theItem);
        if (!command.hasSecondWord()) {
             System.out.println("Drop what?");
             return;
        }
        currentPlayer.removeItem(theItems);
        System.out.println("The " + theItem + " has been removed from your inventory");
        currentRoom.addItem(theItems);
    }

The "nullpointerexception" error highlights the "if(anItem.getName().equals(theItem))" line of the following method in the Player class.

  /**
     * 
     */
    public Item getItem(String theItem)
    {
        Iterator iter = myItems.iterator();
        while(iter.hasNext()) {
            Item anItem = (Item) iter.next();
                if(anItem.getName().equals(theItem)) {
                    return anItem;
                }
        }
        return null;
    }

This method for searching the inventory is in the game class:

   /**
     * 
     */
    private void inventory(Command command)
    {
        System.out.println(currentPlayer.getMyItemsDescription());
    }

When the command is used in the game to get the inventory, a "nullpointerexception" error occurs and the following method in the Player class is displayed:

    /**
     * 
     */
    public String getMyItemsDescription()
    {
        String returnString = "The items in your inventory are:";
        for(Iterator iter = myItems.iterator(); iter.hasNext();){
            returnString += " " + ((Item)iter.next()).getDescription();
        }
            return returnString;
    }

Any help is greatly appreciated as I am new to this and have spent hours trying to get this to work.

Step 1:
Find out which variable or method return value is null on that line by printing each of them out. In particlual check whether anItem.getName() is returning null.

I have another method in the Game class which takes an item. The method calls getName() from the Player class without an problems. I hope I do not sound too inexperienced, but how I go about printing out the vairables or return values?

Not brain surgery, just like any other print. eg

System.out.println("anItem = " + anItem + " getName returned + "anItem.getName());

Thank you for the help but for some reason that does not seem to work. It is unable to find the variables when I try to compile.

That's just an example of the kind of code you should use to print the variables at the point where you get the NPE. You were supposed to understand it, then apply what you have learned. It sounds like you copy/pasted it to some random part of your code.
That's it from me tonight.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.