为什么我将null作为目的地?

时间:2015-03-14 22:35:57

标签: java

当程序打印出System.out.println(_spaces.get(“classroom”)。toStringLong());它吐了回来“教室:一个大门的演讲厅,人行道上有一扇门。”为什么它会说一个空门?我想我必须修复我的_buildPortals方法,但我不确定如何。

public class ConfigLoader 
{
    private Ini _ini;
    private HashMap<String, Space> _spaces = new HashMap<String, Space>();
    private HashMap<String, Portal> _portals = new HashMap<String, Portal>();
    private HashMap<String, Agent> _agents = new HashMap<String, Agent>();

public ConfigLoader(File iniFile)
{
    _ini = new Ini(iniFile);
}

public Agent buildAll()
{
    _buildSpaces();
    _buildPortals();
    _buildExits();
    _buildDestinations();
    System.out.println(_spaces.get("classroom").toStringLong());
    _buildAgents();
    //return _selectStartAgent();
    return null;
}

private void _buildSpaces()
{
    for(String spaceName : _ini.keys("spaces"))
    {
        String description = _ini.get("spaces", spaceName);
        String image = _ini.get("images", "images");
        Space spaceInstance = new Space(spaceName, description, null, image);
        _spaces.put(spaceName, spaceInstance);
    }
}

private void _buildPortals()
{
    for(String portalName : _ini.keys("portals"))
    {
        String description = _ini.get("portal", portalName);
        Portal portalInstance = new Portal(portalName, description, null);
        _portals.put(portalName,  portalInstance);
    }
}

private void _buildExits()
{
    for(String spaceName : _ini.keys("exits"))
    {
        String spaceExit = _ini.get("exits", spaceName);
        Space space = _spaces.get(spaceName);
        Portal exit = _portals.get(spaceExit);
        space.setPortal(exit);
    }
}

private void _buildDestinations()
{
    for(String portalName : _ini.keys("destinations"))
    {
        String destination = _ini.get("destinations", portalName);
        Space dest = _spaces.get(destination);
        Portal portal = _portals.get(portalName);

        if(dest == null)
        {
            System.out.println("Error");
            System.exit(1);
        }
        else
        {
            portal.setDestination(dest);
        }
    }
}

private void _buildAgents()
{
    for(String agentName : _ini.keys("agents"))
    {
        String agent = _ini.get("agents", agentName);
        Space space = _spaces.get(agent);

        if(space == null)
        {
            System.out.println("Error");
            System.exit(1);
        }
        else
        {
            Agent a = new Agent(space, agentName);
            _agents.put(agentName, a);
        }
    }
}

private Agent _selectStartAgent()
{
    for(String agentName : _ini.keys("start"))
    {
        String agent = _ini.get("start", agentName);
        Agent agentInstance = _agents.get(agent);

        if(agent == null)
        {
            System.out.println("Error");
            System.exit(1);
        }
        else
        {
            return agentInstance;
        }
    }
    return null;
}

}

2 个答案:

答案 0 :(得分:0)

遵循代码中的其他模式,也许..

    String description = _ini.get("portal", portalName);

需要

    String description = _ini.get("portals", portalName);

如果是这样,通常最好将这样的东西提取为字符串常量。

private static final String PORTALS = "portals";

并在多个地方使用它。

答案 1 :(得分:0)

你的buildSpaces方法的第二行是错误的。你得到的图像与某个空间有关,但你的来电中有两个字符串,这是不对的。