DAO Setter 在设置 BigDecimal 值时显示 NullPointerException 错误 - Java Swing
DAO Setter shows NullPointerException error when setting the BigDecimal value - Java Swing
设置十进制值时出现以下错误。请忽略命名约定,抱歉,它是从遗留应用程序复制的。
错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.shams.MainWindow.btnSaveActionPerformed(MainWindow.java:161)
at com.shams.MainWindow.access0(MainWindow.java:25)
at com.shams.MainWindow.actionPerformed(MainWindow.java:85)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:719)
at java.awt.EventQueue.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
ExpenseDTO
import java.math.BigDecimal;
import java.util.Date;
//
public class Expense {
BigDecimal fltAmt=BigDecimal.ZERO;
Date dtDate;
Category category;
Long intExpenseId;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public Long getIntExpenseId() {
return intExpenseId;
}
public void setIntExpenseId(Long intExpenseId) {
this.intExpenseId = intExpenseId;
}
public BigDecimal getFltAmt() {
return fltAmt;
}
public void setFltAmt(BigDecimal fltAmt) {
this.fltAmt = fltAmt;
}
public Date getDtDate() {
return dtDate;
}
public void setDtDate(Date dtDate) {
this.dtDate = dtDate;
}
}
主窗口
.......
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Date selectedDate = (Date) datePicker.getModel().getValue();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
CBItem selected_item = (CBItem) txtCategory.getSelectedItem();
System.out.println(selected_item.getId());
System.out.println(selected_item.getName());
lbl.setText(txtAmt.getText() + "," + txtCategory.getSelectedIndex() + ","+ dateFormat.format(selectedDate));
/* AddNewPanel.add(lbl);
AddNewPanel.revalidate();
AddNewPanel.repaint();*/
BigDecimal vl=new BigDecimal(txtAmt.getText());
System.out.print(BigDecimal.ONE);
expense.setFltAmt(BigDecimal.ONE);
/*expense.setDtDate(new Date(dateFormat.format(selectedDate)));
category.setIntCatId(selected_item.getId());
expense.setCategory(category);*/
}
我知道错误是设置 BigDecimal 的行,但无法确定导致问题的原因。当我评论这行时。我在这里只复制了必需的(我认为)代码。任何人都想要完整的代码,我也可以复制它。任何帮助都会很棒...!!
谢谢...
已编辑- 主窗口代码完全...
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.shams;
import com.shams.dto.Category;
import com.shams.dto.Expense;
import com.shams.utils.DBConnection;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JLabel;
import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;
/**
*
* @author Shamseer
*/
public class MainWindow extends javax.swing.JFrame {
Expense expense;
Category category;
/**
* Creates new form MainWindow
*/
public MainWindow() {
initComponents();
initMyComponents();
loadData();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
mainPanel = new javax.swing.JPanel();
AddNewPanel = new javax.swing.JPanel();
txtCategory = new javax.swing.JComboBox();
txtAmt = new javax.swing.JTextField();
btnSave = new javax.swing.JButton();
ViewPanel = new javax.swing.JPanel();
txtReportType = new javax.swing.JComboBox();
menuBarETApp = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
menuAddNew = new javax.swing.JMenuItem();
viewMenu = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
mainPanel.setLayout(new java.awt.CardLayout());
txtCategory.setEditable(true);
txtCategory.setAutoscrolls(true);
txtCategory.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtCategoryActionPerformed(evt);
}
});
txtCategory.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
txtCategoryKeyTyped(evt);
}
});
AddNewPanel.add(txtCategory);
txtAmt.setPreferredSize(new java.awt.Dimension(50, 25));
AddNewPanel.add(txtAmt);
btnSave.setText("Save");
btnSave.setToolTipText("");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
AddNewPanel.add(btnSave);
mainPanel.add(AddNewPanel, "cardAddNewPanel");
txtReportType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
javax.swing.GroupLayout ViewPanelLayout = new javax.swing.GroupLayout(ViewPanel);
ViewPanel.setLayout(ViewPanelLayout);
ViewPanelLayout.setHorizontalGroup(
ViewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ViewPanelLayout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(txtReportType, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(516, Short.MAX_VALUE))
);
ViewPanelLayout.setVerticalGroup(
ViewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ViewPanelLayout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(txtReportType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(414, Short.MAX_VALUE))
);
mainPanel.add(ViewPanel, "cardViewPanel");
getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
fileMenu.setText("File");
menuAddNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, 0));
menuAddNew.setText("Add New");
fileMenu.add(menuAddNew);
menuBarETApp.add(fileMenu);
viewMenu.setText("View");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, 0));
jMenuItem1.setText("View Transactions");
jMenuItem1.setToolTipText("");
viewMenu.add(jMenuItem1);
menuBarETApp.add(viewMenu);
setJMenuBar(menuBarETApp);
pack();
}// </editor-fold>
private void txtCategoryActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtCategoryKeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Date selectedDate = (Date) datePicker.getModel().getValue();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
CBItem selected_item = (CBItem) txtCategory.getSelectedItem();
System.out.println(selected_item.getId());
System.out.println(selected_item.getName());
lbl.setText(txtAmt.getText() + "," + txtCategory.getSelectedIndex() + ","+ dateFormat.format(selectedDate));
/* AddNewPanel.add(lbl);
AddNewPanel.revalidate();
AddNewPanel.repaint();*/
BigDecimal vl=new BigDecimal(txtAmt.getText());
System.out.print(BigDecimal.ONE);
expense.setFltAmt(BigDecimal.ONE);
/*expense.setDtDate(new Date(dateFormat.format(selectedDate)));
category.setIntCatId(selected_item.getId());
expense.setCategory(category);*/
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel AddNewPanel;
private javax.swing.JPanel ViewPanel;
private javax.swing.JButton btnSave;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuItem menuAddNew;
private javax.swing.JMenuBar menuBarETApp;
private javax.swing.JTextField txtAmt;
private javax.swing.JComboBox txtCategory;
private javax.swing.JComboBox txtReportType;
private javax.swing.JMenu viewMenu;
// End of variables declaration
UtilDateModel model = new UtilDateModel();
JDatePanelImpl datePanel = new JDatePanelImpl(model);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel);
JLabel lbl=new JLabel("");
@SuppressWarnings("unchecked")
private void loadData() {
//JDatePickerUtil datPick = new JDatePickerUtil();
//CBItem array=new CBItem[]{"test",9};
//Vector vctItem = new Vector();
//CBItem[] cbIt = null;//=new ArrayList<>();
try{
Connection conn = new DBConnection().createConnection();
Statement stmt = conn.createStatement();
String sql = "Select * from category";
//List<String> catNames= new ArrayList<>();
ResultSet rs = stmt.executeQuery(sql);
//int size=0;
while(rs.next()){
//catNames.add(rs.getString("strCatName"));
//cbIt=new CBItem[size+1];
//cbIt[size]=new CBItem(rs.getInt("intCatId"), rs.getString("strCatName"));//.addElement( new CBItem(1, "car" ) );
txtCategory.addItem(new CBItem(rs.getInt("intCatId"), rs.getString("strCatName")));
//size++;
}
//txtCategory.addItem(cbIt);
}
catch(Exception e){
e.printStackTrace();
}
}
private void initMyComponents() {
AddNewPanel.add(datePicker);
AddNewPanel.revalidate();
AddNewPanel.repaint();
}
}
class CBItem{
int id;
String name;
CBItem(int i, String car) {
this.id=i;
this.name=car;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return name+" ("+id+")";
}
}
你有:
Expense expense;
已定义变量,但从未设置。那就是您获得 NPE 的地方。你不应该至少初始化它吗?
Expense expense = new Expense();
设置十进制值时出现以下错误。请忽略命名约定,抱歉,它是从遗留应用程序复制的。
错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.shams.MainWindow.btnSaveActionPerformed(MainWindow.java:161)
at com.shams.MainWindow.access0(MainWindow.java:25)
at com.shams.MainWindow.actionPerformed(MainWindow.java:85)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:719)
at java.awt.EventQueue.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
ExpenseDTO
import java.math.BigDecimal;
import java.util.Date;
//
public class Expense {
BigDecimal fltAmt=BigDecimal.ZERO;
Date dtDate;
Category category;
Long intExpenseId;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public Long getIntExpenseId() {
return intExpenseId;
}
public void setIntExpenseId(Long intExpenseId) {
this.intExpenseId = intExpenseId;
}
public BigDecimal getFltAmt() {
return fltAmt;
}
public void setFltAmt(BigDecimal fltAmt) {
this.fltAmt = fltAmt;
}
public Date getDtDate() {
return dtDate;
}
public void setDtDate(Date dtDate) {
this.dtDate = dtDate;
}
}
主窗口
.......
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Date selectedDate = (Date) datePicker.getModel().getValue();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
CBItem selected_item = (CBItem) txtCategory.getSelectedItem();
System.out.println(selected_item.getId());
System.out.println(selected_item.getName());
lbl.setText(txtAmt.getText() + "," + txtCategory.getSelectedIndex() + ","+ dateFormat.format(selectedDate));
/* AddNewPanel.add(lbl);
AddNewPanel.revalidate();
AddNewPanel.repaint();*/
BigDecimal vl=new BigDecimal(txtAmt.getText());
System.out.print(BigDecimal.ONE);
expense.setFltAmt(BigDecimal.ONE);
/*expense.setDtDate(new Date(dateFormat.format(selectedDate)));
category.setIntCatId(selected_item.getId());
expense.setCategory(category);*/
}
我知道错误是设置 BigDecimal 的行,但无法确定导致问题的原因。当我评论这行时。我在这里只复制了必需的(我认为)代码。任何人都想要完整的代码,我也可以复制它。任何帮助都会很棒...!!
谢谢...
已编辑- 主窗口代码完全...
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.shams;
import com.shams.dto.Category;
import com.shams.dto.Expense;
import com.shams.utils.DBConnection;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JLabel;
import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;
/**
*
* @author Shamseer
*/
public class MainWindow extends javax.swing.JFrame {
Expense expense;
Category category;
/**
* Creates new form MainWindow
*/
public MainWindow() {
initComponents();
initMyComponents();
loadData();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
mainPanel = new javax.swing.JPanel();
AddNewPanel = new javax.swing.JPanel();
txtCategory = new javax.swing.JComboBox();
txtAmt = new javax.swing.JTextField();
btnSave = new javax.swing.JButton();
ViewPanel = new javax.swing.JPanel();
txtReportType = new javax.swing.JComboBox();
menuBarETApp = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
menuAddNew = new javax.swing.JMenuItem();
viewMenu = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
mainPanel.setLayout(new java.awt.CardLayout());
txtCategory.setEditable(true);
txtCategory.setAutoscrolls(true);
txtCategory.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtCategoryActionPerformed(evt);
}
});
txtCategory.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
txtCategoryKeyTyped(evt);
}
});
AddNewPanel.add(txtCategory);
txtAmt.setPreferredSize(new java.awt.Dimension(50, 25));
AddNewPanel.add(txtAmt);
btnSave.setText("Save");
btnSave.setToolTipText("");
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
AddNewPanel.add(btnSave);
mainPanel.add(AddNewPanel, "cardAddNewPanel");
txtReportType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
javax.swing.GroupLayout ViewPanelLayout = new javax.swing.GroupLayout(ViewPanel);
ViewPanel.setLayout(ViewPanelLayout);
ViewPanelLayout.setHorizontalGroup(
ViewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ViewPanelLayout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(txtReportType, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(516, Short.MAX_VALUE))
);
ViewPanelLayout.setVerticalGroup(
ViewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ViewPanelLayout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(txtReportType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(414, Short.MAX_VALUE))
);
mainPanel.add(ViewPanel, "cardViewPanel");
getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
fileMenu.setText("File");
menuAddNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, 0));
menuAddNew.setText("Add New");
fileMenu.add(menuAddNew);
menuBarETApp.add(fileMenu);
viewMenu.setText("View");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, 0));
jMenuItem1.setText("View Transactions");
jMenuItem1.setToolTipText("");
viewMenu.add(jMenuItem1);
menuBarETApp.add(viewMenu);
setJMenuBar(menuBarETApp);
pack();
}// </editor-fold>
private void txtCategoryActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtCategoryKeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Date selectedDate = (Date) datePicker.getModel().getValue();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
CBItem selected_item = (CBItem) txtCategory.getSelectedItem();
System.out.println(selected_item.getId());
System.out.println(selected_item.getName());
lbl.setText(txtAmt.getText() + "," + txtCategory.getSelectedIndex() + ","+ dateFormat.format(selectedDate));
/* AddNewPanel.add(lbl);
AddNewPanel.revalidate();
AddNewPanel.repaint();*/
BigDecimal vl=new BigDecimal(txtAmt.getText());
System.out.print(BigDecimal.ONE);
expense.setFltAmt(BigDecimal.ONE);
/*expense.setDtDate(new Date(dateFormat.format(selectedDate)));
category.setIntCatId(selected_item.getId());
expense.setCategory(category);*/
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainWindow().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel AddNewPanel;
private javax.swing.JPanel ViewPanel;
private javax.swing.JButton btnSave;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuItem menuAddNew;
private javax.swing.JMenuBar menuBarETApp;
private javax.swing.JTextField txtAmt;
private javax.swing.JComboBox txtCategory;
private javax.swing.JComboBox txtReportType;
private javax.swing.JMenu viewMenu;
// End of variables declaration
UtilDateModel model = new UtilDateModel();
JDatePanelImpl datePanel = new JDatePanelImpl(model);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel);
JLabel lbl=new JLabel("");
@SuppressWarnings("unchecked")
private void loadData() {
//JDatePickerUtil datPick = new JDatePickerUtil();
//CBItem array=new CBItem[]{"test",9};
//Vector vctItem = new Vector();
//CBItem[] cbIt = null;//=new ArrayList<>();
try{
Connection conn = new DBConnection().createConnection();
Statement stmt = conn.createStatement();
String sql = "Select * from category";
//List<String> catNames= new ArrayList<>();
ResultSet rs = stmt.executeQuery(sql);
//int size=0;
while(rs.next()){
//catNames.add(rs.getString("strCatName"));
//cbIt=new CBItem[size+1];
//cbIt[size]=new CBItem(rs.getInt("intCatId"), rs.getString("strCatName"));//.addElement( new CBItem(1, "car" ) );
txtCategory.addItem(new CBItem(rs.getInt("intCatId"), rs.getString("strCatName")));
//size++;
}
//txtCategory.addItem(cbIt);
}
catch(Exception e){
e.printStackTrace();
}
}
private void initMyComponents() {
AddNewPanel.add(datePicker);
AddNewPanel.revalidate();
AddNewPanel.repaint();
}
}
class CBItem{
int id;
String name;
CBItem(int i, String car) {
this.id=i;
this.name=car;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return name+" ("+id+")";
}
}
你有:
Expense expense;
已定义变量,但从未设置。那就是您获得 NPE 的地方。你不应该至少初始化它吗?
Expense expense = new Expense();