gpt4 book ai didi

java - 检查 boolean 值时,我不断收到空指针异常错误

转载 作者:行者123 更新时间:2023-12-02 11:28:35 25 4
gpt4 key购买 nike

在 BlueJ 和 Eclipse 中,检查字符串相等性时出现空指针异常。

这是我的代码:

import java.util.Set;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;

/**
* Class Room - a room in an adventure game.
*
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* A "Room" represents one location in the scenery of the game. It is
* connected to other rooms via exits. For each existing exit, the room
* stores a reference to the neighboring room.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/

public class Room
{
private String description;
private String itemDescription;
private HashMap<String, Room> exits; // stores exits of this room.
private ArrayList<Item> items;

/**
* Create a room described "description". Initially, it has
* no exits. "description" is something like "a kitchen" or
* "an open court yard".
* @param description The room's description.
*/
public Room(String description)
{
this.description = description;
this.itemDescription = "This room contains: ";
exits = new HashMap<>();
items = new ArrayList<Item>();
}

public Room(String description, Item item)
{
items = new ArrayList<Item>();
this.description = description;
addItem(item);
this.itemDescription = "This room contains: " + item.getLongDescription() ;
exits = new HashMap<>();
}
/**
* Define an exit from this room.
* @param direction The direction of the exit.
* @param neighbor The room to which the exit leads.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}

public void addItem(Item item)
{
items.add(item);
System.out.println(itemDescription.equals("This room contains: "));
this.itemDescription = this.itemDescription + /*(this.itemDescription.equals("This room contains: ") ?
"" : ", ")*/ ", " + item.getLongDescription();
}

public void removeItem(String shortItemDescription){
int i= 0;
Iterator<Item> it = items.iterator();
while(it.hasNext()){
if (it.next().getShortDescription().equals(shortItemDescription)){
it.remove();
i++;
}
else
i++;
}
}

public ArrayList<Item> getItems(){
return items;
}

/**
* @return The short description of the room
* (the one that was defined in the constructor).
*/
public String getShortDescription()
{
return description;
}

/**
* Return a description of the room in the form:
* You are in the kitchen.
* Exits: north west
* @return A long description of this room
*/
public String getLongDescription()
{
return "You are " + description + ".\n" + (itemDescription.equals("This room contains: ") ?
"This room is empty" : itemDescription) + ".\n" + getExitString();
}

/**
* Return a string describing the room's exits, for example
* "Exits: north west".
* @return Details of the room's exits.
*/
private String getExitString()
{
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit : keys) {
returnString += " " + exit;
}
return returnString;
}

/**
* Return the room that is reached if we go from this room in direction
* "direction". If there is no room in that direction, return null.
* @param direction The exit's direction.
* @return The room in the given direction.
*/
public Room getExit(String direction)
{
return exits.get(direction);
}
}

在方法addItem(Item item)中检查字符串比较方法 itemDescription.equals("This room contains: ") 时出现空指针异常错误,以及 System.out.println() - 语句如 itemDescription.equals("This room contains: ") 的注释三元运算符测试中所示.

奇怪的是getLongDescription()中的三元运算符功能正常。

最佳答案

如果您使用 Item 构建一个 Room 实例。您可以在实例化 itemDescription 变量之前调用 addItem

public Room(String description, Item item) 
{
// ...
addItem(item);
this.itemDescription = "This room contains: " + item.getLongDescription() ;
// ...
}

因此在方法 addItem 中,this.itemDescription 仍然为 null。

public void addItem(Item item)
{
// ...
System.out.println(itemDescription.equals("This room contains: "));
// ...
}

这就打破了。

要解决此问题,请在实例化 itemDescription 后调用 addItem。请注意,您不需要添加 item 描述,因为这已在 addItem 方法中完成。

public Room(String description, Item item) 
{
// ...
this.itemDescription = "This room contains: ";
addItem(item);
// ...
}
<小时/>

您会后悔 String itemDescription 速度太快了!

相反,创建方法getRoomDescription,它将迭代items列表并生成String itemDescription(使用StringBuilder >) !

类似于

public String getRoomDescription(){
StringBuilder sb = new StringBuilder("Rooms contains :");
for(Item i : items){
sb.append("\n".append(i.getLongDescription());
}
return sb.toString();
}

编辑 itemDescription 变量很困惑并且容易出现问题(就像您遇到的那样)。

<小时/>

最后一件事,您所做的测试

itemDescription.equals("This room contains: ")

应该给你相同的结果

items.size() == 0

但是更容易理解(items 列表中没有值),并且如果您编辑要打印描述的方式,则不太容易出错。

关于java - 检查 boolean 值时,我不断收到空指针异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49447170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com