I'm a rookie java programmer and I'm trying to build a system where I add items a user inputs to a jtable but each attempt to add items leads to a java.lang.NullPointerException error when it gets to the quantity to be added. I don't seem to know what is going on. Quantity was declared as an object in jtable properties. Help, please. I received the Exception at warehouses.addItem(item);
This is the code to add item
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String itemName = itemnamTxtfld.getText(), serialNum = itemSerialNotxtfld.getText(), seller = sellerTxtfld.getText(),
expDate = expDateFmtFld.getText(); int quantity = 0;
if (itemnamTxtfld.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "Input Item Name", "Input Error", JOptionPane.WARNING_MESSAGE);
return;
} else if (!itemSerialNotxtfld.getText().matches("(\\d{3}-?){2}\\d{4}")) {
JOptionPane.showMessageDialog(this, "Input Item Serial Number", "Input Error", JOptionPane.WARNING_MESSAGE);
return;
} else if (sellerTxtfld.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "Input seller", "Input Error", JOptionPane.WARNING_MESSAGE);
return;
} //^\d{2}-?\d{2}-?\d{4}$ //^((19|2[0-9])[0-9]{2})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$
else if (!quantityTxtfld.getText().isEmpty()) {
try {
quantity = Integer.parseInt(quantityTxtfld.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Quantity must be a Number", "Quantity Error", JOptionPane.WARNING_MESSAGE);
return;
}
}
if (quantityTxtfld.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "Input quantity", "Input Error", JOptionPane.WARNING_MESSAGE);
} else if (expDateFmtFld.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "Input Date", "Input Error", JOptionPane.WARNING_MESSAGE);
return;
}
if (!expDateFmtFld.getText().matches("^(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-((20|2[0-9])[0-9]{2})$")) {
JOptionPane.showMessageDialog(this, "Input Date", "Input Error", JOptionPane.WARNING_MESSAGE);
return;
} else {
itemAccount Itemaccount = null;
if(typeComboBox.getSelectedItem().toString() == "Alcoholic"){
Itemaccount = new Alcoholic(quantity);
}else if(typeComboBox.getSelectedItem().toString() == "Non Alcoholic"){
Itemaccount = new NonAlcoholic(quantity);
}else if(typeComboBox.getSelectedItem().toString() == "Beverage"){
Itemaccount = new Beverage(quantity);
}else if(typeComboBox.getSelectedItem().toString() == "Biscuit"){
Itemaccount = new Biscuit(quantity);
}else if(typeComboBox.getSelectedItem().toString() == "Food Stuff"){
Itemaccount = new FoodStuff(quantity);
}else{
int confirm = JOptionPane.showConfirmDialog(this, "Do you want to add item?", "Add Item", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION){
item = new Item(itemName, serialNum, seller, expDate, Itemaccount);
warehouses.addItem(item);
this.dispose();}
else{
this.dispose();
}
}
} }
code to add item to table
public void addItemToTable(Item item){
model.addRow(new Object[]{});
reloadItemRowData(model.getRowCount() - 1, item);
to add item to arraylist
ArrayList<Item> goods = new ArrayList<Item>();
void addItem(Item item){
if(itemtExists(item.getItemAccount().getItemID())){
item.getItemAccount().setItemID(findValidItemNumber());
}
goods.add(item);
}
item class
class Item {
private WarehouseApp warehouse;
private final String itemName;
private final String serialNum;
private final String seller;
private final String expDate;
private itemAccount Itemaccount;
Item(String itemName, String serialNum, String seller, String expDate, itemAccount Itemaccount){
this.itemName = itemName;
this.serialNum = serialNum;
this.seller = seller;
this.expDate = expDate;
this.Itemaccount = Itemaccount;
}
/**
* @return the itemName
*/
public String getItemName() {
return itemName;
}
/**
* @return the serialNum
*/
public String getSerialNum() {
return serialNum;
}
/**
* @return the seller
*/
public String getSeller() {
return seller;
}
/**
* @return the expDate
*/
public String getExpDate() {
return expDate;
}
itemAccount getItemAccount() {
return Itemaccount;
}}
get and set quantity
public abstract class itemAccount {
private int quantity = 0;
private int itemID;
private static int NumberOfItems = 1000;
itemAccount(){
itemID = getNextItemID();
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}