抛出java.lang.NullPointerException

时间:2015-11-14 04:04:23

标签: java

如果文件对象返回null,如何捕获抛出的null?我一直在java.lang.NullPointerException

public static File getFile() {
        String filePath = JOptionPane.showInputDialog("Enter the full path to           the file you want to open");
        File file = new File(filePath);
        while(!file.exists()) {
            filePath = JOptionPane.showInputDialog("Please enter a valid full path");
        }
        return file;
    }
}

1 个答案:

答案 0 :(得分:0)

关于public File(String pathname)构造函数的javadoc:

NullPointerException - If the pathname argument is null

因此,在将filePath传递给null

之前,检查new File(String path)不是 stdClass Object ( [result_count] => 1 [total_count] => 1 [next_offset] => 1 [entry_list] => Array ( [0] => stdClass Object ( [id] => 6adfd27d-0a48-48c9-97b2-5639c6d9e697 [module_name] => Leads [name_value_list] => stdClass Object ( [assigned_user_name] => stdClass Object ( [name] => assigned_user_name [value] => Administrator ) [modified_by_name] => stdClass Object ( [name] => modified_by_name [value] => Administrator ) [created_by_name] => stdClass Object ( [name] => created_by_name [value] => Administrator ) [id] => stdClass Object ( [name] => id [value] => 6adfd27d-0a48-48c9-97b2-5639c6d9e697 )