我正在尝试运行此代码。这包含一个包含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)
答案 0 :(得分:1)
据我所见,jtable1
尚未初始化。确保它是。如果错误仍然存在,那是因为getValue()
中的getData()
次调用之一返回null
。在这种情况下,您可以使用String.valueOf(jTable1.getValueAt(0,0))
代替jTable1.getValueAt(0,0).toString()
来避免NullPointerException
。
答案 1 :(得分:1)
这是完整的代码吗?据我所知,你宣布jTable1
,但你没有初始化它。因此,您必须使用
jTable1
jTable1 = new JTable(data, columnNames);
构造函数在哪里
JTable(Object[][] rowData, Object[] columnNames)
JTable(Vector rowData, Vector columnNames)
此处提供更多信息:https://docs.oracle.com/javase/tutorial/uiswing/components/table.html
所以我认为你在:
得到一个空指针异常course = jTable1.getValueAt(0, 0).toString();
因为jTable1
为空