当我尝试在数据库中单击注册按钮时INSERT RECORD(如用户名和密码)时,它通过JOptionPane显示错误“java.lang.NullPointerException”...请告诉我解决方法如何插入记录**
import java.sql.*;
import javax.swing.*;
public class Newuser extends javax.swing.JInternalFrame {
Connection cn;
PreparedStatement st;
ResultSet rs;
String url,sql;
public Newuser() {
initComponents();
}
public void MyConnection()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
url="jdbc:sqlserver://localhost;instance=LALLYDB;databaseName=Menu;user=kd;password=123";
cn=DriverManager.getConnection(url); //above instance ,i.e,LALLYDB is DatabaseServer Name
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(this, ee);
}
}
public void Insert_Record(String username,String passwd)
{
try
{
MyConnection(); //calling MyConnection function
st=cn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery();
sql="Insert into Project values(?,?)";
st=cn.prepareStatement(sql);
st.setString(1,username);
st.setString(2,passwd);
st.executeUpdate();
JOptionPane.showMessageDialog(this,"Record successfully inserted");
cn.close();
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(this, ee);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
nlbl1 = new javax.swing.JLabel();
nlbl2 = new javax.swing.JLabel();
ntxt1 = new javax.swing.JTextField();
ntxt2 = new javax.swing.JTextField();
nlbl3 = new javax.swing.JLabel();
nlbl4 = new javax.swing.JLabel();
nlbl5 = new javax.swing.JLabel();
ntxt3 = new javax.swing.JTextField();
ntxt4 = new javax.swing.JTextField();
regitser = new javax.swing.JButton();
jcombo1 = new javax.swing.JComboBox<>();
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setResizable(true);
setTitle("New User Registration");
setPreferredSize(new java.awt.Dimension(488, 425));
setVisible(true);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
nlbl1.setText("Username");
getContentPane().add(nlbl1, new org.netbeans.lib.awtextra.AbsoluteConstraints(84, 44, -1, -1));
nlbl2.setText("Password");
getContentPane().add(nlbl2, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 80, -1, -1));
getContentPane().add(ntxt1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 40, 140, -1));
getContentPane().add(ntxt2, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 80, 140, -1));
nlbl3.setText("Email-id");
getContentPane().add(nlbl3, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 120, -1, -1));
nlbl4.setText("Hint Question");
getContentPane().add(nlbl4, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 160, -1, -1));
nlbl5.setText("Hint Answer");
getContentPane().add(nlbl5, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 200, -1, -1));
getContentPane().add(ntxt3, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 120, 140, -1));
getContentPane().add(ntxt4, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 200, 140, -1));
regitser.setText("Register");
regitser.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
regitserActionPerformed(evt);
}
});
getContentPane().add(regitser, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 200, -1, -1));
jcombo1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Select", "What is your pet name ?", "What is your school name ?", "What is your favourite sport ?" }));
getContentPane().add(jcombo1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 160, -1, -1));
pack();
}// </editor-fold>
private void regitserActionPerformed(java.awt.event.ActionEvent evt) {
Insert_Record(ntxt1.getText(),ntxt2.getText());
}
// Variables declaration - do not modify
private javax.swing.JComboBox<String> jcombo1;
private javax.swing.JLabel nlbl1;
private javax.swing.JLabel nlbl2;
private javax.swing.JLabel nlbl3;
private javax.swing.JLabel nlbl4;
private javax.swing.JLabel nlbl5;
private javax.swing.JTextField ntxt1;
private javax.swing.JTextField ntxt2;
private javax.swing.JTextField ntxt3;
private javax.swing.JTextField ntxt4;
private javax.swing.JButton regitser;
// End of variables declaration
}
答案 0 :(得分:0)
您通过JOptionPane获得的错误包含一个亚麻,其中发生异常(您正在捕获它并显示它)。在MessageDialog中查看linenumber并查看原因是什么行,或者在catch-blocks中键入ee.printStackTrace(),然后您可以看到抛出异常的语句。也在st = cn.prepareStatement(sql,...);变量sql尚未设置,这是故意的吗?
NullPointerException通常意味着您正在尝试访问之前尚未初始化的变量。