Jtree 数据库 出现
java.lang.NullPointerExceptionstatic public DefaultMutableTreeNode root = new DefaultMutableTreeNode("资产");
private JTree getJTree() {
if (jTree == null) {
DefaultMutableTreeNode [] Node = null;
db.dbconn();
int i = 1;
String sql = "select * from channel where class=1";
try{
rs = db.stmt.executeQuery(sql);
while(rs.next()){
i++;
Node[i] = new DefaultMutableTreeNode(rs.getString("channel"));
root.add(Node[i]);
}
rs.close();
} catch(Exception e){e.printStackTrace();}
db.closedb();
jTree = new JTree(root);
jTree.setEditable(true);
treeModel = (DefaultTreeModel) jTree.getModel();
}
return jTree;
}
运行出现
java.lang.
NullPointerException at k.Property.getJTree(Property.java:139)
错误行 Node[i] = new DefaultMutableTreeNode(rs.getString("channel"));
------解决方案--------------------DefaultMutableTreeNode [] Node = null; 这句话不报NULL指针才怪,你可以用List