当前位置: 代码迷 >> J2SE >> Jtree 数据库 出现java.lang.NullPointerException解决思路
  详细解决方案

Jtree 数据库 出现java.lang.NullPointerException解决思路

热度:500   发布时间:2016-04-24 13:33:05.0
Jtree 数据库 出现java.lang.NullPointerException
static 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
  相关解决方案