gpt4 book ai didi

java应用程序查询数据库返回nullpointerException

转载 作者:行者123 更新时间:2023-12-01 13:36:55 25 4
gpt4 key购买 nike

我的 java 代码有问题。它应该将数据插入数据库,但它返回一个空指针异常。这是我的代码。

package client;
import java.sql.*;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class AddCon {


private Connection conn;
private JFrame frmAddContract;
private JTextField ContractNo;
private JTextField EngagerName;
private JTextField contNo;
private JTextField month;
private JTextField day;
private JTextField year;
private JTextField tme;
private JTextField cladd;
private JTextField eventadd;
private JTextField textField_fp;
private JTextField refer;


/**
* Launch the application.
*/
public static void main(String[] args) {



EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddCon window = new AddCon();
window.frmAddContract.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public AddCon() {

try{
//Load database driver and connect
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/KusinaNiKambal","root","1234");
Statement stmt = null;
stmt = conn.createStatement();}

catch (Exception e){
JOptionPane.showMessageDialog(null, e);}

/**
* Initialize the contents of the frame.
*/
initialize();
}
private void initialize() {
frmAddContract = new JFrame();
frmAddContract.setResizable(false);
frmAddContract.setTitle("Add Contract");
frmAddContract.setBounds(100, 100, 450, 640);
frmAddContract.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmAddContract.getContentPane().setLayout(null);

ContractNo = new JTextField();
ContractNo.setBounds(38, 32, 152, 20);
frmAddContract.getContentPane().add(ContractNo);
ContractNo.setColumns(10);

JLabel lblContract = new JLabel("Contract #");
lblContract.setBounds(38, 18, 76, 14);
frmAddContract.getContentPane().add(lblContract);

JLabel lblEngager = new JLabel("Engager");
lblEngager.setBounds(38, 63, 48, 14);
frmAddContract.getContentPane().add(lblEngager);

EngagerName = new JTextField();
EngagerName.setBounds(38, 78, 152, 20);
frmAddContract.getContentPane().add(EngagerName);
EngagerName.setColumns(10);

contNo = new JTextField();
contNo.setBounds(38, 128, 152, 20);
frmAddContract.getContentPane().add(contNo);
contNo.setColumns(10);

JLabel lblNewLabel = new JLabel("Contact #");
lblNewLabel.setBounds(38, 109, 86, 14);
frmAddContract.getContentPane().add(lblNewLabel);

month = new JTextField();
month.setBounds(38, 255, 116, 20);
frmAddContract.getContentPane().add(month);

day = new JTextField();
day.setBounds(181, 255, 81, 20);
frmAddContract.getContentPane().add(day);

year = new JTextField();
year.setBounds(294, 255, 86, 20);
frmAddContract.getContentPane().add(year);
year.setColumns(10);

JLabel lblMonth = new JLabel("Month in number format");
lblMonth.setBounds(38, 236, 100, 14);
frmAddContract.getContentPane().add(lblMonth);

JLabel lblDay = new JLabel("Day");
lblDay.setBounds(181, 236, 48, 14);
frmAddContract.getContentPane().add(lblDay);

JLabel lblYear = new JLabel("Year");
lblYear.setBounds(294, 236, 48, 14);
frmAddContract.getContentPane().add(lblYear);

JLabel lblEventDate = new JLabel("Event Date");
lblEventDate.setBounds(38, 222, 76, 14);
frmAddContract.getContentPane().add(lblEventDate);

tme = new JTextField();
tme.setBounds(38, 307, 59, 20);
frmAddContract.getContentPane().add(tme);
tme.setColumns(10);

cladd = new JTextField();
cladd.setBounds(38, 180, 363, 31);
frmAddContract.getContentPane().add(cladd);
cladd.setColumns(10);

JLabel lblAddress = new JLabel("Client Address");
lblAddress.setBounds(38, 159, 98, 14);
frmAddContract.getContentPane().add(lblAddress);

JLabel lblTime = new JLabel("Time");
lblTime.setBounds(38, 286, 60, 14);
frmAddContract.getContentPane().add(lblTime);



eventadd = new JTextField();
eventadd.setBounds(38, 358, 373, 50);
frmAddContract.getContentPane().add(eventadd);
eventadd.setColumns(10);

JLabel lblEventAddress = new JLabel("Event Address");
lblEventAddress.setBounds(38, 338, 116, 14);
frmAddContract.getContentPane().add(lblEventAddress);

textField_fp = new JTextField();
textField_fp.setBounds(38, 454, 276, 20);
frmAddContract.getContentPane().add(textField_fp);
textField_fp.setColumns(10);

JLabel lblFilepathOfContract = new JLabel("Filepath of Contract - the PDF file");
lblFilepathOfContract.setBounds(38, 431, 116, 14);
frmAddContract.getContentPane().add(lblFilepathOfContract);

JButton btnBrowse = new JButton("Browse...");
btnBrowse.setBounds(324, 453, 89, 23);
frmAddContract.getContentPane().add(btnBrowse);
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(null);
String filepath = fc.getSelectedFile().getAbsolutePath();
textField_fp.setText(filepath);
}
});

refer = new JTextField();
refer.setBounds(38, 510, 276, 20);
frmAddContract.getContentPane().add(refer);
refer.setColumns(10);

JLabel lblEncodedBy = new JLabel("Referred By");
lblEncodedBy.setBounds(38, 485, 116, 14);
frmAddContract.getContentPane().add(lblEncodedBy);




JButton btnAdd = new JButton("Add");
btnAdd.setBounds(101, 560, 89, 23);
frmAddContract.getContentPane().add(btnAdd);
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

try{

String cntr = ContractNo.getText();
String en = EngagerName.getText();
String cont = contNo.getText();
String mo = month.getText();
String d = day.getText();
String yr = year.getText();
String dte = yr + "-" + mo + "-" + d;
String cla = cladd.getText();
String tm = tme.getText() + ":00";
String evadd = eventadd.getText();
String filepath = textField_fp.getText();
String ref = refer.getText();


String SQL = "insert into cis "+
"values ('"+cntr+"','"+en+"','"+
cont+"','"+cla+"' '"+dte+"','"+tm+"','"+
evadd+"','"+filepath+"','"+ref+")";
PreparedStatement stmt = conn.prepareStatement(SQL);
ResultSet rs = stmt.executeQuery();

}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}


});

JButton btnCancel = new JButton("Cancel");
btnCancel.setBounds(225, 560, 89, 23);
frmAddContract.getContentPane().add(btnCancel);
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
frmAddContract.setVisible(false);
}



});
}
}

抱歉,我在java编程方面并不是很有经验,这是我第一次编写查询数据库的代码。

最佳答案

您永远不会初始化您的 conn 实例变量。这将导致这一行(至少)出现 NullPointerException:

PreparedStatement stmt = conn.prepareStatement(SQL);
^------------ NEVER INITIALIZED
<小时/>
public class AddCon {
private Connection conn;

...

// constructor
public AddCon() {
try{
//Load database driver and connect
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(<your parameters>);
^----------- MISTAKE
...

在上面的代码中,您创建了一个连接,但没有将其分配给实例变量,而是使用了局部变量。将该行替换为:

this.conn = DriverManager.getConnection(<your parameters>);

编辑:始终发布堆栈跟踪,以防出现异常!

关于java应用程序查询数据库返回nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21205156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com