我正在尝试运行此代码。它包含一个具有2行4列的表,该表用于输入学生的成绩,但是我不断在353行收到此nullpointer异常的错误。Plzzz帮助我
这是代码
package uclidit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class EdittedForm extends javax.swing.JFrame {
String course,yearf,yeart, aggr;
public EdittedForm() {
initComponents();
}
@SuppressWarnings("unchecked")
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
getData();
try{
String url="jdbc:mysql://localhost:3306/entryform";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url,"root","root");
String query="insert into
marks(course,yearf,yeart,sem1,sem2,sem3,sem4,sem5,sem6,sem7,sem8,aggregate)
values(?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1,course);
ps.setString(2,yearf);
ps.setString(3,yeart);
ps.setFloat(4,0);
ps.setFloat(5,0);
ps.setFloat(6,0);
ps.setFloat(7,0);
ps.setFloat(8, 0);
ps.setFloat(9,0);
ps.setFloat(10,0);
ps.setFloat(11,0);
ps.setString(12,aggr);
ps.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
}
public void getData()
{
course = jTable1.getValueAt(0,0).toString();
JOptionPane.showMessageDialog(this, course);
yearf = jTable1.getValueAt(0,1).toString();
JOptionPane.showMessageDialog(this,yearf );
yeart=jTable1.getValueAt(0,2).toString();
JOptionPane.showMessageDialog(this, yeart);
aggr = jTable1.getValueAt(0,3).toString();
JOptionPane.showMessageDialog(this, aggr);
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EdittedForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JScrollPane jScrollPane5;
private javax.swing.JScrollPane jScrollPane6;
private javax.swing.JScrollPane jScrollPane7;
private javax.swing.JScrollPane jScrollPane8;
private javax.swing.JScrollPane jScrollPane9;
private javax.swing.JTable jTable1;
private javax.swing.JTable jTable2;
private javax.swing.JTable jTable3;
private javax.swing.JTable jTable4;
private javax.swing.JTable jTable5;
private javax.swing.JTable jTable6;
private javax.swing.JTable jTable7;
private javax.swing.JTable jTable8;
private javax.swing.JTable jTable9;
// End of variables declaration
}
错误是
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at uclidit.EdittedForm.getData(EdittedForm.java:353)
at uclidit.EdittedForm.jButton1ActionPerformed(EdittedForm.java:316)
at uclidit.EdittedForm.access$000(EdittedForm.java:17)
at uclidit.EdittedForm$7.actionPerformed(EdittedForm.java:251)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
最佳答案
据我所知,jtable1
尚未初始化。确保是。如果错误仍然存在,那是因为getValue()
中的getData()
调用之一返回了null
。在这种情况下,可以使用String.valueOf(jTable1.getValueAt(0,0))
代替jTable1.getValueAt(0,0).toString()
来避免NullPointerException
。