开发者

What is wrong with this Java code? Why can't I put in HashMap? [closed]

开发者 https://www.devze.com 2022-12-20 03:47 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 8 years ago.

Improve this question
class{
    public HashMap<String, String> eachperson;  

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

(Please excuse the public/privates in front of the class and function. I just want to know if I'm putting the hash map correctly. )

For the true code, please see below:

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;
    }   
}


The specific error that you are receiving would be helpful. However, I don't see where you are initializing your HashMap for the first add. You declare it up top with no assignment, and then you attempt to use it in the if(this.in_first_name) case without assigning it.


You have declared a variable called 'eachperson' that is of type HashMap but you never initialized. Also, it is usually best practice to use the Map interface to 'use' the map in case you need to change the map implementation.

Replace the eachperson declaration that the HashMap issue should be solved.

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

You did have some code to 'reset' the HashMap after it gets the 'characters' element. Note that characters will occur after startElement so your map was never initialized before it was used the first time. You might also want to use 'clear' instead of recreating the map.

dset.eachperson.clear();

That will clear your map but does not require creating a new instance.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号