-1

Ok so I am reading what has already been asking about NPE on here but I can't seem to find anything that will help with my code. It says that It is being thrown at .searchCollection(COLLECTION_NAME).

public class PantryDataStorage {

private static final String COLLECTION_NAME = "pantry";
private static HashMap<String, PantryItem> pantry = getDatabaseItems();
private static String API_KEY = "c3672b0c-b96c-4145-8b75-bd6895b5458e";
private static OrchestrateClient client = new OrchestrateClient(API_KEY);
ShoppingItem shoppingItem = new ShoppingItem();



public static void savePantryItem(String itemName, PantryItem updatedPantryItem){

    pantry.put(itemName, updatedPantryItem);
    client.kv(COLLECTION_NAME, itemName).put(updatedPantryItem).get().getKey();

}

public static void deletePantryItem(String itemName){

    pantry.remove(itemName);
    client.kv(COLLECTION_NAME, itemName)
    .delete()
    .get();
}

public static HashMap<String, PantryItem>getDatabaseItems(){

    SearchResults<PantryItem> result = client
            .searchCollection(COLLECTION_NAME)
            .limit(100)
            .get(PantryItem.class, "*")
            .get();

    Iterator<Result<PantryItem>> iterator = result.getResults().iterator();
    HashMap<String, PantryItem> listHash = new HashMap<String, PantryItem>();

    while(iterator.hasNext()){
        PantryItem pantryitem = iterator.next().getKvObject().getValue();
        listHash.put(pantryitem.getItemName(), pantryitem);
    }
    return listHash;
}
public static boolean itemConsists(String itemName){

    pantry.containsKey(itemName);
    return true;
}

public static void iteratorMethod(){
    Iterator<PantryItem> pantryItemIterator = pantry.values().iterator();

    while (pantryItemIterator.hasNext()) {
        System.out.println(pantryItemIterator.next());
    }
 }

}
VedantK
  • 9,728
  • 7
  • 66
  • 71

1 Answers1

0

Starting from savePantryItem you are using client variable and it wasn't declared nor initialized anywhere outside this and other methods as well. Consider declaring client as a private instance variable.

PatNowak
  • 5,721
  • 1
  • 25
  • 31