Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TableTestM extends JFrame {
JTable jTable1;
JScrollPane jScrollPane1;
public TableTestM() {
jTable1 = new JTable(new MyTableModel(new ContSQL()));
jScrollPane1 = new JScrollPane(jTable1);
this.add(jScrollPane1);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
new TableTestM();
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class MyTableModel implements TableModel {
private ContSQL contSql = null;
private static ResultSet rs = null;
private static Statement stmt = null;
private static Connection conn = null;
private int rowNum = 0;
private int colNum = 0;
public MyTableModel(ContSQL contSql) {
this.contSql = contSql;
try {
stmt = contSql.getStmt();
rs = stmt.executeQuery("select * from log_tab");
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addTableModelListener(TableModelListener arg0) {
// TODO Auto-generated method stub
}
public Class<?> getColumnClass(int arg0) {
return null;
}
public int getColumnCount() {
ResultSetMetaData rsmd;
try {
rsmd = rs.getMetaData();
colNum = rsmd.getColumnCount()-1;
} catch (SQLException e) {
e.printStackTrace();
}
return colNum;
}
public String getColumnName(int i) {
ResultSetMetaData rsmd;
try {
rsmd = rs.getMetaData();
return rsmd.getColumnName(i + 1);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public int getRowCount() {
try {
rs.last();
rowNum=rs.getRow()-1;
rs.beforeFirst();
return rowNum;
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public Object getValueAt(int row, int col) {
try {
if(row==0&&col==0){
rs.next();
}
if(row<rowNum&&col+1==colNum){
rs.next();
}
return rs.getString(col+1);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean isCellEditable(int arg0, int arg1) {
return false;
}
public void removeTableModelListener(TableModelListener arg0) {
// TODO Auto-generated method stub
}
public void setValueAt(Object arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
// public static void main(String[] args){
// MyTableModel ta = new MyTableModel(new ContSQL());
// System.out.println(ta.getRowCount()+"=====");
// System.out.println(ta.getColumnCount()+"=====");
//
// for(int i=0;i<=ta.rowNum;i++){
// for(int j=0;j<=ta.colNum;j++){
// System.out.println(ta.getValueAt(i, j)+"wocao");
// }
// }
//
// }
}
import java.sql.*;
public class ContSQL {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public ContSQL() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager
.getConnection("jdbc:mysql://localhost/fmm?user=root&password=521");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// finally {
// if (conn != null) {
// try {
// conn.close();
// } catch (SQLException e) {
// }
// }
// if (conn != null) {
// try {
// conn.close();
// } catch (SQLException e) {
// }
// }
// if (conn != null) {
// try {
// conn.close();
// } catch (SQLException e) {
// }
// }
//
// }
}
/*public boolean isValidate(String username, String passwd) {
String sql = "select * from log_tab where username='" + username
+ "' and passwd='" + passwd + "'";
try {
rs = stmt.executeQuery(sql);
if (rs.next()) {
return true;
}
} catch (SQLException e) {
return false;
}
return false;
}*/
public Statement getStmt() throws SQLException {
return conn.createStatement();
}
}
create database fmm;
use fmm;
create table log_tab(
id int primary key auto_increment,
userName varchar(20) not null,
passwd varchar(9) not null
);
insert into log_tab(userName,passwd) values('zhangsan','wowowo');
insert into log_tab(userName,passwd) values('lisi','wowowo');
insert into log_tab(userName,passwd) values('wangwu','wowowo');
commit;
运行界面TableTestM时
总是报Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException异常
各位大虾帮忙解决下吧 谢谢了