2010-02-12 77 views
-4
class{ 
    public HashMap<String, String> eachperson; 

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

(請原諒在類和函數的前面的公共/私處我只是想知道如果我把正確的散列圖)這個Java代碼有什麼問題?爲什麼我不能放入HashMap?

對於真正的代碼,請參閱以下內容:

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

什麼是錯誤?不要只說「爲什麼我不能?」 – gubby 2010-02-12 12:07:46

+0

我不知道錯誤,因爲我在做Android,並且它彈出這個奇怪的Edit Source事物。 – TIMEX 2010-02-12 12:08:35

+0

在eclipse或其他方面創建一個新項目,看看編譯器怎麼說的,你可能會得到更多的錯誤,但是尋找描述hashmap的東西,也許它與運行時環境(很可能)有關。 – ant 2010-02-12 12:11:17

回答

5

您收到的具體錯誤將會有所幫助。但是,我沒有看到您初始化您的第一個添加的HashMap的位置。您將其聲明爲最高級別,然後嘗試將其用於if(this.in_first_name)未分配的情況。

2

您已經聲明瞭一個名爲'eachperson'的類型爲HashMap但您從未初始化的變量。此外,通常情況下,如果您需要更改地圖實施,則最好使用Map界面來「使用」地圖。

替換應該解決HashMap問題的每個人聲明。

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

你確實有一些代碼「重置」它得到了「人物」元素HashMap中之後。請注意,字符會在startElement之後發生,因此您的地圖在第一次使用之前從未初始化過。您可能還想使用「清除」而不是重新創建地圖。

dset.eachperson.clear(); 

這將清除您的地圖,但不需要創建新的實例。