我想在我的JFrame(查看汽车)中创建一个按钮,该按钮将显示带表的新JFrame。我想用MySQL数据库中的数据填充表格。当我启动程序时,它工作正常,但当我尝试单击应该显示我的JFrame与表的按钮时,它显示错误“conn”为空。我在Java方面比较新,所以如果我的代码中有一些愚蠢的东西,那就很抱歉。你能简单解释一下我的错误以及我应该改变什么吗?
MyJFrame代码:
public class MyJFrame {
private JFrame frame;
public static void main(String[] args) {
String dbHost="localhost";
String dbDatabase="cars";
String dbUser = "root";
String dbPassword = "";
CarDAO carDAO = new CarDAO();
ResultSet result = null;
try {
// register driver
Class.forName("com.mysql.jdbc.Driver");
// Make Connection Url
String connectionUrl = "jdbc:mysql://" + dbHost
+ "/" + dbDatabase
+ "?user=" + dbUser
+ "&password=" + dbPassword;
//open Connection
Connection conn = DriverManager.getConnection(connectionUrl);
carDAO.setConn(conn);
conn.close();
}catch (ClassNotFoundException cnfe){
throw new RuntimeException(cnfe);
}catch (SQLException sqle) {
throw new RuntimeException(sqle);
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyJFrame window = new MyJFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyJFrame() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 472, 346);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Create Car");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
CreateCar createCar = new CreateCar();
createCar.setVisible(true);
}
});
btnNewButton.setBounds(10, 125, 135, 46);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Search Car For Sale");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton_1.setBounds(10, 68, 135, 46);
frame.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Update Entry");
btnNewButton_2.setBounds(10, 182, 135, 46);
frame.getContentPane().add(btnNewButton_2);
JButton btnNewButton_4 = new JButton("Sold Car");
btnNewButton_4.setBounds(10, 239, 135, 46);
frame.getContentPane().add(btnNewButton_4);
JButton btnNewButton_3 = new JButton("View Cars");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
ShowCars showCars = new ShowCars();
showCars.setVisible(true);
}
});
btnNewButton_3.setBounds(10, 11, 135, 46);
frame.getContentPane().add(btnNewButton_3);
JLabel lblNewLabel = new JLabel("Choose your option");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 17));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setBounds(160, 11, 190, 46);
frame.getContentPane().add(lblNewLabel);
}
}
ShowCars类:
public class ShowCars extends JFrame {
private JPanel contentPane;
private JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShowCars frame = new ShowCars();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ShowCars() {
CarDAO carDAO = new CarDAO();
table = new JTable();
carDAO.showCars(table);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 489, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 453, 339);
contentPane.add(scrollPane);
scrollPane.setViewportView(table);
}
}
CarDAO课程:
public class CarDAO {
String type;
int price;
String date;
int ID;
static String sql;
String year;
String month;
String day;
String word;
Connection conn;
boolean show = false;
static int option;
Scanner input = new Scanner(System.in);
Cars cars = new Cars();
MyJFrame myJFrame = new MyJFrame();
public void setConn (Connection Conn){
this.conn = Conn;
}
public void showCars (JTable table){
try{
String sql = "select * from cars;";
// prepare Statement
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
}catch (Exception e) {
e.printStackTrace();
}
option = 2;
}
}
错误消息:
java.lang.NullPointerException
at databaseProject.CarDAO.showCars(CarDAO.java:64)
at databaseProject.ShowCars.<init>(ShowCars.java:41)
at databaseProject.MyJFrame$4.actionPerformed(MyJFrame.java:120)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:-1)
CarDAO carDAO = new CarDAO();
table = new JTable();
carDAO.showCars(table);
您忘了设置连接。
编辑:基本上你的节目非常混乱&#34;。总结一下:
开始阅读的好地方可能是https://en.wikipedia.org/wiki/Multitier_architecture
当您了解了基础知识后,您可以拥有一个框架(如Spring Boot)来处理所有&#34;样板&#34;代码,为您提供连接池,事务处理等。