J2SE论坛网»J2SE技术»newDialog为什么传递不了数据!SOS!

newDialog为什么传递不了数据!SOS!

问?:
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\wo>e:

E:\>cd editjava

E:\editjava>java ZSedit
java.lang.NullPointerException
at ZSedit.actionPerformed(ZSedit.java:279)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)
//请问大虾 这是报什么错!!
答!: 1:
空指针异常
有个对象没有初始化就用了
答!: 2:
我又查了一偏,还是没有发现没有 初始发的对象;

大虾能不能要明白点!要不要我把代码给你看看 这里提示太长 不让发!
答!: 3:
import java.awt.event.*;
import java.io.*;
//import java.lang.*;
import java.awt.datatransfer.*;
import java.awt.*;
//import javax.swing.*;


public class ZSedit extends Frame implements ActionListener
{
private TextArea ta2= new TextArea(4,15);
private TextArea ta = new TextArea();
private MenuBar mb = new MenuBar();
private Menu filemenu = new Menu("文件 F");
private MenuItem newfile = new MenuItem("新建");
private MenuItem openfile = new MenuItem("打开文件");
private MenuItem savefile = new MenuItem("保存");
private MenuItem saveAsfile = new MenuItem("另存为");
private MenuItem close = new MenuItem("退出");
private Menu editfile = new Menu("编辑 E");
private MenuItem selectItem = new MenuItem("全选");
private MenuItem copy = new MenuItem("复制");
private MenuItem cutItem = new MenuItem("剪切");
private MenuItem load = new MenuItem("粘贴");
private MenuItem farlong = new MenuItem("自动换行");
private Menu help = new Menu("帮助 F");
private MenuItem myhelp = new MenuItem("关于致胜");
private MenuItem zhuce = new MenuItem("注册");
private String fileName=null;
private Dialog aboutDialog,newDialog;
private Button aboutButton,newButton;
private Label lgLabel;
private TextField tf;


private Toolkit toolkit= Toolkit.getDefaultToolkit();
private Clipboard clipboard=toolkit.getSystemClipboard();

private FileDialog openFileDialog = new FileDialog(this,"打开文件",FileDialog.LOAD);
private FileDialog saveAsFileDialog = new FileDialog(this," 文件另存为",FileDialog.SAVE);
private FileDialog saveFileDialog = new FileDialog(this,"文件保存",FileDialog.SAVE);


public ZSedit()
{

Panel pl1,pl2,pl3;
setLayout(new BorderLayout());
pl1 = new Panel();
pl2 = new Panel();
pl3 = new Panel();
//pl3.setBackground(Color.cyan);
pl1.setLayout(new BorderLayout());
pl2.setLayout(new BorderLayout());
pl3.setLayout(new BorderLayout());


setTitle("ZS_Edit_Java: [ "+fileName+" ]");
setFont(new Font("宋体",Font.PLAIN,12));
ta.setFont(new Font("楷体",Font.PLAIN,15));
ta.setEditable(false);

//setSize(1024,768);
add(pl1,BorderLayout.WEST);
add(pl2,BorderLayout.CENTER);
add(pl3,BorderLayout.SOUTH);

setMenuBar(mb);
mb.add(filemenu);
mb.add(editfile);
mb.add(help);
filemenu.add(newfile);
filemenu.add(openfile);
filemenu.add(savefile);
filemenu.add(saveAsfile);
filemenu.addSeparator();
filemenu.add(close);
editfile.add(selectItem);
editfile.addSeparator();
editfile.add(copy);
editfile.add(cutItem);
editfile.add(load);
editfile.addSeparator();
editfile.add(farlong);
help.add(myhelp);
help.addSeparator();
help.add(zhuce);

lgLabel=new Label("【--欢迎使用致胜科技软件--】");
//lgLabel.setBackground(Color.cyan);
pl2.add("Center",ta);
pl1.add("Center",ta2);
pl3.add("West",lgLabel);
pack();


aboutDialog=new Dialog(this);
//aboutDialog.setBackground(Color.cyan);
Label textLabel1=new Label("***欢迎使用致胜科技提供的软件***");
Label textLabel2=new Label("***www.zs0769.com***");
Label textLabel3=new Label("联系电话:0769-22311403");
Label textLabel4=new Label("飞跃软件工作室制");
aboutButton = new Button("确定");
aboutDialog.setLayout(new FlowLayout());
aboutDialog.add(textLabel1);
aboutDialog.add(textLabel2);
aboutDialog.add(textLabel3);
aboutDialog.add(textLabel4);
aboutDialog.add(aboutButton);
aboutButton.addActionListener(this);
aboutDialog.setTitle("About_ZSEdit_Java");
aboutDialog.setSize(210,160);
aboutDialog.setResizable(false);
aboutDialog.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
aboutDialog.setVisible(false);
}
});

newDialog=new Dialog(this);
Label tl=new Label("文件名: ");
TextField tf= new TextField(15);
newButton= new Button("确定");
newButton.addActionListener(this);
newDialog.setLayout(new FlowLayout());
newDialog.add(tl);
newDialog.add(tf);
newDialog.add(newButton);
newDialog.setTitle("New_File_Dialog");
newDialog.pack();
newDialog.setResizable(false);
newDialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
newDialog.setVisible(false);
}
});




addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
//FileReader
// if(fileName.getFile()!=null){
saveFileDialog.show();
fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();
writeFile(fileName);
System.exit(0);
// }
// else
// System.exit(1);

}
});
newFile.addActionListener(this);

setLocationRelativeTo(null);
public void actionPerformad(ActionEvent e){
Object eventSource=e.getSource();
if(eventSource==newFile){
newDialog.setVisible(true);
}
else if(eventSource==newButton){
fileName=tf.getText();
File file= new File(fileName);
setTitle("ZSEdit_Java : ["+fileName+" ]");
tf.setText("");
newDialog.setVisible(false);
ta.setEditable(true);
}
}

public static void main(String [] args)
{
Frame fr = new ZSedit();

fr.show();
}
}
答!: 4:
//**请用这个.java
//**上边一个有问题

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

public class ZSedit extends Frame implements ActionListener
{
private TextArea ta2= new TextArea(4,15);
private TextArea ta = new TextArea();
private MenuBar mb = new MenuBar();
private Menu filemenu = new Menu("文件 F");
private MenuItem newfile = new MenuItem("新建");
private MenuItem openfile = new MenuItem("打开文件");
private MenuItem savefile = new MenuItem("保存");
private MenuItem saveAsfile = new MenuItem("另存为");
private MenuItem close = new MenuItem("退出");
private Menu editfile = new Menu("编辑 E");
private MenuItem selectItem = new MenuItem("全选");
private MenuItem copy = new MenuItem("复制");
private MenuItem cutItem = new MenuItem("剪切");
private MenuItem load = new MenuItem("粘贴");
private MenuItem farlong = new MenuItem("自动换行");
private Menu help = new Menu("帮助 F");
private MenuItem myhelp = new MenuItem("关于致胜");
private MenuItem zhuce = new MenuItem("注册");
private String fileName=null;
private Dialog aboutDialog,newDialog;
private Button aboutButton,newButton;
private Label lgLabel;
private TextField tf;


private Toolkit toolkit= Toolkit.getDefaultToolkit();
private Clipboard clipboard=toolkit.getSystemClipboard();

private FileDialog openFileDialog = new FileDialog(this,"打开文件",FileDialog.LOAD);
private FileDialog saveAsFileDialog = new FileDialog(this," 文件另存为",FileDialog.SAVE);
private FileDialog saveFileDialog = new FileDialog(this,"文件保存",FileDialog.SAVE);


public ZSedit()
{

Panel pl1,pl2,pl3;
setLayout(new BorderLayout());
pl1 = new Panel();
pl2 = new Panel();
pl3 = new Panel();
//pl3.setBackground(Color.cyan);
pl1.setLayout(new BorderLayout());
pl2.setLayout(new BorderLayout());
pl3.setLayout(new BorderLayout());


setTitle("ZS_Edit_Java: [ "+fileName+" ]");
setFont(new Font("宋体",Font.PLAIN,12));
ta.setFont(new Font("楷体",Font.PLAIN,15));
ta.setEditable(false);

//setSize(1024,768);
add(pl1,BorderLayout.WEST);
add(pl2,BorderLayout.CENTER);
add(pl3,BorderLayout.SOUTH);

setMenuBar(mb);
mb.add(filemenu);
mb.add(editfile);
mb.add(help);
filemenu.add(newfile);
filemenu.add(openfile);
filemenu.add(savefile);
filemenu.add(saveAsfile);
filemenu.addSeparator();
filemenu.add(close);
editfile.add(selectItem);
editfile.addSeparator();
editfile.add(copy);
editfile.add(cutItem);
editfile.add(load);
editfile.addSeparator();
editfile.add(farlong);
help.add(myhelp);
help.addSeparator();
help.add(zhuce);

lgLabel=new Label("【--欢迎使用致胜科技软件--】");
//lgLabel.setBackground(Color.cyan);
pl2.add("Center",ta);
pl1.add("Center",ta2);
pl3.add("West",lgLabel);
pack();


aboutDialog=new Dialog(this);
//aboutDialog.setBackground(Color.cyan);
Label textLabel1=new Label("***欢迎使用致胜科技提供的软件***");
Label textLabel2=new Label("***www.zs0769.com***");
Label textLabel3=new Label("联系电话:0769-22311403");
Label textLabel4=new Label("飞跃软件工作室制");
aboutButton = new Button("确定");
aboutDialog.setLayout(new FlowLayout());
aboutDialog.add(textLabel1);
aboutDialog.add(textLabel2);
aboutDialog.add(textLabel3);
aboutDialog.add(textLabel4);
aboutDialog.add(aboutButton);
//aboutButton.addActionListener(this);
aboutDialog.setTitle("About_ZSEdit_Java");
aboutDialog.setSize(210,160);
aboutDialog.setResizable(false);
aboutDialog.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
aboutDialog.setVisible(false);
}
});

newDialog=new Dialog(this);
Label tl=new Label("文件名: ");
TextField tf= new TextField(15);
newButton= new Button("确定");
newButton.addActionListener(this);
newDialog.setLayout(new FlowLayout());
newDialog.add(tl);
newDialog.add(tf);
newDialog.add(newButton);
newDialog.setTitle("New_File_Dialog");
newDialog.pack();
newDialog.setResizable(false);
newDialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
newDialog.setVisible(false);
}
});




addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
//FileReader
// if(fileName.getFile()!=null){
saveFileDialog.show();
fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();
// writeFile(fileName);
System.exit(0);
// }
// else
// System.exit(1);

}
});
newfile.addActionListener(this);

setLocationRelativeTo(null);
}

public void actionPerformed(ActionEvent e){
Object eventSource=e.getSource();
if(eventSource==newfile){
newDialog.setVisible(true);
}
else if(eventSource==newButton){
fileName=tf.getText();
File file= new File(fileName);
setTitle("ZSEdit_Java : ["+fileName+" ]");
tf.setText("");
newDialog.setVisible(false);
ta.setEditable(true);
}
}

public static void main(String [] args){

Frame fr = new ZSedit();
fr.show();
}
}

相关JAVA教程:
想利用MVC模式实现一个小程序,遇到了麻烦,高手帮忙看下。在线等
interface 和 Object 的问题
[说难不难的问题]:__FUNCTION__是c++中表示当前函数名的宏,java中又如何 获取当前类和方法 ?
String格式的问题
js 怎样获得td里的值?
关于嵌套类
如何覆盖的写文件?
checkbox 提交问题?
能否有-------非堵塞---------socket
大家好,小弟想学习JAVA,但却苦于不知从何学起,望大家给与指点迷精?3Q
关于java术语的中文名称
请问Java能编串口通信程序么?