问题描述
我是 Java 和 JDBC 的新手。我正在编写我的第一个连接到数据库的 Java 应用程序。但是当我单击执行 INSERT 的 JButton 时出现此错误。但我收到此错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at controlbiblioteca.Conexion.execute(Conexion.java:39)
at controlbiblioteca.InsertarPrestamo.btnInsertaractionPerformed(InsertarPrestamo.java:96)
at controlbiblioteca.InsertarPrestamo.access$000(InsertarPrestamo.java:9)
at controlbiblioteca.InsertarPrestamo$1.actionPerformed(InsertarPrestamo.java:66)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
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.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.Lightweightdispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.Lightweightdispatcher.processMouseEvent(Container.java:4525)
at java.awt.Lightweightdispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
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)
在 Conexion.java:39 我有这个:
Statement instruccion;
try {
instruccion = myConn.createStatement(); //line39
instruccion.executeUpdate(sql);
} catch (sqlException ex) {
JOptionPane.showMessageDialog(null,"Error al ejecutar la instruccion");
System.out.println(ex);
System.exit(0);
}
在 Jbutton 中我写了这段代码。
if(txtIDuser.getText().isEmpty()|| txtIDejemplar.getText().isEmpty()|| txtFechpres.getText().isEmpty()|| txtFechdev.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"Favor de llenar todos los campos");
}
else
{
Conexion c = new Conexion();
c.execute("INSERT INTO prestamo VALUES ("+txtIDuser.getText()+","+txtIDejemplar.getText()+",'"+txtFechpres.getText()+"','"+txtFechdev.getText()+"' );");
JOptionPane.showMessageDialog(null,"Insercion realizada correctamente","Exito!",JOptionPane.informatION_MESSAGE);
}
在名为 Bibliotecaria 的类中,显然没有使用私有连接变量。 enter image description here
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)