-2

I am new to java. I am trying to write a code for search which should retrieve that particular searched data from database(MS Access) and display it in jTable. But its giving me a null pointer exception. the code is as given below:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class GuestDetail extends javax.swing.JFrame {
      PreparedStatement ps;
      ResultSet rs;
      Connection conn;
      Statement stmt;
      TableModel tm;
      int sr;
/**
 * Creates new form GuestDetail
 */
public GuestDetail() {
    initComponents();
    try{
    conn = HotelManagement.ConnectDb();
    tm=new DefaultTableModel(4, 4);
    jTable1.setModel(tm);
    }catch(Exception ex)
    {
        JOptionPane.showMessageDialog(null, ex);
    }
}


 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)         {                                         

  // TODO add your handling code here:
     try{

       System.out.println("Searching Records..");
           String fn=jTextField1.getText();

        System.out.println("searching name="+fn);

      stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

     //  String query1="SELECT * FROM GuestDetails WHERE FirstName LIKE'fn%'";

      String query="select FirstName,LastName,City,ContactNo from GuestDetails where FirstName= ' "+jTextField1.getText()+" '";

      System.out.println("query:"+query);
              rs.next();
              int rows=rs.getInt(1);
              ResultSet rs= stmt.executeQuery(query);
              Object[][] data=new Object[rows][4];
              Object[] headers={"FirstName","LastName","City","ContactNo"};

              int i=0;
              while(rs.next())
              {
                 int j=0;
                 data[i][j++]=rs.getString(1);
                 data[i][j++]=rs.getString(2);
                 data[i][j++]=rs.getString(3);
                 data[i++][j]=rs.getLong(4);
              }
              DefaultTableModel tm=new DefaultTableModel(data, headers);
             jTable1.setModel(tm);    






   }catch (Exception ex) {
         Logger.getLogger(GuestDetail.class.getName()).log(Level.SEVERE, null, ex);
        ex.printStackTrace();

   }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
pallavi
  • 133
  • 1
  • 3
  • 11
  • 1
    can you please post exception stack trace ? – DeepInJava Oct 18 '14 at 14:11
  • _the code is as given below_ The code you given us won't even compile. Apart from that, just saying you have a `NullPointerException` is not really helpful. We need to know at which line in the code you post that the `NullPointerException` happened. But if you figured out where the `NullPointerException` occurs, you can probably fix it as well. Also see [this question](http://stackoverflow.com/q/218384/1076463) – Robin Oct 18 '14 at 15:28

2 Answers2

2

You seem to be assuming that the first row and column of the ResultSet is the row count. Instead, create an empty DefaultTableModel and use the addRow() method for each row that you encounter in the ResultSet.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

There's no way you should be going to the database from the EDT. I'm assuming that, since all that activity is in an event handler, it's going to be called as a result of a button click and that it'll be called by the EDT.

You need to put all of the data retrieval and the construction of the "data" array into a Swingworker runnable, and then put the data model creation and installation into the table into the "done" method of the Swingworker.

DaveB
  • 1,836
  • 1
  • 15
  • 13