为了账号安全,请及时绑定邮箱和手机立即绑定

想实现对话框点叉键关闭当前窗口,可添加此功能后,程序编译不出了,是不是少了什么东西。

想实现对话框点叉键关闭当前窗口,可添加此功能后,程序编译不出了,是不是少了什么东西。

恩恩好吧 2016-06-06 22:14:56
控制台的显示Exception in thread "main" java.lang.NullPointerException at com.zyn_1.WriteNote.myEvent(WriteNote.java:121) at com.zyn_1.WriteNote.init(WriteNote.java:49) at com.zyn_1.WriteNote.<init>(WriteNote.java:19) at com.zyn_1.WriteNote.main(WriteNote.java:147)代码如下:package com.zyn_1; import java.awt.*;    import java.awt.event.*; import java.io.*;    import javax.management.RuntimeErrorException; public class WriteNote { private Frame f; private MenuBar mb; private Menu m1,m2; private MenuItem mi1,mi2,mi3,mi4,mi5,mi6; private FileDialog open,save; private TextArea ta; private File file; private Dialog d; private Label l; WriteNote(){ init(); } public void init(){ f = new Frame("记事本");//创建frame窗体 //对窗体进行基本设置 //f.setLayout(new FlowLayout());//设置布局 f.setLocation(300, 70);//位置 f.setSize(600,640); //大小 mb = new MenuBar(); m1 = new Menu("文件"); m2 = new Menu("帮助"); mi1 = new MenuItem("新建"); mi2 = new MenuItem("打开"); mi3 = new MenuItem("保存"); mi4 = new MenuItem("另存为"); mi5 = new MenuItem("退出"); mi6 = new MenuItem("版本"); mb.add(m1); mb.add(m2); m1.add(mi1); m1.add(mi2); m1.add(mi3); m1.add(mi4); m1.add(mi5); m2.add(mi6); open = new FileDialog(f,"打开",FileDialog.LOAD); save = new FileDialog(f,"保存",FileDialog.SAVE); myEvent(); ta = new TextArea(); d = new Dialog(f,"版本",false); d.setLocation(400, 250);//位置 d.setSize(300,170); //大小 d.setLayout(new FlowLayout()); l = new Label(); l.setText("制作的一个简单记事本"); d.add(l); f.add(ta); f.setMenuBar(mb); f.setVisible(true);//通过setvisiable(true)显示窗体 } public void myEvent(){ mi2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub open.setVisible(true); String dirpath = open.getDirectory(); String FileName = open.getFile(); if(dirpath == null || FileName == null) return; ta.setText(""); file =  new File(dirpath,FileName); try{ BufferedReader bu = new BufferedReader(new FileReader(file)); String line = null; while((line=bu.readLine())!=null){ ta.append(line+"\r\n"); } bu.close(); } catch(IOException x){ throw new RuntimeErrorException( null, "读取失败"); } } }); mi3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(file==null){ save.setVisible(true); String dirpath = save.getDirectory(); String FileName = save.getFile(); if(dirpath == null || FileName == null) return; file =  new File(dirpath,FileName); } try{ BufferedWriter bu = new BufferedWriter(new FileWriter(file)); String text = ta.getText(); bu.write(text); bu.close(); } catch(IOException x){ throw new RuntimeErrorException( null, "读取失败"); } } }); mi5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit(0); } }); d.addWindowListener(new WindowAdapter() { public  void windowClosing(WindowEvent e){ d.setVisible(false); } }); f.addWindowListener(new WindowAdapter() { public  void windowClosing(WindowEvent e){ System.exit(0); } }); mi6.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub d.setVisible(true); } }); } public static void main(String[] args) { // TODO Auto-generated method stub new WriteNote(); }}
查看完整描述

2 回答

?
yanrun

TA贡献317条经验 获得超240个赞

问题出在d的初始化,把

  d = new Dialog(f, "版本", false);
  d.setLocation(400, 250);//位置
  d.setSize(300, 170);    //大小
  d.setLayout(new FlowLayout());

从init方法移动到myEvent方法就可以解决,具体原因我也不是很清楚

查看完整回答
1 反对 回复 2016-06-07
?
hulukid

TA贡献190条经验 获得超40个赞


你可以加断点调试一下,nullPointer的错误很好找的

查看完整回答
1 反对 回复 2016-06-07
  • 2 回答
  • 0 关注
  • 1611 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信