-1

I've got to make a simple application that orders based on storage in a warehouse but I've run into this NullPointerException in the Warehouse and main classes and cant seem to figure out what the problem is. This is the code:

package com.forward.forward;

import java.util.HashMap;

public class Warehouse
{

public HashMap<String, ProductQuantity> storageMap = new HashMap<>();

public Warehouse() {
}

public void addProductQuantity(String description, ProductQuantity 
quantity)
{
    if(description != quantity.getProduct().getDescription())
    {
        throw new IllegalArgumentException("<devMessage>Please make sure 
that description and Product description match" +
                "when creating objects!");
    }
    storageMap.put(description, quantity);
}

public int getProductQuantity(String description)
{
    if(!storageMap.containsKey(description))
    {
        System.out.println("Product is not present in warehouse!");
    }
    return storageMap.get(description).getQuantity();
}

@Override
public String toString() {
    return "Warehouse{" +
            "storageMap=" + storageMap +
            '}';
}
}

This is the main class:

package com.forward.forward;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ForwardApplication {

public static void main(String[] args)
{

    Product product1 = new Product("Fender Jazz Bass", "Bass", "Relative", "None", 400, 2016);
    Product product2 = new Product("Fender Jaguar", "Guitar", "Relative", "None", 300, 2020);
    Product product3 = new Product("Fender Mustang", "Guitar", "Relative", "None", 200, 2018);
    Product product4 = new Product("Jackson Rhodes V", "Guitar", "Relative", "None", 1000, 1998);
    Product product5 = new Product("Vox VT20+", "Amplifiers", "Relative", "None", 250, 2015);

    ProductQuantity product1Quantity = new ProductQuantity(product1, 2);
    ProductQuantity product2Quantity = new ProductQuantity(product2, 4);
    ProductQuantity product3Quantity = new ProductQuantity(product3, 3);
    ProductQuantity product4Quantity = new ProductQuantity(product4, 0);
    ProductQuantity product5Quantity = new ProductQuantity(product5, 10);

    Warehouse stock = new Warehouse();
    stock.addProductQuantity("Fender Jazz Bass", product1Quantity);
    stock.addProductQuantity("Fender Jaguar", product2Quantity);
    stock.addProductQuantity("Fender Mustang", product3Quantity);
    stock.addProductQuantity("Jackson Rhodes V", product4Quantity);
    stock.addProductQuantity("Vox VT20+", product5Quantity);

    System.out.println("Search for product availability: ");
    Scanner in = new Scanner(System.in);
    String description = in.next();


    ArrayList<Warehouse> productsList = new ArrayList<>();
    productsList.add(stock);
    for (Warehouse w:
         productsList) {
        if (w.getProductQuantity(description) == 0)
        {
            System.out.println("Product unavailable!");
        }
        else
        {
            System.out.println("Available!");
        }
    }

    //System.out.println(productsList);  //made for testing

}

}

The error message when i run the application is as follows:

Search for product availability: 
Jackson Rhodes V
Product is not present in warehouse!
Exception in thread "main" java.lang.NullPointerException
at com.forward.forward.Warehouse.getProductQuantity(Warehouse.java:29)
at 
com.forward.forward.ForwardApplication.main(ForwardApplication.java:44)

Process finished with exit code 1

1 Answers1

1

You need to initialize hashmap

public HashMap<String, ProductQuantity> storageMap = new HashMap<>(); 

or pass instance through constructor

Krzysztof K
  • 736
  • 4
  • 19
  • this fixed things momentarily but now when i use the getProductQuantity method in main i still get the same error but in the return statement of the method. Ill update the code as it is now – Glenis Tivari Jul 21 '21 at 20:44