【问题标题】:Issue with java reading file from src directoryjava从src目录读取文件的问题
【发布时间】:2016-03-30 19:37:37
【问题描述】:

我正在做一些大学实验室工作,应该从 src 目录中读取文件 Real.txt。但无论将什么信息传递给 testEceptions 方法,我都会得到一个 IOException。这可能是一些简单的事情,但我的头已经从一天的大学工作中融化了。

import java.awt.EventQueue; 
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class TestFourExceptionsGUI extends JFrame
{
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable() {
            public void run()
            {
                try {
                    TestFourExceptionsGUI frame = new TestFourExceptionsGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestFourExceptionsGUI()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnTestIoexception = new JButton("Test IOException");
        btnTestIoexception.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("I am here");
                testExceptions("Hi", "Whatever.txt", "http://www.itb.ie", false);
            }
        });
        btnTestIoexception.setBounds(115, 50, 180, 23);
        contentPane.add(btnTestIoexception);

        JButton btnTestUrlException = new JButton("Test Url Exception");
        btnTestUrlException.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                testExceptions("", "Real.txt", "http://www.itb.ie", false);
            }
        });
        btnTestUrlException.setBounds(115, 99, 180, 23);
        contentPane.add(btnTestUrlException);

        JButton btnTestNullpointerexception = new JButton("Test NullPointerException");
        btnTestNullpointerexception.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                testExceptions("Hi", "Real.txt", "http://www.itb.ie", false);
            }
        });
        btnTestNullpointerexception.setBounds(115, 152, 180, 23);
        contentPane.add(btnTestNullpointerexception);

        JButton btnGeneralException = new JButton("Test General Exception");
        btnGeneralException.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                testExceptions("","Real.txt","http://www.itb.ie", true);
            }
        });
        btnGeneralException.setBounds(115, 200, 180, 23);
        contentPane.add(btnGeneralException);
    }

    private static final Object URL = null;

    public void testExceptions(String str, String url, String filename, boolean generalexceptionActivated)
    {
        try {
            str.toCharArray(); //Null string potential error
            new FileReader(filename); //Unknown filename potential error
            new URL(url); //Badly written URL potential error
            if (generalexceptionActivated) { //Potential error
                this.clone(); //Will be caught as a general error!
            }
        } catch (MalformedURLException e) {
            JOptionPane.showMessageDialog(null, "A URL has been badly written " + e.getMessage(),  null, JOptionPane.INFORMATION_MESSAGE);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "An IOException has been caught " + e.getMessage(),  null, JOptionPane.INFORMATION_MESSAGE);
        } catch (NullPointerException e) {
            JOptionPane.showMessageDialog(null, "A NullPOinter Exception has been caught " + e.getMessage(),  null, JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "A General Exception has been caught " + e.getMessage(),  null, JOptionPane.INFORMATION_MESSAGE);
        } finally {
            JOptionPane.showMessageDialog(null, "The finally block has been called ",  null, ERROR);
        }
    }
}

例外是:

线程“AWT-EventQueue-0”中的异常 java.lang.RuntimeException:JOptionPane:类型必须是 JOptionPane.ERROR_MESSAGE、JOptionPane.INFORMATION_MESSAGE、JOptionPane.WARNING_MESSAGE、JOptionPane.QUESTION_MESSAGE 或 JOptionPane.PLAIN_MESSAGE 之一

【问题讨论】:

  • 您似乎以错误的顺序传递了 testExceptions(..) 参数。该方法接受字符串、url、文件名,但您似乎给了它一个字符串、文件名、url。
  • 谢谢 huipoloi 我认为这很简单

标签: java exception-handling nullpointerexception malformedurlexception


【解决方案1】:

感谢您的帮助,以下编辑解决了问题

    JButton btnTestIoexception = new JButton("Test IOException");
    btnTestIoexception.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            testExceptions("Hi","http://www.itb.ie","Whatever.txt", false);
        }
    });
    btnTestIoexception.setBounds(115, 50, 180, 23);
    contentPane.add(btnTestIoexception);

    JButton btnTestUrlException = new JButton("Test Url Exception");
    btnTestUrlException.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            testExceptions("Hi","","Real.txt", false);
        }
    });
    btnTestUrlException.setBounds(115, 99, 180, 23);
    contentPane.add(btnTestUrlException);

    JButton btnTestNullpointerexception = new JButton("Test NullPointerException");
    btnTestNullpointerexception.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
                testExceptions(null,"http://www.itb.ie","Real.txt",false);
        }
    });
    btnTestNullpointerexception.setBounds(115, 152, 180, 23);
    contentPane.add(btnTestNullpointerexception);

    JButton btnGeneralException = new JButton("Test General Exception");
    btnGeneralException.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            testExceptions("","http://www.itb.ie","Real.txt", true);
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多