Reputation:
I am trying to run this code .This contains a table which has 2 rows and 4 column and the table is for entering the marks of the student but i am constantly getting this error of nullpointer exception on line 353. Plzzz help me here is the code
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
}
and the error is
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)
Upvotes: 0
Views: 478
Reputation: 1569
Is this the complete code? As far as I can see you declare jTable1
but you don't initialize it. So you have to prepare or create a jTable1
with
jTable1 = new JTable(data, columnNames);
where the constructors are
JTable(Object[][] rowData, Object[] columnNames)
JTable(Vector rowData, Vector columnNames)
further information here: https://docs.oracle.com/javase/tutorial/uiswing/components/table.html
So I think that you get a null pointer exception at:
course = jTable1.getValueAt(0, 0).toString();
because jTable1
is null
Upvotes: 1
Reputation: 14149
As far as I can see, jtable1
isn't initialised. Make sure it is. If the error persists, it is because one of the getValue()
calls in getData()
returns null
. You can use String.valueOf(jTable1.getValueAt(0,0))
instead of jTable1.getValueAt(0,0).toString()
to avoid NullPointerException
in that case.
Upvotes: 1