2012-05-17 32 views
-1

我迫切需要一些help.I是做StockManagement project.I我不能夠插入的項目中,因爲錯誤的數據庫:無法獲取項目對象爲正確DB操作

在異常線程 org.bil.service.StockManagerImpl.actionPerformed(StockManagerImpl.java:163) 「AWT-EventQueue的-0」 顯示java.lang.NullPointerException

我不能夠解決這個問題。我的代碼很長,但我認爲這會幫助你找到問題。

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 

} 

希望這兩個類都足以解決問題。 請你幫me.Thank您

+1

問題爲空,因爲該消息指示,在163行的StockManagerImpl.java中發生。您至少可以告訴我們它是哪一行,並且只需使用調試器來了解該行的空值。 –

+0

請在SO上發佈問題時瞭解如何正確設置代碼的格式。StockManagerImpl的第一部分格式不正確,並顯示爲普通文本(提示:在行前至少需要4個空格才能顯示爲代碼) – Attila

回答

1

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

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 is null`:

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

或更改代碼,以確保它不能在這一點上