一道有问题的小java

来源:互联网 发布:和男友逛街知乎 编辑:程序博客网 时间:2024/06/10 05:56

这是一个考试系统,有一点点令我头痛的小错误.希望有人能帮我

package demo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class iodemo2 extends JFrame implements ActionListener{
    JPanel p1,p2,p3,p4;
    JLabel lab;
    JRadioButton r[]=new JRadioButton[4];
    JButton b1,b2,b3,b4;
    String str="";
    //StringTokenizer str_sub;
    File f;
    BufferedReader in;
    public iodemo2() {
        super("考试系统");
        this.setSize(300,180);
        this.setDefaultCloseOperation(3);
        p1=new JPanel();
        p1.add(new JLabel("题目"));
        lab=new JLabel();
        p1.add(lab);
        p2=new JPanel();
        JRadioButton r[]=new JRadioButton[4];
        ButtonGroup rbg=new ButtonGroup();//编组
        for(int i=0;i<4;i++){
            r[i]=new JRadioButton();
            rbg.add(r[i]);
            p2.add(r[i]);
        }
        p3=new JPanel();
        b1=new JButton("上一题");
        b1.setEnabled(false);
        b2=new JButton("下一题");
        p3.add(b1);
        b2.addActionListener(this);
        p3.add(b2);
        p4=new JPanel();
        b3=new JButton("交卷");
        b4=new JButton("查看");
        b4.setEnabled(false);
        p4.add(b3);
        p4.add(b4);
        this.getContentPane().setLayout(new GridLayout(4,1));
        this.getContentPane().add(p1);
        this.getContentPane().add(p2);
        this.getContentPane().add(p3);
        this.getContentPane().add(p4);
        this.setResizable(false);
        File f=new File("/a.txt");
        try {
                    BufferedReader in = new BufferedReader(new FileReader(f));
                    str=in.readLine();
                    StringTokenizer str_sub=new StringTokenizer(str,"#");
                    lab.setText(str_sub.nextToken());
                    for(int i=0;i<4;i++){
                        r[i].setText(str_sub.nextToken());
                    }
                    //str=in.readLine();

                } catch (IOException ex) {
                }

               
        this.setVisible(true);
    }

    public static void main(String[] args) {
        iodemo2 iodemo2 = new iodemo2();
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==b2){
            try {
                /*
                File f=new File("/a.txt");
                BufferedReader in = new BufferedReader(new FileReader(f));
                                         
                if((str=in.readLine())!=null){
               
                    StringTokenizer str_sub = new StringTokenizer(str, "#");
                    this.lab.setText(str_sub.nextToken());
                    for (int i=0; i<4; i++) {
                        this.r[i].setText(str_sub.nextToken());
                    }
                }*/
               Scanner c = new Scanner(f);
               str=c.nextLine();
               StringTokenizer str_sub = new StringTokenizer(str, "#");
                    this.lab.setText(str_sub.nextToken());
                    for (int i=0; i<4; i++) {
                        this.r[i].setText(str_sub.nextToken());
                    }
            }
            catch (IOException ex) {
                javax.swing.JOptionPane.showMessageDialog(null,"有错");
            } 
            catch (NullPointerException ex) {
               javax.swing.JOptionPane.showMessageDialog(null,"空对象");
           }             
            }

        }
   

说我有个空对象,可我是不知道空对象是哪个.我都晕了

原创粉丝点击