Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Cna someone help me with this java program. I have 2 errors and lost on how to f

ID: 3721338 • Letter: C

Question

Cna someone help me with this java program. I have 2 errors and lost on how to fix it (I have been struggling awhile with this due to study for finals). The errors I am getting are :

Exception in thread "main" java.lang.NullPointerException

at edu.waketech.ccave.provided.Spelunker.getLongDescription(Spelunker.java:103)

at edu.waketech.ccave.provided.CaveTestMain.main(CaveTestMain.java:20)

Below I have the code.

***************Spelunker Class:***********************

import java.util.ArrayList;

import edu.waketech.ccave.common.CCaveElement;

import edu.waketech.ccave.common.Direction;

import edu.waketech.ccave.common.ItemDirectory;

import edu.waketech.ccave.common.RoomDirectory;

import edu.waketech.ccave.item.CCaveItem;

import edu.waketech.ccave.location.CCaveRoom;

/**

* Our fearless spelunker.

*

* We track the inventory of Items that we're carrying by assigning them the

* location SPELUNKER_ID. It's not really a "CCaveRoom," but it is a unique

* String and can let us identify the items we're carrying versus the items that

* we're not.

*

*

* If we were doing a Model-View-Controller design pattern for this game (and

* we're close, but not quite there), this would be our controller. The types

* associated with Rooms and Items would be our data model.

*

*

*/

public class Spelunker implements CCaveElement {

/**

*

*/

private static final long serialVersionUID = 1786845014557682582L;

public static final String SPELUNKER_ID = "spelunker";

public RoomDirectory roomDirec = RoomDirectory.getInstance();

private CCaveRoom currentLocation;

/**

* Create a spelunker with a given name and starting location.

*

* @param startRoom

* Were We Start

*/

public Spelunker(String startRoom) {

currentLocation = roomDirec.get(startRoom);

}

/**

* Assign the spelunker to the given location

*

* @param location will become the current location of the spelunker

*/

public void setLocation(CCaveRoom location) {

this.currentLocation = location;

}

/**

* Move the player from the current location in the given direction. Update our

* state as appropriate.

*

*

* @param direction

* Direction to move

*/

public void move(CCaveRoom direction) {

currentLocation = currentLocation.nextRoom(direction);

}

/**

* Take some action on an inventory item--pick it up, drop it, etc.

*

* Dropped items are assigned to be in our current location. Items that are

* picked up are assigned the location SPELUNKER_ID.

*

* @param whatToDo

* the command to perform, like GET or DROP

* @param objectOfAction

* the item to pick up or drop or do whatever with

*/

public void changeInventory(ItemCommand whatToDo, String objectOfAction) {

ItemDirectory itemDirec = ItemDirectory.getInstance();

if (whatToDo == ItemCommand.GET) {

CCaveItem itemObj = itemDirec.get(objectOfAction);

if (itemObj != null)

itemObj.setLocation(SPELUNKER_ID);

;

} else if (whatToDo == ItemCommand.DROP) {

CCaveItem itemObj = itemDirec.get(objectOfAction);

if (itemObj != null) {

itemObj.setLocation(currentLocation.getId());

}

}

}

// NOT I18N CAPABLE

@Override

public String getLongDescription() {

String cRoom = currentLocation.getLongDescription();

String roomInventory = currentLocation.getContentsLongDescription();

StringBuilder youHave = new StringBuilder();

youHave.append("you have ");

ItemDirectory direc = ItemDirectory.getInstance();

ArrayList<CCaveItem> contents = direc.getItemsInRoom(SPELUNKER_ID);

for (CCaveItem ele : contents) {

youHave.append(ele.getLongDescription());

youHave.append(" ");

}

return "You are in " + cRoom + " In here is" + roomInventory + " and you have " + youHave.toString();

}

// NOT I18N CAPABLE

@Override

public String getShortDescription() {

String cRoom = currentLocation.getShortDescription();

StringBuilder youHave = new StringBuilder();

youHave.append("you have ");

ItemDirectory direc = ItemDirectory.getInstance();

ArrayList<CCaveItem> contents = direc.getItemsInRoom(SPELUNKER_ID);

for (CCaveItem ele : contents) {

youHave.append(ele.getShortDescription());

}

return "You are in " + cRoom + " and has " + youHave;

}

@Override

public String getId() {

return SPELUNKER_ID;

}

@Override

public boolean isId(String identifier) {

return SPELUNKER_ID.equalsIgnoreCase(identifier);

}

/**

* If we get a command that's classified as "OTHER," we find the item being

* manipulated and rely on it to do whatever specific processing is necessary.

*

* @param cmd

* The command to be executed

*

* @param item

* the item being manipulated

*/

public void otherCommand(ItemCommand cmd, String item) {

// if we can identify the item, we'll operate on it and it alone

CCaveItem caveItem = ItemDirectory.getInstance().get(item);

if (caveItem != null) {

caveItem.executeCommand(cmd, item, currentLocation);

}

}

public Direction move(Direction dir) {

// TODO Auto-generated method stub

return dir;

}

}

********************CaveTestMain Class:*******************

import java.io.IOException;

import edu.waketech.ccave.common.Direction;

/**

* A place with a main method where we can code some tests of our cave code.

*

* @author parks

*

*/

public class CaveTestMain {

public static void main(String[] args) throws IOException {

// First some sanity checks.

// CCaveMap caveMap = CCaveMap.accessMap();

Spelunker me = new Spelunker("endofroad");

System.out.println(me.getLongDescription());

me.move(Direction.S);

System.out.println(me.getLongDescription());

me.move(Direction.N);

System.out.println(me.getLongDescription());

me.move(Direction.E);

System.out.println(me.getLongDescription());

}

}

************RoomDirectory Class:*********************

import java.util.ArrayList;

import edu.waketech.ccave.location.Bathroom;

import edu.waketech.ccave.location.Bedroom;

import edu.waketech.ccave.location.CCaveRoom;

import edu.waketech.ccave.location.Dining;

import edu.waketech.ccave.location.EndOfRoad;

import edu.waketech.ccave.location.Garage;

import edu.waketech.ccave.location.Garden;

import edu.waketech.ccave.location.Kitchen;

import edu.waketech.ccave.location.Lounge;

import edu.waketech.ccave.location.PoolRoom;

import edu.waketech.ccave.location.Tunnels;

import edu.waketech.ccave.location.Washroom;

import edu.waketech.ccave.location.Wellhouse;

public class RoomDirectory {

private ArrayList<CCaveRoom> caveRoom = new ArrayList<>();

private static RoomDirectory instance = new RoomDirectory();

private final CCaveRoom[] ALL_ITEMS = {

new Bathroom(),

new Bedroom(),

new CCaveRoom(),

new Dining(),

new EndOfRoad(),

new Garage(),

new Garden(),

new Kitchen(),

new Tunnels(),

new Washroom(),

new Wellhouse(),

new PoolRoom(),

new Lounge(),

};

private RoomDirectory() {

for (CCaveRoom cci: ALL_ITEMS) {

caveRoom.add(cci);

}

}

public static RoomDirectory getInstance() {

return instance;

}

public boolean contains(java.lang.String id) {

for (CCaveRoom ele : caveRoom) {

if ( ele.getId().equalsIgnoreCase(id))

return true;

}

return false;

}

public int indexOf(java.lang.String id) {

int i = 0;

for (i = 0; i < caveRoom.size(); i++) {

if (( caveRoom.get(i)).getId().equalsIgnoreCase(id))

return i;

}

return -1;

}

public boolean remove(java.lang.String id) {

int i = -1;

boolean found = false;

while (!found) {

i++;

if (( caveRoom.get(i)).getId().equalsIgnoreCase(id))

found = true;

}

if (i < caveRoom.size()) {

caveRoom.remove(i);

return true;

}

return false;

}

public CCaveRoom get(java.lang.String id) {

int i = indexOf(id);

if (i == -1) return null;

return caveRoom.get(i);

}

}

Explanation / Answer

Hi, look These Programs are having dependency of Various Classes and Jars That you Imported in Classes like interface CCaveElement and Other Imports. So i Can not Copy it my workspace until All the Dependencies are not resolved. But the main Problem you are facing Let's Tackle that . The Exception you are getting in Code is very Comman.It is run Time Exception There are reason for That. Any of the variable Should not be Null . So you Must intialize all the Variable with Some value Here also You need To do same . You Need to intialize all The Variable. Beacause few Variable default value is null. So do this fix. It will work definately.

As These Programs are having dependency on Jars and other classes and Interface that are not given so i can not take it in My workspace but The Solution whatever is Provided works well,

in case of any doubt and query Please let me know in comments i will be happy to help you thanks..

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote