-1

私は必死に助けが必要です.私はStockManagementプロジェクトを行っています.エラーのため、アイテムをデータベースに挿入できません:

スレッド「AWT-EventQueue-0」での例外 java.lang.NullPointerException at org.bil.service.StockManagerImpl.actionPerformed(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

}

この 2 つのクラスで問題を解決できることを願っています。私を助けてください。ありがとう

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
}

または、コードを変更して、その時点でnullにならないようにします

于 2012-05-17T10:37:20.593 に答える