我已经在堆栈溢出中遵循了几个解决方案并实现了这种做法。但问题并没有得到解决。实际上,当我点击详细更新按钮时,我得到一个NullPointException。这是代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class EditStdReg extends JInternalFrame implements ActionListener {
private JPanel panel;
private JButton btnUpdate, btnCancel, btnView;
private JLabel lblName, lblAdNo, lblPhNo, lblSex, lblFatherName, lblOccupation;
private JLabel lblMotherName, lblDOB, lblAge, lblCaste, lblReligion, lblHouseName;
private JLabel lblCity, lblDistrict, lblState, lblPin, lblYear, lblQualification;
private JTextField txtName, txtAdNo, txtPhNo, txtFatherName, txtOccupation;
private JTextField txtMotherName, txtDOB, txtAge, txtCaste, txtReligion, txtHouseName;
private JTextField txtCity, txtDistrict, txtState, txtPin, txtYear, txtQualification;
private JComboBox cmbSex;
private Connection con;
private Statement st;
private PreparedStatement ps;
private int adno;
public EditStdReg() {
super("Edit Student Registration", true, true, true, true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setSize(1000, 1000);
panel = new JPanel();
lblAdNo = new JLabel("Admission no");
lblName = new JLabel("Name");
lblPhNo = new JLabel("Phone no");
lblSex = new JLabel("Sex");
lblFatherName = new JLabel("Father's name");
lblOccupation = new JLabel("Occupation");
lblMotherName = new JLabel("Mother's name");
lblDOB = new JLabel("DOB");
lblAge = new JLabel("Age");
lblCaste = new JLabel("Caste");
lblReligion = new JLabel("Religion");
lblHouseName = new JLabel("House Name");
lblCity = new JLabel("City");
lblDistrict = new JLabel("District");
lblState = new JLabel("State");
lblPin = new JLabel("Pin");
lblYear = new JLabel("Year");
lblQualification = new JLabel("Qualification");
txtName = new JTextField();
txtAdNo = new JTextField();
txtPhNo = new JTextField();
//txtSex = new JTextField();
cmbSex = new JComboBox();
cmbSex.addItem("MALE");
cmbSex.addItem("FEMALE");
cmbSex.setSelectedIndex(0);
txtFatherName = new JTextField();
txtOccupation = new JTextField();
txtMotherName = new JTextField();
txtDOB = new JTextField();
txtAge = new JTextField();
txtCaste = new JTextField();
txtReligion = new JTextField();
txtHouseName = new JTextField();
txtCity = new JTextField();
txtDistrict = new JTextField();
txtState = new JTextField();
txtPin = new JTextField();
txtQualification = new JTextField();
txtYear = new JTextField();
btnUpdate = new JButton("Update", new ImageIcon(ClassLoader.getSystemResource("src/images/update.png")));
btnCancel = new JButton("Cancel", new ImageIcon(ClassLoader.getSystemResource("src/images/cancel.png")));
btnView = new JButton("View", new ImageIcon(ClassLoader.getSystemResource("src/images/view.png")));
btnUpdate.addActionListener(this);
btnCancel.addActionListener(this);
btnView.addActionListener(this);
panel.setLayout(null);
lblAdNo.setBounds(100, 50, 150, 25);
txtAdNo.setBounds(200, 50, 200, 25);
lblName.setBounds(100, 90, 150, 25);
txtName.setBounds(200, 90, 200, 25);
lblPhNo.setBounds(100, 130, 150, 25);
txtPhNo.setBounds(200, 130, 200, 25);
lblSex.setBounds(100, 170, 150, 25);
cmbSex.setBounds(200, 170, 200, 25);
lblFatherName.setBounds(100, 210, 150, 25);
txtFatherName.setBounds(200, 210, 200, 25);
lblOccupation.setBounds(100, 250, 100, 25);
txtOccupation.setBounds(200, 250, 200, 25);
lblMotherName.setBounds(100, 290, 100, 25);
txtMotherName.setBounds(200, 290, 200, 25);
lblDOB.setBounds(100, 330, 150, 25);
txtDOB.setBounds(200, 330, 200, 25);
lblCaste.setBounds(100, 370, 150, 25);
txtCaste.setBounds(200, 370, 200, 25);
lblAge.setBounds(500, 90, 150, 25);
txtAge.setBounds(600, 90, 200, 25);
lblReligion.setBounds(500, 130, 100, 25);
txtReligion.setBounds(600, 130, 200, 25);
lblHouseName.setBounds(500, 170, 100, 25);
txtHouseName.setBounds(600, 170, 200, 25);
lblCity.setBounds(500, 210, 150, 25);
txtCity.setBounds(600, 210, 200, 25);
lblDistrict.setBounds(500, 250, 100, 25);
txtDistrict.setBounds(600, 250, 200, 25);
lblState.setBounds(500, 290, 50, 25);
txtState.setBounds(600, 290, 200, 25);
lblPin.setBounds(500, 330, 150, 25);
txtPin.setBounds(600, 330, 200, 25);
lblYear.setBounds(500, 370, 100, 25);
txtYear.setBounds(600, 370, 200, 25);
lblQualification.setBounds(100, 420, 80, 25);
txtQualification.setBounds(200, 420, 600, 25);
btnUpdate.setBounds(300, 470, 100, 25);
btnCancel.setBounds(450, 470, 100, 25);
btnView.setBounds(600, 470, 100, 25);
btnUpdate.setEnabled(false);
panel.add(lblAdNo);
panel.add(txtAdNo);
panel.add(lblName);
panel.add(txtName);
panel.add(lblPhNo);
panel.add(txtPhNo);
panel.add(lblSex);
panel.add(cmbSex);
panel.add(lblFatherName);
panel.add(txtFatherName);
panel.add(lblOccupation);
panel.add(txtOccupation);
panel.add(lblMotherName);
panel.add(txtMotherName);
panel.add(lblDOB);
panel.add(txtDOB);
panel.add(lblAge);
panel.add(txtAge);
panel.add(lblCaste);
panel.add(txtCaste);
panel.add(lblReligion);
panel.add(txtReligion);
panel.add(lblHouseName);
panel.add(txtHouseName);
panel.add(lblCity);
panel.add(txtCity);
panel.add(lblDistrict);
panel.add(txtDistrict);
panel.add(lblState);
panel.add(txtState);
panel.add(lblPin);
panel.add(txtPin);
panel.add(txtYear);
panel.add(lblYear);
panel.add(lblQualification);
panel.add(txtQualification);
panel.add(btnUpdate);
panel.add(btnCancel);
panel.add(btnView);
add(panel, BorderLayout.CENTER);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/anu?zeroDateTimeBehavior=convertToNull","root","root");
st = con.createStatement();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error on connection to database, cannot continue updation process", "Error", JOptionPane.ERROR_MESSAGE);
}//outer try catch closed
}//constructor closed
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("Update")) {
try {
if(adno!=Integer.parseInt(txtAdNo.getText())){
JOptionPane.showMessageDialog(null,"Roll number cannot be changed","Updation error",JOptionPane.ERROR_MESSAGE);
return;
}
String sql = "UPDATE student SET SName=?,Phno=?,Sex=?,Fname=?,Occupation=?,Mname=?,Dob=?" +
",Age=?,Caste=?,Religion=?,Hname=?,City=?,District=?,State=?,Pin=?,Year=?,Qualification=?" +
" WHERE Rollno="+ adno;
ps=con.prepareStatement(sql);
ps.setString(1,txtName.getText());
ps.setString(2,txtPhNo.getText());
ps.setString(3,cmbSex.getSelectedItem().toString());
ps.setString(4,txtFatherName.getText());
ps.setString(5,txtOccupation.getText());
ps.setString(6,txtMotherName.getText());
ps.setString(7,txtDOB.getText());
ps.setInt(8,Integer.parseInt(txtAge.getText()));
ps.setString(9,txtCaste.getText());
ps.setString(10,txtReligion.getText());
ps.setString(11,txtHouseName.getText());
ps.setString(12,txtCity.getText());
ps.setString(13,txtDistrict.getText());
ps.setString(14,txtState.getText());
ps.setString(15,txtPin.getText());
ps.setInt(16,Integer.parseInt(txtYear.getText()));
ps.setString(17,txtQualification.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Registration details successfully updated", "Success", JOptionPane.INFORMATION_MESSAGE);
ClearForm();
btnUpdate.setEnabled(false);
} catch (Exception x) {
//JOptionPane.showMessageDialog(null, "Error on database operation,Updation failure", "Error", JOptionPane.ERROR_MESSAGE);
x.printStackTrace();
}//inner try catch closed
}//if closed
if (e.getActionCommand().equalsIgnoreCase("View")) {
try {
ResultSet rs = st.executeQuery("SELECT * FROM student WHERE RollNo=" + txtAdNo.getText());
if (rs.next()) {
adno = Integer.parseInt(txtAdNo.getText());
txtName.setText(rs.getString("SName"));
cmbSex.setSelectedItem(rs.getString("Sex"));
txtFatherName.setText(rs.getString("FName"));
txtMotherName.setText(rs.getString("MName"));
txtAge.setText(rs.getString("Age"));
txtOccupation.setText(rs.getString("Occupation"));
txtPhNo.setText(rs.getString("phno"));
txtReligion.setText(rs.getString("Religion"));
txtHouseName.setText(rs.getString("Hname"));
txtCaste.setText(rs.getString("Caste"));
txtCity.setText(rs.getString("City"));
txtState.setText(rs.getString("State"));
txtDOB.setText(rs.getDate("Dob").toString());
txtDistrict.setText(rs.getString("District"));
txtPin.setText(rs.getString("Pin"));
txtYear.setText(rs.getString("Year"));
txtQualification.setText(rs.getString("Qualification"));
btnUpdate.setEnabled(true);
} else {
JOptionPane.showMessageDialog(null, "Roll No not found in database", "Not found", JOptionPane.INFORMATION_MESSAGE);
ClearForm();
btnUpdate.setEnabled(false);
}//if else closed
} catch (Exception x) {
JOptionPane.showMessageDialog(null, "Error on database operation,Updation failure", "Error", JOptionPane.ERROR_MESSAGE);
}//inner try catch closed
}//if closed
if (e.getActionCommand().equalsIgnoreCase("Cancel")) {
this.dispose();
}//if closed
}
private void ClearForm() {
txtAdNo.setText("");
txtName.setText("");
cmbSex.setSelectedIndex(0);
txtFatherName.setText("");
txtOccupation.setText("");
txtMotherName.setText("");
txtPhNo.setText("");
txtDOB.setText("");
txtAge.setText("");
txtCaste.setText("");
txtReligion.setText("");
txtHouseName.setText("");
txtCity.setText("");
txtDistrict.setText("");
txtState.setText("");
txtPin.setText("");
txtYear.setText("");
txtQualification.setText("");
}//clearform() closed
}//class closed
抛出的异常是:
java.lang.NullPointerException
at EditStdReg.actionPerformed(EditStdReg.java:197)
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:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
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:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
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)
答案 0 :(得分:1)
替换构造函数
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/anu?zeroDateTimeBehavior=convertToNull","root","root");
与
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/anu?zeroDateTimeBehavior=convertToNull","root","root");
<强>原因强>
您在构造函数中声明了一个单独的本地Connection对象,在构造函数作用域结束后,其他方法无法使用它。您已在实例级别拥有另一个Connection对象,该对象可用于包括构造函数在内的所有方法。用那个。
答案 1 :(得分:1)
<强>问题强>:
您已声明连接对象constructor
两次。
进入您的班级,第二次进入try-catch
ps=con.prepareStatement(sql);
区内。{/ p>
在构造函数中初始化的连接对象的范围仅限于该构造函数,因此当您尝试
时con
此NullPointerException
未初始化,因此臭名昭着try-catch
<强>解决方案强>
更改Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/anu?
zeroDateTimeBehavior=convertToNull","root","root");
阻止声明
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/anu?
zeroDateTimeBehavior=convertToNull","root","root");
到
con
这样您只能在Constructor
内初始化一个连接对象{{1}}。