Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

GUI练习:文件的打开保存

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class MyMenuDemo {

    private Frame f;
    private MenuBar mb;
    private TextArea ta;
    private Menu m;
    private MenuItem closeItem,openItem,saveItem;

    private FileDialog openDia,saveDia;

    private File file;

    public MyMenuDemo()
    {
        init();
    }

    public void init()
    {
        f=new Frame("my window");
        f.setBounds(200, 100, 500, 400);

        ta=new TextArea();
        mb=new MenuBar();
        m=new Menu("文件");
        //subMenu=new Menu("子菜单");
        openItem=new MenuItem("打开");
        saveItem=new MenuItem("保存");
        closeItem=new MenuItem("关闭");
        mb.add(m);
        m.add(openItem);
        m.add(saveItem);
        m.add(closeItem);
        //subMenu.add(subItem);

        f.add(ta);
        f.setMenuBar(mb);
        f.setVisible(true); 
        myEvent();
    }

    public void myEvent()
    {
        openItem.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                openDia.setVisible(true);
                String pathDir=openDia.getDirectory();
                String fileName=openDia.getFile();
                if(pathDir==null || fileName==null)
                    return ;

                file=new File(pathDir,fileName);

                try {
                    BufferedReader bufr=new BufferedReader(new FileReader(file));

                    ta.setText(null);

                    String line=null;
                    while((line=bufr.readLine())!=null)
                    {
                        ta.append(line+"\r\n");
                    }

                    bufr.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    throw new RuntimeException("读取失败");
                }
            }
        });

        saveItem.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                saveDia.setVisible(true);
                if(file==null)
                {

                String pathDir=saveDia.getDirectory();
                String fileName=saveDia.getFile();
                if(pathDir==null || fileName==null)
                    return ;
                file=new File(pathDir,fileName);                    
                }
                try {
                    BufferedWriter bufw=new BufferedWriter(new FileWriter(file));

                    String text=ta.getText();
                    bufw.write(text);
                    bufw.flush();
                    bufw.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }   
            }

        });

        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });

        closeItem.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        new MyMenuDemo();
    }
}

程序运行,但是点击打开保存条目时报错如下:
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at MyMenuDemo 1.actionPerformed(MyMenuDemo.java:54)atjava.awt.MenuItem.processActionEvent(UnknownSource)atjava.awt.MenuItem.processEvent(UnknownSource)atjava.awt.MenuComponent.dispatchEventImpl(UnknownSource)atjava.awt.MenuComponent.dispatchEvent(UnknownSource)atjava.awt.EventQueue.dispatchEventImpl(UnknownSource)atjava.awt.EventQueue.access 400(Unknown Source)
at java.awt.EventQueue 3.run(UnknownSource)atjava.awt.EventQueue 3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain 1.doIntersectionPrivilege(UnknownSource)atjava.security.ProtectionDomain 1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue 4.run(UnknownSource)atjava.awt.EventQueue 4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

查找54行openDia,未对其进行实例化,在第27、28行加入
openDia = new FileDialog(f);
saveDia = new FileDialog(f);

再次运行,程序正确,可以对其进行正确文件打开关闭以及保存操作。
1.点击打开:
这里写图片描述

2.选择1.txt文件打开:
这里写图片描述

这里写图片描述

3.修改文档内容如下:
这里写图片描述

4.点击保存之后,直接打开1.txt文件如下:
这里写图片描述

修改保存成功!!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值