0
我想加載數據哈希映射的哈希映射,我不斷收到空指針,不知道如果我做的權利事情。加載數據到HashMap <String,HashMap <String,HashMap <String,ArrayList <ClassOb> >>>
我希望我的哈希映射基於三個關鍵SalesAreaCode,year和product。我想將其餘部分存儲在arraylist中。我想通過銷售區域代碼,年份和產品,並列出與這三個關鍵字相關的所有產品,如客戶,數量,價格: 我真的需要幫助或有人指向我正確的方向。任何幫助將不勝感激!!艾倫
代碼:
public HashMap<String, HashMap<String, HashMap<String, ArrayList<Sales>>>> getSalesDetails(Connection con)
{
HashMap<String, HashMap<String, HashMap<String, ArrayList<Sales>>>>> hmpSales =
new HashMap<String, HashMap<String, HashMap<String, ArrayList<Sales>>>>>();
Sales s;
HashMap<String,ArrayList<Sales>> hmpSalProd = new HashMap<String, ArrayList<Sales>>();
HashMap<String,HashMap<String,ArrayList<Sales>>> hmpSalYear =
new HashMap<String, ArrayList<Sales>>>();
try
{
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next())
{
s=new Sales();
if(!(hmpSalProd.containskey(rs.getString("product"))))
{
s.setSalesCode(rs.getString("sacd"));
s.setCustomerCode(rs.getString("custcd"));
s.setYear(rs.getInt("year"));
s.setProduct(rs.getString("product"));
s.setPrice(rs.getDouble("price"));
s.setQty(rs.getInt("qty"));
hmpSalProd.put(rs.getString("product"),
new ArrayList<Sales>(Arrays.asList(s)));
}
else
{
s.setSalesCode(rs.getString("sacd"));
s.setCustomerCode(rs.getString("custcd"));
s.setYear(rs.getInt("year"));
s.setProduct(rs.getString("product"));
s.setPrice(rs.getDouble("price"));
s.setQty(rs.getInt("qty"));
hmpSalProd.get(rs.getString("product")).add(s);
}
if(!(hmpSalYear.containsKey(String.valueOf(rs.getInt("year")))))
{
hmpSalYear.put(String.valueOf(rs.getInt("year")),hmpSalProd);
}
else
{
hmpSalYear.get(String.valueOf(rs.getInt("year"))).
get(rs.getString("product")).add(s);
}
if(!(hmpSalArea.containsKey(rs.getString("sacd"))))
{
hmpSales.put(rs.getString("sacd"),hmpSalYear);
}
else
{
hmpSales.get(rs.getString("sacd")).
get(String.valueOf(rs.getInt("year"))).
get(rs.getString("product")).add(s);
}
}
pst.close();
rs.close();
}
catch(SQLException se)
{
System.out.println("error loading: " + se.getMessage());
}
return hmpSales;
}
寫一些代表你的數據的POJO而不是使用'HashMap >>>' –
jlordo
你可以發佈異常堆棧跟蹤嗎? –
你從哪裏得到NPE? – Archer