I have a problem when i try to print my inventory "Exception in thread "main" java.lang.NullPointerException"

i've made printInventory method for my products in my main method, but when i try to print the inventory after i've called the removeProduct in my inventory, it says 'Exception in thread "main" java.lang.NullPointerException'

public class ProductDatabase {

static Product[] products;

static int productCount = 0;

public void removeProduct(int id) {

for (int i = 0; i < productCount; i++) {

if (products[i].id == id) {

products[i] = null;

productCount--;

break;

}

}

}

public String printInventory() {

System.out.println(String.format("%10s %10s %10s %10s %10s", "ID", "|", "Name", "|", "Price"));

System.out.println(String.format("%s", "-------------------------------------------------------------"));

for (int i = 0; i < productCount; i++) {

Product p = products[i];

System.out.println(String.format("%10s %10s %10s %10s...

Read More »

By: StackOverFlow - Thursday, 8 November

Related Posts