public DefaultTableModel showTable(DefaultTableModel tmodel, String sql){ DefaultTableModel tm = tmodel; String sq = sql; try { st=con.createStatement(); rs=st.executeQuery(sq); System.out.println("Query: "+sq); ResultSetMetaData rmeta = (ResultSetMetaData)rs.getMetaData(); int numColumns = rmeta.getColumnCount(); System.out.println("Column Count: "+numColumns); for(int i=1;i<=numColumns;i++){ if(i<=numColumns){ System.out.println(rmeta.getColumnName(i)); tm.addColumn(rmeta.getColumnName(i)); } } } catch (SQLException ex) { Logger.getLogger(JDBCCrud.class.getName()).log(Level.SEVERE, null, ex); } return tm; }//SHOWTABLE
when i add: tm.addColumn(rmeta.getColumnName(i));
the program will show errors which says that it was a NULL POINTER and lead me to this code.
im getting the name of column to the database. and i used this code: System.out.println(rmeta.getColumnName(i)); to check if java gets the data from sqlserver database.