hi guys
I have a problem whith my code. I am trying to make a JTable with some information of a database. but I have some problem with the Object[][] use for proceed to JTable creation.
i put my code :
excuze my english. I'm not so good
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import javax.swing.JTable; public class Connect { public static void main(String[] args) { try { //connection to my database Class.forName("org.postgresql.Driver"); System.out.println("Driver O.K."); String url = "jdbc:postgresql://localhost:5432/Ecole"; String user = "postgres"; String passwd = "*******"; Connection conn = DriverManager.getConnection(url, user, passwd); System.out.println("Connexion effective !"); Statement state = conn.createStatement(); ResultSet result = state.executeQuery("SELECT * FROM classe"); //we take MetaData ResultSetMetaData resultMeta = result.getMetaData(); //the variable of my JTable String title[] = {"classe", "prof principale"}; Object[][] data = null; int i = 0; while(result.next()){ // my table Class has 2 column so I write "j<2" for(int j=0;j<2;j++){ // to put a line on my data data[i][j]=result.getString("cls_nom"); j++; data[i][j]=result.getString("cls_id"); } // to change a line i++; } result.close(); state.close(); // I try here to create a JTable but it doesn't work // the compiler write (java.lang.NullPointerException) JTable tableau = new JTable( data,title); } catch (Exception e) { e.printStackTrace(); } } }
error:java.lang.NullPointerException
at Connect.main(Connect.java:41)
thank you for your help