1

first of all my Java experience isn't great. Basically when i click the button "Update" it should change the field in the hashmap to what the user has chosen in the Text Field. However when i press update it gives me a NullPointerException at this line:

foundManufacturer.setManufacturer(txtManufacturer.getText().toString().trim());

What can i do to fix this so that it updates the field? Here is my code. The Problem i am having is in the ActionPerformed Method at the bottom

package partA;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.*;

public class EditProductDetails extends JFrame implements ActionListener
{
    private JLabel lblProductID, lblManufacturer, lblName, lblCategory, lblCost, 
    lblSellingPrice;
    private JTextField txtProductID, txtManufacturer, txtName, txtCost,
    txtSellingPrice;
    private JComboBox cmbCategory;
    private JButton btnUpdate, btnSearch, btnExit;
    private JTextArea txtArea;

    private HashMap<String, Stock> products;

    public EditProductDetails(HashMap<String, Stock> sItems)
    {
        products = sItems;

        cmbCategory = new JComboBox();
        String[]supplies = {"Nutrition Bars", "Sports Suplements", "Vitamins", 
                   "Weight Loss", "Accessories"};
        cmbCategory = new JComboBox(supplies);

        btnUpdate = new JButton("Update Product");
        btnUpdate.addActionListener(this);
        btnSearch = new JButton("Search!");
        btnSearch.addActionListener(this);
        btnExit = new JButton("Exit");
        btnExit.addActionListener(this);

        txtProductID = new JTextField("");
        txtManufacturer = new JTextField("");
        txtName = new JTextField("");
        txtCost = new JTextField("");
        txtSellingPrice = new JTextField("");

        txtArea = new JTextArea("");
        txtArea.setEnabled(false);
        txtArea.setBackground(Color.DARK_GRAY);
        txtArea.setForeground(Color.YELLOW);

        lblProductID = new JLabel("Product ID: ");
        lblProductID.setFont(new Font("SansSerif",Font.BOLD,14));
        lblManufacturer = new JLabel("New Manufacturer: ");
        lblManufacturer.setFont(new Font("SansSerif",Font.BOLD,14));
        lblName = new JLabel("New Name: ");
        lblName.setFont(new Font("SansSerif",Font.BOLD,14));
        lblCategory = new JLabel("New Category: ");
        lblCategory.setFont(new Font("SansSerif",Font.BOLD,14));
        lblCost = new JLabel("New Cost: ");
        lblCost.setFont(new Font("SansSerif",Font.BOLD,14));
        lblSellingPrice = new JLabel("New Selling Price: ");
        lblSellingPrice.setFont(new Font("SansSerif",Font.BOLD,14));

        getContentPane().setLayout(new GridBagLayout());

        addComp(lblProductID, 0, 0, 2, 1, 1, 1);
        addComp(lblManufacturer, 0, 2, 2, 1, 1, 1);
        addComp(lblName, 0, 3, 2, 1, 1, 1);
        addComp(lblCategory, 0, 4, 2, 1, 1, 1);
        addComp(lblCost, 0, 5, 2, 1, 1, 1);
        addComp(lblSellingPrice, 0, 6, 2, 1, 1, 1);
        addComp(txtProductID, 2, 0, 2, 1, 1, 1);
        addComp(txtManufacturer, 2, 2, 2, 1, 1, 1);
        addComp(txtName, 2, 3, 2, 1, 1, 1);
        addComp(cmbCategory, 2, 4, 2, 1, 1, 1);
        addComp(txtCost, 2, 5, 2, 1, 1, 1);
        addComp(txtSellingPrice, 2, 6, 2, 1, 1, 1);
        addComp(txtArea, 5, 0, 2, 2, 1, 1);
        addComp(btnSearch, 0, 1, 4, 1, 1, 1);
        addComp(btnUpdate, 0, 7, 4, 1, 1, 1);
        addComp(btnExit, 0, 8, 4, 1, 1, 1);


    }


    private void addComp(Component c, int gridx, int gridy,
            int width, int height,
            int weightx, int weighty)
    {
        GridBagConstraints gc = new GridBagConstraints();

        gc.fill = GridBagConstraints.BOTH;
        gc.insets = new Insets(5, 5, 5, 5);
        gc.gridx = gridx;
        gc.gridy = gridy;
        gc.gridwidth = width; 
        gc.gridheight = height;
        gc.weightx = weightx;
        gc.weighty = weighty;

        getContentPane().add(c, gc);
}


    @Override
    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource()==btnUpdate)
        {
            String findProductID="", findManufacturer="", findName="", findCategory="",
                   findCost="", findSellingPrice="";

            Stock foundItem, foundManufacturer, foundName, foundCategory, foundCost,
            foundSellingPrice;

            findProductID = txtProductID.getText().trim();
            findManufacturer = txtManufacturer.getText().trim();
            findName = txtName.getText().trim();
            findCategory = cmbCategory.getSelectedItem().toString().trim();
            findCost = txtCost.getText().trim();
            findSellingPrice = txtSellingPrice.getText().trim();

            foundItem=products.get(findProductID);
            foundManufacturer=products.get(findManufacturer);
            foundName=products.get(findName);
            foundCategory=products.get(findCategory);
            foundCost=products.get(findCost);
            foundSellingPrice=products.get(findSellingPrice);

            foundManufacturer.setManufacturer(txtManufacturer.getText().toString().trim());
            foundName.setName(txtName.getText().toString().trim());
            foundCategory.setCategory(cmbCategory.getSelectedItem().toString().trim());
            foundCost.setCost(Integer.parseInt(txtCost.getText().toString().trim()));
            foundSellingPrice.setSellingPrice(Integer.parseInt(txtSellingPrice.getText().toString().trim()));


            JOptionPane.showMessageDialog
            (null, "The product with ID " + txtProductID.getText() + " was edited successfully!");
        }

        if(event.getSource()==btnExit)
        {
            this.setVisible(false);
        }

        if(event.getSource()==btnSearch)
        {
            txtArea.setText("");

            String s;
            // Add a header
            s="Product ID.   Manufacturer     Name       Category     No.Sold  No.In Stock  Cost  Sell Price\n";
            txtArea.append(s);
            s="--------------------------------------------------------------------------------------------------------------------------\n";
            txtArea.append(s);

            String findProductID="";
            Stock foundItem;
            findProductID=txtProductID.getText().trim();
            foundItem=products.get(findProductID);
            txtArea.append(foundItem.toString());
        }

    }

}
MichaelG
  • 61
  • 8

0 Answers0