这个Java代码有什么问题?为什么我不能放入HashMap?

时间:2010-02-12 12:06:36

标签: java hashmap

class{
    public HashMap<String, String> eachperson;  

    apple(){
        this.eachperson.put("thekey","thevalue");
    }
}

(请原谅班级和职能面前的公众/私人。我只是想知道我是否正确地使用哈希地图。)

有关真实代码,请参阅以下内容:

class ParsedDataSet{
    public HashMap<String, String> eachperson;      
    public List<HashMap<String,String>> peoplelist = new ArrayList<HashMap<String,String>>();
}

class ListprofileHandler extends DefaultHandler{
    private boolean in_results = true;
    private boolean in_item = false;
    private boolean in_first_name = false;
    private boolean in_last_name = false;
    private boolean in_picture_url = false;
    private boolean in_enditem_dummy = false;
    private ParsedDataSet dset = new ParsedDataSet();
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(localName.equals("results")){
            this.in_results = true;
        }else if(localName.equals("item")){
            this.in_item = true;
        }else if(localName.equals("first_name")){
            this.in_first_name = true;
        }else if(localName.equals("last_name")){
            this.in_last_name = true;
        }else if(localName.equals("picture_url")){
            this.in_picture_url = true;
        }else if(localName.equals("enditem_dummy")){
            this.in_enditem_dummy = true;
        }
    }
    public void endElement(String uri, String localName, String qName)throws SAXException {
        if(localName.equals("results")){
            this.in_results = false;
        }else if(localName.equals("item")){
            this.in_item = false;
        }else if(localName.equals("first_name")){
            this.in_first_name = false;
        }else if(localName.equals("last_name")){
            this.in_last_name = false;
        }else if(localName.equals("picture_url")){
            this.in_picture_url = false;
        }else if(localName.equals("enditem_dummy")){
            this.in_enditem_dummy = false;
        }
    }           
    public void characters(char ch[], int start, int length) throws SAXException {
        if(this.in_first_name){
            dset.eachperson.put("first_name", new String(ch, start, length));
        }
        if(this.in_enditem_dummy){
            dset.peoplelist.add(dset.eachperson);
            dset.eachperson = new HashMap<String,String>();  //Reached a new item. So reset the temporary hashmap.
        }

    }       

    public ParsedDataSet getParsedListData(){
        return this.dset;
    }   
}

2 个答案:

答案 0 :(得分:5)

您收到的具体错误会有所帮助。但是,我没有看到您为第一次添加初始化HashMap的位置。你声明它没有赋值,然后你试图在if(this.in_first_name)的情况下使用它而不分配它。

答案 1 :(得分:2)

您已声明一个名为'eachperson'的变量,其类型为HashMap,但您从未初始化。此外,通常最佳做法是使用Map界面“使用”地图,以防您需要更改地图实现。

替换应解决HashMap问题的每个人声明。

public HashMap<String,String> eachperson = new HashMap<String,String>(); 

在获取'characters'元素后,你确实有一些代码可以'重置'HashMap。请注意,字符将在startElement之后出现,因此您的地图在第一次使用之前从未初始化。您可能还想使用“清除”而不是重新创建地图。

dset.eachperson.clear();

这将清除您的地图,但不需要创建新实例。