很抱歉打扰大家,现在我在这方面遇到了问题。这段代码说它是一个 java.lang.nullpointerexception,我已经为此工作了将近 2 个小时。下面的代码给出了错误。
try
{
increment = transnum+1;
String hmo = cbHMO.getSelectedItem().toString();
String testname = cbTests.getSelectedItem().toString();
int amount = Integer.parseInt(txtTestAmount.getText());
double discount = Double.parseDouble(txtDiscountAmount.getText());
double total = Double.parseDouble(txtTotal.getText());
double amountpaid = Double.parseDouble(txtPAMount.getText());
String refer = txtReferred.getText();
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/smartcare", "root", "");
stmt = conn.createStatement();
String sql = "INSERT INTO transactions (transnum, patientid, transtype, paymenttype, initialamount, discount, total, amountpaid, hmo, referral, time, date) values (?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement preparedStmt = conn.prepareStatement(sql);
preparedStmt.setInt(1, increment);
preparedStmt.setInt(2, pid);
preparedStmt.setString(3, testname);
preparedStmt.setString(4,ptype);
preparedStmt.setInt(5, amount);
preparedStmt.setDouble(6, discount);
preparedStmt.setDouble(7, total);
preparedStmt.setDouble(8,amountpaid);
preparedStmt.setString(9, hmo);
preparedStmt.setString(10, refer);
preparedStmt.setTime(11, time);
preparedStmt.setDate(12, daten);
preparedStmt.execute();
increment++;
JOptionPane.showMessageDialog(null, "Added Successfully");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
现在在同一个 jbutton 上,也有一个代码可以插入到 mysql 数据库的不同部分,如果我在顶部注释代码并只留下下面的代码,它就可以工作。这是真正令人头疼的人。留下下面的代码
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/smartcare", "root", "");
stmt = conn.createStatement();
PatientsList pl = new PatientsList();
pid = pl.getPID();
String fname = pl.getFname();
String lname = pl.getLname();
//DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//DateFormat timeformat = new SimpleDateFormat("HH:MM:SS");
//Date dateobj = new Date();
// Calendar calobj = Calendar.getInstance();
// java.util.Date date = new java.util.Date();
java.sql.Time time = getCurrentJavaSqlTime();
java.sql.Date daten = getCurrentJavaSqlDate();
User u = new User();
int uid=u.getPID();
//JOptionPane.showMessageDialog(null, dateFormat.format(calobj.getTime()));
//JOptionPane.showMessageDialog(null, timeformat.format(calobj.getTime()));
wincrement = qnumber+1;
String sql2 = "INSERT INTO patientswait(queuenumber,patientid,firstname,lastname,time,date,status,userid, deptid) values (?,?,?,?,?,?,?,?,?)";
PreparedStatement preparedStmt = conn.prepareStatement(sql2);
preparedStmt.setInt(1, wincrement);
preparedStmt.setInt(2, pid);
preparedStmt.setString(3, fname);
preparedStmt.setString(4, lname);
preparedStmt.setTime(5, time);
preparedStmt.setDate(6, daten);
preparedStmt.setString(7, "not sent");
preparedStmt.setInt(8, uid);
preparedStmt.setInt(9, did);
preparedStmt.execute();
increment++;
//conn.close();
JOptionPane.showMessageDialog(null, "Added Successfully");
}// TODO add your handling code here:
catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
} // TODO add your handling code here:
如果有人能指出这将是太棒了。哦,是的图像在通用界面界面下方
这是我拿出来时出现的
increment = transnum+1;
String hmo = cbHMO.getSelectedItem().toString();
String testname = cbTests.getSelectedItem().toString();
int amount = Integer.parseInt(txtTestAmount.getText());
double discount = Double.parseDouble(txtDiscountAmount.getText());
double total = Double.parseDouble(txtTotal.getText());
double amountpaid = Double.parseDouble(txtPAMount.getText());
结果就是我的 catch 语句不会捕获它。如果声明不在 try 语句中,则不会出现下面的堆栈跟踪。输出菜单上没有堆栈跟踪
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at smartcare.CashierCustomer.bSubmitMouseClicked(CashierCustomer.java:769)
at smartcare.CashierCustomer.access$600(CashierCustomer.java:28)
at smartcare.CashierCustomer$9.mouseClicked(CashierCustomer.java:331)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6538)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
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)