がData.ListGroupedProducts
forループを使用してHashMapを反復しようとするとnullが返されますか?ここで
public static HashMap<String, HashMap<String, WrapperProduct>> ListGroupedProducts = new HashMap<>();
の宣言でこれを行うための私の理由は、新規/更新されたオブジェクトとの深い、コレクション内のオブジェクトを交換することです。私はnull
を取得して、get
にHashMap内で必要なコレクションを作成しようとしています。ここで
私のデバッグのスクリーンショット、Data.ListGroupedProducts
の上にマウスです。
赤い矢印は、私が到達しようとしていることです... forループでこれを繰り返すことはできませんか? Boannの答えに
UPDATE
おかげで、私は問題を解決することができました。 KeySetを反復するためにfor-loopを使用しようと試みたのと同じ間違いで誰かが落ちた場合の完全なメソッドです。あなたはHashMap
を反復するループのfor (int x = 0; x < size; x++)
スタイルを使用することはできません
//Product coming in, has the latest updates and this will be used
//to update the product deep in Data.ListGroupedProducts.
public boolean UpdateListGroupProducts(ManifestProduct product) {
for(int a=0; a < Data.SelectedInvoice.size(); a++){
String invoiceNumber = Data.SelectedInvoice.get(a).ID;
if(Data.ListGroupedProducts.containsKey(invoiceNumber)){
//Iterate over each key using a keyset, we will then use the key on the for-loop
//to retrieve the inner object and manipulate it as needed.
for(String key: Data.ListGroupedProducts.get(invoiceNumber).keySet()){
for(int x=0; x < Data.ListGroupedProducts.get(invoiceNumber).get(key).ProductList.size(); x++){
String productID = Data.ListGroupedProducts.get(invoiceNumber).get(key).ProductList.get(x).ID;
String productItemID = Data.ListGroupedProducts.get(invoiceNumber).get(key).ProductList.get(x).ITEMID;
if(product.ID.equals(productID) && product.ITEMID.equals(productItemID)){
//Use ArrayList's set method to replace a position in the array with a new instance.
Data.ListGroupedProducts.get(invoiceNumber).get(key).ProductList.set(x, product);
return true;
}
}//Inner For
}//Outer For
}
}
return false;
}
これを[MCVE]に減らすことはできますか? Javaの命名規則に従ってください。 'Data.ListGroupedProducts.get(invoiceNumber)'の値を再利用しようとするならば、それをショートネーム変数に抽出し、それを再利用してください。はるかに読みやすいコードを作成します。 – Savior
'a <= Data.SelectedInvoice.size() - 1'は、通常、' a
これはあまり意味がありません。 'Data.SelectedInvoice.get(a).ID'で' containsKey'を呼び出し、 'Data.SelectedInvoice.get(a).ID.toString()'で 'get'を呼び出します。彼らは同じではありませんか? –