| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1062 人关注过本帖
标题:急!! (在线等) 我做了一个文件模拟 但是创建目录时总出错 高手帮忙看看 ...
只看楼主 加入收藏
shuiyouhan
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2006-1-12
收藏
 问题点数:0 回复次数:4 
急!! (在线等) 我做了一个文件模拟 但是创建目录时总出错 高手帮忙看看那
我做了一个文件模拟 但是创建目录时总出错 高手帮忙看看那

运行后右键单击 “ROOTES”,创建目录时总会报以下错误错

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at file.FileGraphicsUserIndex$1.actionPerformed(FileGraphicsUserIndex.java:90)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
        at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1216)
        at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1257)
        at java.(Component.java:6038)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.(Component.java:5803)
        at java.awt.Container.processEvent(Container.java:2058)
        at java.(Component.java:4410)
        at java.awt.Container.dispatchEventImpl(Container.java:2116)
        at java.(Component.java:4240)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
        at java.awt.Container.dispatchEventImpl(Container.java:2102)
        at java.awt.Window.dispatchEventImpl(Window.java:2429)
        at java.(Component.java:4240)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)



代码如下:


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
//import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import
import
import
import
import

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class FileGraphicsUserIndex extends JPanel {

    /**
     *
     */
    private static final long serialVersionUID = -4696988113883085233L;
    private VirtualDisk disk;
    private JTextField field;
    private JTree fileTree;
    public FileGraphicsUserIndex(){
        setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(),"文件管理"));
        setLayout(new BorderLayout());
        VirtualDisk disk=getVirtualDiskFromTxt();
        
        add(disk,BorderLayout.SOUTH);
        //用户接口
        JPanel commandPanel=new JPanel();
        JLabel label=new JLabel("用户命令接口:");
        commandPanel.add(label);
        //label.setFont(new Font("宋体",Font.ITALIC,12));
        commandPanel.add(field=new JTextField(12));
        //field.setBackground(new Color(0x6699ff));
        field.setForeground(Color.white);
        add(commandPanel,BorderLayout.NORTH);
        //目录树
        fileTree=disk.getFileDirectoryTree();
        //fileTree.setBackground(new Color(0x6699ff));
        fileTree.setComponentPopupMenu(getPopupMenu());
        JScrollPane treePane=new JScrollPane(fileTree);
        add(treePane,BorderLayout.CENTER);
        treePane.setPreferredSize(new Dimension(60,0));
        treePane.setBorder(BorderFactory.createTitledBorder("文件系统"));
    }
    
    private VirtualDisk getVirtualDiskFromTxt(){
        //return new VirtualDisk();
        ObjectInputStream in;
        try {
            in = new ObjectInputStream(
                    new FileInputStream(new File("src/disk.txt")));
            Object object=in.readObject();
            disk=(VirtualDisk)object;
            return disk;
        } catch (FileNotFoundException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    private JPopupMenu getPopupMenu(){
        JPopupMenu menu=new JPopupMenu();
        JMenu newMenu=new JMenu("新建");
        menu.add(newMenu);
        JMenuItem newDirectory=new JMenuItem("目录文件");
        newDirectory.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                DefaultMutableTreeNode parent=(DefaultMutableTreeNode)
                    fileTree.getSelectionPath().getLastPathComponent();
                VirtualFile parentFile=(VirtualFile)parent.getUserObject();
                //加一个输入文件名的对话框
                String childFileName=JOptionPane.showInputDialog( "Please enter the file name:\n"+
                        "注意:文件名长度不大于6");
                //String childFileName=getUsableFileName(parent);
                VirtualFile childFile=new VirtualFile(childFileName,VirtualFile.EXTENSION_FILE);
                DefaultMutableTreeNode child=new DefaultMutableTreeNode(childFile);
                disk.createFile(parentFile, childFile);
                disk.getTreeModel().insertNodeInto(child, parent,parent.getChildCount());
            }
        });
        newMenu.add(newDirectory);
        JMenuItem newCharFile=new JMenuItem("字符文件");
        newCharFile.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                DefaultMutableTreeNode parent=(DefaultMutableTreeNode)
                fileTree.getSelectionPath().getLastPathComponent();
                VirtualFile parentFile=(VirtualFile)parent.getUserObject();
                
                String childFileName=JOptionPane.showInputDialog( "Please enter the file name:\n"+
                "注意:文件名长度不大于6");
                
                VirtualFile childFile=new VirtualFile(childFileName,VirtualFile.EXTENSION_CHAR_FILE);
                DefaultMutableTreeNode child=new DefaultMutableTreeNode(childFile);
                disk.createFile(parentFile, childFile);
                disk.getTreeModel().insertNodeInto(child, parent,parent.getChildCount());
            }
        });
        newMenu.add(newCharFile);
               
        JMenuItem newExeFile=new JMenuItem("可执行文件");
        newExeFile.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                DefaultMutableTreeNode parent=(DefaultMutableTreeNode)
                fileTree.getSelectionPath().getLastPathComponent();
                VirtualFile parentFile=(VirtualFile)parent.getUserObject();
                
                String childFileName=JOptionPane.showInputDialog( "Please enter the file name:\n"+
                "注意:文件名长度不大于6");
                //String childFileName=getUsableFileName(parent);
                VirtualFile childFile=new VirtualFile(childFileName,VirtualFile.EXTENSION_EXECUTE_FILE);
                DefaultMutableTreeNode child=new DefaultMutableTreeNode(childFile);
                disk.createFile(parentFile, childFile);
                disk.getTreeModel().insertNodeInto(child, parent,parent.getChildCount());
            }
        });
        newMenu.add(newExeFile);
               
        JMenuItem del=new JMenuItem("删除");
        del.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                DefaultMutableTreeNode choseNode=(DefaultMutableTreeNode)
                fileTree.getSelectionPath().getLastPathComponent();
                DefaultMutableTreeNode parent=(DefaultMutableTreeNode)
                fileTree.getSelectionPath().getParentPath().getLastPathComponent();
                disk.deleteFile(parent,choseNode);
            }
        });
        menu.add(del);
               
        JMenuItem edit=new JMenuItem("编辑");
        edit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                DefaultMutableTreeNode choseNode=(DefaultMutableTreeNode)
                fileTree.getSelectionPath().getLastPathComponent();
                new EditFileDialogFrame(choseNode);
            }
        });
        menu.add(edit);
               
  ///////////////////////////////////////////////////////////////////////////////////////////              
               
                             
               /* JMenuItem copy=new JMenuItem("复制");
        copy.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                // TODO Auto-generated method stub
                //disk.format();
            }
        });
        menu.add(copy);
                */
   ///////////////////////////////////////////////////////////////////////////////////////            
               
               
               
               
               
        JMenuItem execute=new JMenuItem("执行");
        execute.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                DefaultMutableTreeNode choseNode=(DefaultMutableTreeNode)
                    fileTree.getSelectionPath().getLastPathComponent();
                VirtualFile file=(VirtualFile)choseNode.getUserObject();
                String name=file.getFileName();
                //name=name+"@"+(int)(Math.random()*1000);
                String s=file.getFileContent();
//                String program="";
//                for(int i=0;i<s.length();i++){
//                    if((i+2)%5==0)i++;
//                    else program=program+String.valueOf(s.charAt(i));
//                }
                System.out.println(s);
                try{//开始执行
                            //CourseControl.create(s);
                    //添加代码
                }catch(Exception e){
                    String message="创建进程失败原因可能是\n因为申请不到PCB或内存!";
                    JOptionPane.showMessageDialog(null,message);
                }
            }
        });
        menu.add(execute);
               
        JMenuItem newFormat=new JMenuItem("格式化");
        newFormat.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                // TODO Auto-generated method stub
                //disk.format();
            }
        });
        menu.add(newFormat);
   
        return menu;
    }
    
    public String getUsableFileName(DefaultMutableTreeNode parent){
        String[] name=new String[100];
        for(int i=0;i<100;i++){
            if(i<10)name[i]="NFile"+i;
            else name[i]="File"+i;
        }
        int count=parent.getChildCount();
        boolean flag=false;
        for(int j=0;j<100;j++){
            for(int i=0;i<count;i++){
                DefaultMutableTreeNode child=(DefaultMutableTreeNode)parent.getChildAt(i);
                VirtualFile file=(VirtualFile)child.getUserObject();
                if(file.getFileName().equals(name[j])){
                    flag=true;
                    break;
                }
            }
            if(flag==false)
                return name[j];
            else flag=false;
        }
        return null;
    }
    private class EditFileDialogFrame extends JFrame{
        private static final long serialVersionUID = 1L;
        private JTextField name,extension;
        private JButton cancel,record;
        private JTextArea txt;
        public EditFileDialogFrame(DefaultMutableTreeNode choseNode){
            final VirtualFile file=(VirtualFile)choseNode.getUserObject();
            setTitle("编辑文件界面");
            setBounds(100,100,300,350);
            JPanel nPanel=new JPanel();
            nPanel.setBorder(BorderFactory.createEtchedBorder());
            add(nPanel,BorderLayout.NORTH);
            nPanel.add(new JLabel("文件名"));
            nPanel.add(name=new JTextField(5));
            name.setText(file.getFileName());
            nPanel.add(new JLabel("扩展名"));
            nPanel.add(extension=new JTextField(5));
            extension.setText(file.getFileExtension());
            extension.setEditable(false);
            JPanel cPanel=new JPanel();
            add(cPanel,BorderLayout.CENTER);
            cPanel.add(new JScrollPane(txt=new JTextArea(10,20)));
            txt.setText(file.getFileContent());
            cPanel.setBorder(BorderFactory.createEtchedBorder());
            JPanel sPanel=new JPanel();
            sPanel.setBorder(BorderFactory.createEtchedBorder());
            add(sPanel,BorderLayout.SOUTH);
            sPanel.add(cancel=new JButton("取消"));
            cancel.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    setVisible(false);
                }
            });
            sPanel.add(record=new JButton("保存"));
            record.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    file.setContent(txt.getText());
                    file.setFileName(name.getText());
                    file.setFileLength(txt.getText().length()+8);
                    disk.changeFileContent(file, txt.getText(), name.getText());
                    setVisible(false);
                }
            });
            setVisible(true);
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame=new JFrame();
        frame.add(new FileGraphicsUserIndex());
        frame.setSize(300,500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
搜索更多相关主题的帖子: 目录 文件 模拟 
2008-01-09 12:27
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
空指针异常,最好排的异常了,你仔细检查一下有没有用到什么没有初始化的变量

可惜不是你,陪我到最后
2008-01-09 13:18
shuiyouhan
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2006-1-12
收藏
得分:0 
我查查阿

2008-01-09 15:03
shuiyouhan
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2006-1-12
收藏
得分:0 
没有阿
我全查了一遍
都实例化了

老大,帮忙查查,多些多谢

2008-01-09 15:29
qsrock
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-12-29
收藏
得分:0 
VirtualDisk,VirtualFile是什么类阿,都没有叫人怎么帮你查阿
2008-01-14 14:15
快速回复:急!! (在线等) 我做了一个文件模拟 但是创建目录时总出错 高手帮忙 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027879 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved