代码如下:
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.io.*;
import javax.swing.filechooser.*;
public class test
{
public test()
{
init();
}
public void init()
{
JFrame f=new JFrame("资源管理器");
f.setSize(400,300);
f.setLocationRelativeTo(null);
DefaultMutableTreeNode root=new DefaultMutableTreeNode("计算机");
File roots[]=File.listRoots();
for(int i=0;i<roots.length;i++)
{
DefaultMutableTreeNode node=new DefaultMutableTreeNode(roots[i]);
root.add(node);
explored(roots[i],node);
}
JTree tree=new JTree(root);
JScrollPane sp=new JScrollPane(tree);
f.add(sp);
f.setVisible(true);
}
public void explored(File file,DefaultMutableTreeNode node)
{
if(file.isDirectory())
{
File children[]=file.listFiles();
for(int i=0;i<children.length;i++)
{
if(children[i].isDirectory())
{
DefaultMutableTreeNode _node=new DefaultMutableTreeNode(children[i].getName());
node.add(_node); explored(children[i],_node);
}
}
}
}
public static void main(String args[])
{ new test(); }
}
控制台总是 提示 Exception in thread "main" java.lang.NullPointerException 请问为什么,哪错了
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.io.*;
import javax.swing.filechooser.*;
public class test
{
public test()
{
init();
}
public void init()
{
JFrame f=new JFrame("资源管理器");
f.setSize(400,300);
f.setLocationRelativeTo(null);
DefaultMutableTreeNode root=new DefaultMutableTreeNode("计算机");
File roots[]=File.listRoots();
for(int i=0;i<roots.length;i++)
{
DefaultMutableTreeNode node=new DefaultMutableTreeNode(roots[i]);
root.add(node);
explored(roots[i],node);
}
JTree tree=new JTree(root);
JScrollPane sp=new JScrollPane(tree);
f.add(sp);
f.setVisible(true);
}
public void explored(File file,DefaultMutableTreeNode node)
{
if(file.isDirectory())
{
File children[]=file.listFiles();
for(int i=0;i<children.length;i++)
{
if(children[i].isDirectory())
{
DefaultMutableTreeNode _node=new DefaultMutableTreeNode(children[i].getName());
node.add(_node); explored(children[i],_node);
}
}
}
}
public static void main(String args[])
{ new test(); }
}
控制台总是 提示 Exception in thread "main" java.lang.NullPointerException 请问为什么,哪错了
解决方案 »
- JTable控件点击列头自动排序出异常
- 怎样在JPanel显示部分图片
- java里的构造函数里有初始化列表吗
- jdbc连sqlserver2000总提示错误,我已导入了那三个jar包?
- 我在一个jtable里更改表的数据可是最后一个单元格更改完就点击保存按钮的问题
- 数据库相关
- 在unix中怎么布JDK和JDBC呀,怎么在其中写代码可以读SQL server中的表,请大家帮忙一下
- 大家看看这个程序为什么会运行出错?(书上的例题)
- 怎样用java连接sqlserver数据库,不用jdbc-odbc网桥?
- java sftp通道建立连接时报错,在channel.connect()时报("session is down");
- 关于大鱼吃小鱼 大牛们进~~
- 进行语法解析时已达到文件结尾问题,在线等~~
for(int i=0;i<children.length;i++)
这里得到的children可能为null的
{
if(file.isDirectory()){
File children[]=file.listFiles();
if (children == null){
System.out.println("children is null.") ;
return ;
}
for(int i=0;i<children.length;i++){
if(children[i].isDirectory()){
DefaultMutableTreeNode _node=new DefaultMutableTreeNode(children[i].getName());
node.add(_node); explored(children[i],_node);
}
}
}
}