-1

我迫切需要一些帮助。我正在做一个 StockManagement 项目。由于错误,我无法将项目插入到数据库中:

org.bil.service.StockManagerImpl.actionPerformed 的线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常(StockManagerImpl.java:163)

我无法解决它。我的代码很长,但我认为这将帮助您找到问题所在。

public class StockManagerImpl implements ActionListener
{
    Date entryDate;
    int itemNo;
    String itemName;
    double unitPrice;
    double qty;
    double totalPrice;
    String supplier;
    String remarks;
    JTextField txtEntryDate;
    JTextField txtItemNo;
    JTextField txtItemName;
    JTextField txtUnitPrice;
    JTextField txtQty;
    JTextField txtTotalPrice;
    JTextField txtSupplier;
    JTextField txtRemarks;
    JButton BttnSaveAdded;

    StockManagerDAO stockManagerDAO;

    Toolkit kit = Toolkit.getDefaultToolkit();
    Dimension screenSize = kit.getScreenSize();
    int screenHeight = screenSize.height;
    int screenWidth = screenSize.width;
    Image img = kit.getImage("images/icon.JPG");

    JFrame newFrame;
    public void addNewItem() {

        newFrame = new JFrame("Add New");
        newFrame.setSize(220, 250);
        newFrame.setResizable(false);
        newFrame.setIconImage(img);

        JLabel lblEntryDate = new JLabel("Item EntryDate: ");
        JLabel lblItemNo = new JLabel("Item No: ");
        JLabel lblItemName = new JLabel("Item Name: ");
        JLabel lblUnitPrice = new JLabel("Item UnitPrice: ");
        JLabel lblQty = new JLabel("Item Qty: ");
        JLabel lblTotalPrice = new JLabel("Item TotalPrice: ");
        JLabel lblSupplier = new JLabel("SupplierDetails: ");
        JLabel lblRemarks = new JLabel("Remarks: ");
        JLabel lblEmpty1 = new JLabel("");
        JLabel lblEmpty2 = new JLabel("");
        //txtEntryDate = new JTextField(10);
        //  JFormattedTextField txtEntryDate = new JFormattedTextField(new SimpleDateFormat("d-M-yyyy"));
        //  txtEntryDate.setValue(new Date());

        txtEntryDate = new JFormattedTextField(new SimpleDateFormat("d-M-yyyy"));
        txtItemNo = new JTextField(10);
        txtItemName = new JTextField(10);
        txtUnitPrice = new JTextField(10);
        txtQty = new JTextField(10);
        txtTotalPrice = new JTextField(10);
        txtSupplier = new JTextField(10);
        txtRemarks = new JTextField(10);

        JButton bttnAdd = new JButton("Save");
        JButton bttnCancel = new JButton("Cancel");

        bttnAdd.addActionListener(this);
        bttnCancel.addActionListener(this);

        JPanel centerPane = new JPanel();
        JPanel bottomPane = new JPanel();

        centerPane.add(lblEntryDate);
        centerPane.add(txtEntryDate);
        centerPane.add(lblItemNo);
        centerPane.add(txtItemNo);
        centerPane.add(lblItemName);
        centerPane.add(txtItemName);
        centerPane.add(lblUnitPrice);
        centerPane.add(txtUnitPrice);
        centerPane.add(lblQty);
        centerPane.add(txtQty);
        centerPane.add(lblTotalPrice);
        centerPane.add(txtTotalPrice);
        centerPane.add(lblSupplier);
        centerPane.add(txtSupplier);
        centerPane.add(lblRemarks);
        centerPane.add(txtRemarks);
        bottomPane.add(bttnAdd);
        bottomPane.add(bttnCancel);
        centerPane.setLayout(new GridLayout(0, 2));

        newFrame.getContentPane().add(centerPane, BorderLayout.CENTER);

        newFrame.getContentPane().add(bottomPane, BorderLayout.SOUTH);
        newFrame.setLocation(screenWidth / 4, screenHeight / 4);
        newFrame.show();
        System.out.println("Show AddItem Window");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      if(e.getActionCommand()=="Save")
      {
            System.out.println("Enterered ActionListener for AddItem");  
            entryDate = (Date)((JFormattedTextField) txtEntryDate).getValue();
            if(txtItemNo.equals(""))
            {
                itemNo = 0;
            }
            try
            {
            itemNo = Integer.parseInt(txtItemNo.getText());
            unitPrice = Double.parseDouble(txtUnitPrice.getText());
            qty = Double.parseDouble(txtQty.getText());
            }
            catch(NumberFormatException ne)
            {
                System.out.println("Integer .ParseInt error" + ne);
            }
            itemName = txtItemName.getText();
            totalPrice = unitPrice * qty;
            supplier = txtSupplier.getText();
            remarks = txtRemarks.getText();

          if (txtItemName.equals(""))
          {
                JOptionPane.showMessageDialog(null, "Please enter Item name.");
                 }
                if (txtUnitPrice.getText() == "") {
                JOptionPane.showMessageDialog(null,
                        "Please enter UnitPrice for the item");
                     }
                 if (txtQty.getText() == "") {
                 JOptionPane.showMessageDialog(null,
                        "Please enter Quantity of item to be added");
               }    else {

                Items item = new Items(entryDate, itemNo, itemName, unitPrice, qty,
                        totalPrice, supplier, remarks);
                stockManagerDAO.addNewItem(item);
                JOptionPane.showMessageDialog(null, "Item Saved");
               }
        if(e.getActionCommand()=="Cancel")
        {
            newFrame.setVisible(false);
        }
      }
    }
    }


Items.java
public class Items
{
       Date entryDate;
       int itemNo;
       String itemName;
       double unitPrice;
       double qty;
       double totalPrice;
       String supplier;
       String remarks;

      // default constructor
      public Items()
      {       
        entryDate = new Date() ;
        itemNo = 0;
        itemName="";
        unitPrice=0;
        qty=0;
        totalPrice=0;
        supplier="";
       remarks="";
      }

    public Items(Date entryDate,int itemNo,String itemName,double unitPrice,double qty,double totalPrice,String supplier,String remarks)
    {
        this.entryDate = entryDate;
        this.itemNo = itemNo;
        this.itemName = itemName;
        this.unitPrice = unitPrice;
        this.qty =qty;
        this.totalPrice=totalPrice;
        this.supplier=supplier;
        this.remarks=remarks;
    } 
    //getter and setter methods

}

希望这两个类足以解决问题。请帮帮我。谢谢

4

2 回答 2

1

我没有看到任何初始化stockManagerDAO,所以它是null

于 2012-05-17T10:37:00.560 回答
0

您正在访问一个没有有效对象值(有null值)的变量,因此您的异常。您需要检查第 163 行StockManagerImpl(它在其 actionPerformed() method) and see what variables can be null at that point. Once identified the variables in question, protect the operation with an excplicit check on whether it isnull` 中:

if (var != null) {
  // use var
}

或者更改代码以确保此时它不能为空

于 2012-05-17T10:37:20.593 回答