我每次运行的时候都不行,写文件写不进去
而且要报这样的错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Action1.actionPerformed(ex3.java:114)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1957)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2280)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:377)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:232)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5957)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3280)
at java.awt.Component.processEvent(Component.java:5722)
at java.awt.Container.processEvent(Container.java:1960)
at java.awt.Component.dispatchEventImpl(Component.java:4365)
at java.awt.Container.dispatchEventImpl(Container.java:2018)
at java.awt.Component.dispatchEvent(Component.java:4195)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4222)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3886)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3816)
at java.awt.Container.dispatchEventImpl(Container.java:2004)
at java.awt.Window.dispatchEventImpl(Window.java:2300)
at java.awt.Component.dispatchEvent(Component.java:4195)
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 javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;public class ex3 {
public static void main(String args[]){
stu ss = new stu("学生信息输入表",400,400,800,1000);
ss.showmain(ss);
}
} class stu extends JFrame{//GUI的组件

JTextField F1, F2, F3, F4, F5,F6;//定义变量
String jiguan;
JSpinner s1;
JRadioButton r1, r2;
String sSex = "";

public stu (String s, int x, int y, int w, int h){//构造函数
super(s);
setBounds(x,y,w,h);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setResizable(false);//设置为false可以不让用户拉伸
}


public void showmain(stu ss){ //GUI组件添加
Container contentPane = ss.getContentPane();
JPanel p1 = new JPanel(new GridLayout(7,2));
JPanel p2 = new JPanel(new GridLayout(1,2));

JButton b1 = new JButton("确定");
    JButton b2 = new JButton("取消");
    
JLabel L1 = new JLabel("姓名:",JLabel.CENTER);
JTextField F1 = new JTextField();

JLabel L2 = new JLabel("学号:",JLabel.CENTER);
JTextField F2 = new JTextField();

JLabel L3 = new JLabel("年龄:",JLabel.CENTER);
SpinnerNumberModel sm = new SpinnerNumberModel(20,10,30,1);
JSpinner s1 = new JSpinner(sm);

JLabel L4 = new JLabel("性别:",JLabel.CENTER);
JRadioButton r1 = new JRadioButton("男");
JRadioButton r2 = new JRadioButton("女");
ButtonGroup bg=new ButtonGroup();
    bg.add(r1);
    bg.add(r2);

    JLabel L5 = new JLabel("籍贯:",JLabel.CENTER);
String data[]={"四川","重庆","湖南","湖北","安徽","云南","贵州","吉林","辽宁",
"河南","河北","北京","上海","广东"};
JComboBox combo = new JComboBox(data);
        combo.setEditable(true);
        ComboBoxEditor editor = combo.getEditor();
        combo.configureEditor(editor,"可选择输入");
           
        JLabel L6 = new JLabel("备注",JLabel.CENTER);
    JTextField F6 = new JTextField();  
        
        
        p1.add(L1);
        p1.add(F1);
        p1.add(L2);
        p1.add(F2);
        p1.add(L3);
        p1.add(s1);
        p1.add(L4);
        p2.add(r1);
        p2.add(r2);
        p1.add(p2);//把p2添加到p1上去
        p1.add(L5);
        p1.add(combo);
        p1.add(L6);
        p1.add(F6);
        p1.add(b1);
        p1.add(b2);
        
        b1.addActionListener(new Action1(this));//对按钮进行监听,对输入信息的确定与取消的监听
    b2.addActionListener(new Action1(this));
    r1.addItemListener(new Action2(this));//对于性别单选按钮的监听
    r2.addItemListener(new Action2(this));
    contentPane.add(p1,BorderLayout.CENTER);
    pack();//排版
    jiguan=combo.getSelectedItem().toString();
    

}class Action1 implements ActionListener{
stu sf1 = null;
public Action1(stu sf1){
this.sf1 = sf1;
} public void actionPerformed(ActionEvent e){//确定,取消按钮被单击时触发
String cmd = e.getActionCommand();
    if(cmd.equals("确定")){
    String filename = "F:\\stuInfo.txt";//保存的路径以及文件名
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(filename));
            out.write("学生姓名: "+ sf1.F1.getText());
            out.newLine();//换行
            out.write("学生学号: "+ sf1.F2.getText());
            out.newLine();
            out.write("学生年龄: "+sf1.s1.getValue());
            out.newLine();
            out.write("学生性别: "+sf1.sSex);
            out.newLine();
            out.write("学生籍贯: "+sf1.jiguan);
            out.newLine();
            out.write("备注: "+ sf1.F6.getText());
            out.newLine();
            out.close();
        }catch (IOException iox)
            {
              System.out.println("写入文件有错误!"+filename);
         }
        
        String filename2 ="F:\\stuInfo.txt";
            String line;
            try{
              System.out.println("你输入的信息如下:");
              BufferedReader in =new BufferedReader(new FileReader(filename2));
              line=in.readLine();
              while (line !=null)
              {
                System.out.println(line);
                line=in.readLine();
              }
              in.close();
            }
            catch (IOException iox)
            {
              System.out.println("读文件有错误!"+filename2);
            }
     }
    if(cmd.equals("取消")){
     System.exit(0);
     }
    }
}class Action2 implements ItemListener{
stu sf2 = null;
String sex = "";    
public Action2(stu sf2){
this.sf2 = sf2;
}

public void itemStateChanged(ItemEvent e){//单选按钮被单击时触发        if(e.getSource()==sf2.r1) sex="男";
       if(e.getSource()==sf2.r2) sex="女";
       sf2.sSex = sex;
     }
}

解决方案 »

  1.   

    JTextField F1, F2, F3, F4, F5,F6;//定义变量 
    ---------
    在哪new的?
      

  2.   

    JTextField F1 = new JTextField();
    ------------
    这个可是局部变量,不是你的类成员
      

  3.   

    好长 String filename2 ="F:\\stuInfo.txt";
                String line;
                try{
                  System.out.println("你输入的信息如下:");
                  BufferedReader in =new BufferedReader(new FileReader(filename2));
                  line=in.readLine();
                  while (line !=null)
                  {
                    System.out.println(line);
                    line=in.readLine();
                  }
                  in.close(); line 好像没有初始化把
      

  4.   

    楼主,你的问题(空指针错误)已帮你解决,代码贴在下面,自己看吧import javax.swing.*;
    import javax.swing.text.JTextComponent;import java.awt.*;
    import java.awt.event.*;
    import java.io.*;public class ex3 {
    public static void main(String args[]) {
    stu ss = new stu("学生信息输入表", 400, 400, 800, 1000);
    ss.showmain(ss);
    }
    }class stu extends JFrame {// GUI的组件 // JTextField F1, F2, F3, F4, F5, F6;// 定义变量
    JTextField F1 = new JTextField(); JTextField F2 = new JTextField();; JTextField F6 = new JTextField();//LZ最好把定义的变量初始化为全局的.省得出调用问题. String jiguan; SpinnerNumberModel sm = new SpinnerNumberModel(20, 10, 30, 1); JSpinner s1 = new JSpinner(sm); JRadioButton r1, r2; String sSex = ""; public stu(String s, int x, int y, int w, int h) {// 构造函数
    super(s);
    setBounds(x, y, w, h);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);// 设置为false可以不让用户拉伸
    } public void showmain(stu ss) { // GUI组件添加
    Container contentPane = ss.getContentPane();
    JPanel p1 = new JPanel(new GridLayout(7, 2));
    JPanel p2 = new JPanel(new GridLayout(1, 2)); JButton b1 = new JButton("确定");
    JButton b2 = new JButton("取消"); JLabel L1 = new JLabel("姓名:", JLabel.CENTER);
    // JTextField F1 = new JTextField(); JLabel L2 = new JLabel("学号:", JLabel.CENTER);
    // JTextField F2 = new JTextField(); JLabel L3 = new JLabel("年龄:", JLabel.CENTER); JLabel L4 = new JLabel("性别:", JLabel.CENTER);
    JRadioButton r1 = new JRadioButton("男");
    JRadioButton r2 = new JRadioButton("女");
    ButtonGroup bg = new ButtonGroup();
    bg.add(r1);
    bg.add(r2); JLabel L5 = new JLabel("籍贯:", JLabel.CENTER);
    String data[] = { "四川", "重庆", "湖南", "湖北", "安徽", "云南", "贵州", "吉林", "辽宁",
    "河南", "河北", "北京", "上海", "广东" };
    JComboBox combo = new JComboBox(data);
    combo.setEditable(true);
    ComboBoxEditor editor = combo.getEditor();
    combo.configureEditor(editor, "可选择输入"); JLabel L6 = new JLabel("备注", JLabel.CENTER);
    // JTextField F6 = new JTextField(); p1.add(L1);
    p1.add(F1);
    p1.add(L2);
    p1.add(F2);
    p1.add(L3);
    p1.add(s1);
    p1.add(L4);
    p2.add(r1);
    p2.add(r2);
    p1.add(p2);// 把p2添加到p1上去
    p1.add(L5);
    p1.add(combo);
    p1.add(L6);
    p1.add(F6);
    p1.add(b1);
    p1.add(b2); b1.addActionListener(new Action1(this));// 对按钮进行监听,对输入信息的确定与取消的监听
    b2.addActionListener(new Action1(this));
    r1.addItemListener(new Action2(this));// 对于性别单选按钮的监听
    r2.addItemListener(new Action2(this));
    contentPane.add(p1, BorderLayout.CENTER);
    pack();// 排版
    jiguan = combo.getSelectedItem().toString(); }
    }class Action1 implements ActionListener {
    stu sf1 = null; public Action1(stu sf1) {
    this.sf1 = sf1;
    } public void actionPerformed(ActionEvent e) {// 确定,取消按钮被单击时触发
    String cmd = e.getActionCommand();
    if (cmd.equals("确定")) {
    String filename = "F:\\stuInfo.txt";// 保存的路径以及文件名
    try {
    BufferedWriter out = new BufferedWriter(
    new FileWriter(filename));
    // System.out.println(sf1.F1);
    System.out.println(sf1);
    System.out.println(sf1.F1.getText());
    out.write("学生姓名: " + sf1.F1.getText()); out.newLine();// 换行
    out.write("学生学号: " + sf1.F2.getText());
    out.newLine();
    // out.write("学生年龄: " + sf1.s1.getValue());
    out.newLine();
    out.write("学生性别: " + sf1.sSex);
    out.newLine();
    out.write("学生籍贯: " + sf1.jiguan);
    out.newLine();
    out.write("备注: " + sf1.F6.getText());
    out.newLine();
    out.close();
    } catch (IOException iox) {
    System.out.println("写入文件有错误!" + filename);
    } String filename2 = "F:\\stuInfo.txt";
    String line;
    try {
    System.out.println("你输入的信息如下:");
    BufferedReader in = new BufferedReader(
    new FileReader(filename2));
    line = in.readLine();
    while (line != null) {
    System.out.println(line);
    line = in.readLine();
    }
    in.close();
    } catch (IOException iox) {
    System.out.println("读文件有错误!" + filename2);
    }
    }
    if (cmd.equals("取消")) {
    System.exit(0);
    }
    }
    }class Action2 implements ItemListener {
    stu sf2 = null; String sex = ""; public Action2(stu sf2) {
    this.sf2 = sf2;
    } public void itemStateChanged(ItemEvent e) {// 单选按钮被单击时触发 if (e.getSource() == sf2.r1)
    sex = "男";
    if (e.getSource() == sf2.r2)
    sex = "女";
    sf2.sSex = sex;
    }
    }
      

  5.   

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at Action1.actionPerformed(ex3.java:114) 能看懂不,在ex3.java的114行,有空指针异常抛出!
      

  6.   

    引用 3 楼 believefym 的回复:
    JTextField F1 = new JTextField();  
    ------------  
    这个可是局部变量,不是你的类成员