之前是数据库连接的问题,现在变成了空指针的问题,研究了好久还是搞不懂,请大家帮帮我
问题相关代码,请勿粘贴截图
package mymoney;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author 小圈子
*/
public class Login extends javax.swing.JFrame {
/**
* Creates new form Login
*/
Statement st;
Connection con;
String string;
public Login() {
initComponents();
init();
initializeDB();
}
public void init() {
jLabel5.setVisible(false);
jLabel6.setVisible(false);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int isTrue = JOptionPane.showConfirmDialog(null, "确定退出?", "退出", JOptionPane.YES_NO_OPTION);
if (isTrue == JOptionPane.YES_NO_OPTION) {
System.exit(0);
}
}
});
jb3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UserInsert uesrInsert = new UserInsert();
uesrInsert.setVisible(true);
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jf1.setText(null);
jpf.setText(null);
}
});
jrb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jrb.isSelected()==false) {
jpf.setEchoChar('*');
} else {
jpf.setEchoChar((char) 0);
}
}
});
jb1.addActionListener(new ActionListener() {//登陆按钮
public void actionPerformed(ActionEvent e) {
jLabel5.setVisible(false);
String ad = jf1.getText();
String pass = jpf.getText();
int i = 0;
String sc = "select userName,passWord from admin where userName='" + ad + "'";
try {
ResultSet rs = st.executeQuery(sc);
while (rs.next()) {
i++;
String userName = rs.getString("userName");
System.out.println(userName + "sdgdfgdf");
String password = rs.getString("passWord");
if (!ad.equals(userName) || userName.equals("")) {
jLabel5.setVisible(true);
} else if (!pass.equals(password)) {
jLabel6.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "登陆成功!");
Show show = new Show();
show.s = jf1.getText();
show.setVisible(true);
setVisible(false);
}
}
System.out.println(i + "sdfd");
} catch (SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
if (i == 0) {
jLabel5.setVisible(true);
}
}
});
}
public void initializeDB() {
String url = "jdbc:sqlserver://localhost:1433; DatabaseName=MyMoney";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String user = "sa";
String pwd = "111111";
try {
Class.forName(driver);//加载驱动
System.out.println("驱动加载成功");
} catch (ClassNotFoundException event) {
System.out.print("无法创建驱动程式实体!");
}
try {
con = DriverManager.getConnection(url, user, pwd);
System.out.println("引入数据库成功");
st = con.createStatement();
} catch (SQLException e) {
}
}
/**
* 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">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jf1 = new javax.swing.JTextField();
jb1 = new javax.swing.JButton();
jb2 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jb3 = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jpf = new javax.swing.JPasswordField();
jrb = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("管好你的钱");
setFocusable(false);
setLocation(new java.awt.Point(500, 600));
setLocationByPlatform(true);
jLabel1.setForeground(new java.awt.Color(255, 153, 0));
jLabel1.setText("请输入用户名:");
jLabel2.setForeground(new java.awt.Color(255, 153, 0));
jLabel2.setText("请输入密码:");
jb1.setBackground(new java.awt.Color(0, 51, 204));
jb1.setForeground(new java.awt.Color(0, 255, 0));
jb1.setText("登陆");
jb2.setBackground(new java.awt.Color(204, 204, 0));
jb2.setForeground(new java.awt.Color(153, 0, 0));
jb2.setText("重置");
jLabel3.setFont(new java.awt.Font("微软雅黑", 3, 18)); // NOI18N
jLabel3.setForeground(new java.awt.Color(0, 204, 204));
jLabel3.setText("欢迎使用个人财政管理系统!");
jLabel4.setForeground(new java.awt.Color(204, 0, 0));
jLabel4.setText("没有账号?点击注册");
jb3.setText("注册");
jLabel5.setFont(new java.awt.Font("宋体", 0, 10)); // NOI18N
jLabel5.setForeground(new java.awt.Color(255, 0, 0));
jLabel5.setText("用户名输入错误!!!");
jLabel6.setFont(new java.awt.Font("宋体", 0, 10)); // NOI18N
jLabel6.setForeground(new java.awt.Color(255, 51, 51));
jLabel6.setText("密码输入错误!!!");
jrb.setText("显示密码");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(94, 94, 94)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jf1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jrb)
.addComponent(jpf, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel6))
.addGroup(layout.createSequentialGroup()
.addComponent(jb1)
.addGap(18, 18, 18)
.addComponent(jb2)))))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(jb3)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jf1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jrb)
.addGap(13, 13, 13)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jb1)
.addComponent(jb2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jb3)
.addComponent(jLabel4))
.addContainerGap(68, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @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(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JButton jb1;
private javax.swing.JButton jb2;
private javax.swing.JButton jb3;
private javax.swing.JTextField jf1;
private javax.swing.JPasswordField jpf;
private javax.swing.JRadioButton jrb;
// End of variables declaration//GEN-END:variables
}
错误提示:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.sql.Statement.executeQuery(String)" because "this.this$0.st" is null
at mymoney.Login$5.actionPerformed(Login.java:82)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
调换一下顺序,先初始化数据库连接initializeDB();再init();