Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试使用file类加载文件时出现空指针异常_Java_File_Nullpointerexception - Fatal编程技术网

Java 尝试使用file类加载文件时出现空指针异常

Java 尝试使用file类加载文件时出现空指针异常,java,file,nullpointerexception,Java,File,Nullpointerexception,我有两个文件Details.java和TestDetails.java以及一个名为Details.dat的数据文件 Details.java import java.util.Scanner; import java.io.*; public class Details { private String path; File myFile = new File(path); public void setMyFile(String path) { th

我有两个文件Details.java和TestDetails.java以及一个名为Details.dat的数据文件

Details.java

import java.util.Scanner;
import java.io.*;

public class Details {
    private String path;
    File myFile = new File(path);

    public void setMyFile(String path) {
        this.path = path;

    }

    public void load() throws IOException, FileNotFoundException {
        Scanner input = new Scanner(myFile);

        int numberOfMembers = input.nextInt();

        String[] members = new String[numberOfMembers];

        for (String s : members) {

            String name = input.next();

            String age = input.next();

            String qualification = input.next();

            System.out.println("The name of the family member is " + name + " the age of the family member is" + age
                    + " the qualification of the " + "family member is" + qualification);

        }
    }
}
import java.io.IOException;

public class TestDetails {

    public static void main(String[] args) {

        Details myDetails = new Details();

        myDetails.setMyFile(args[0]);

        try {
            myDetails.load();
        } catch (IOException i) {

            System.out.println(i.getMessage());
        }
    }
}
TestDetails.java

import java.util.Scanner;
import java.io.*;

public class Details {
    private String path;
    File myFile = new File(path);

    public void setMyFile(String path) {
        this.path = path;

    }

    public void load() throws IOException, FileNotFoundException {
        Scanner input = new Scanner(myFile);

        int numberOfMembers = input.nextInt();

        String[] members = new String[numberOfMembers];

        for (String s : members) {

            String name = input.next();

            String age = input.next();

            String qualification = input.next();

            System.out.println("The name of the family member is " + name + " the age of the family member is" + age
                    + " the qualification of the " + "family member is" + qualification);

        }
    }
}
import java.io.IOException;

public class TestDetails {

    public static void main(String[] args) {

        Details myDetails = new Details();

        myDetails.setMyFile(args[0]);

        try {
            myDetails.load();
        } catch (IOException i) {

            System.out.println(i.getMessage());
        }
    }
}
详细信息。dat

4

a 26 bsc

b 22 bcom

c 50 ba

d 60 bsc
每当我尝试运行TestDetails.java文件时,我都会得到一个NullPointerException,堆栈跟踪将堆栈跟踪指向file对象

那么这里的问题是什么?为什么我会得到一个NullPointerException

p、 在setFile()方法argumnet中,我在命令提示符下的args[0]位置传入Details.dat

public class Details{
   private  String path;
   File myFile =new File(path);
   ...
myFile
如何将其路径设置为除
以外的任何其他路径

myFile
如何将其路径设置为除
”以外的任何其他路径?

问题在于:

public class Details{
    private String path;
    File myFile = new File(path);
    ...
构建对象时将执行
File myFile=new File(path)
行。这意味着执行此行时,
path
为空

您应该更改代码,使
文件
对象仅在需要时实例化。

问题在于:

public class Details{
    private String path;
    File myFile = new File(path);
    ...
构建对象时将执行
File myFile=new File(path)
行。这意味着执行此行时,
path
为空


您应该更改代码,以便只在需要时实例化
文件
对象。

首先初始化文件,然后设置文件路径。 尝试在Details类中使用构造函数:

public Details(String path)
        this.path = path;
        myFile = new File(path);
}

首先初始化文件,然后设置文件路径。 尝试在Details类中使用构造函数:

public Details(String path)
        this.path = path;
        myFile = new File(path);
}

您正在尝试使用路径名创建文件对象,而不初始化路径。因为

File myFile = new File(path); 

行将在设置路径之前执行,此时为空。所以,首先设置路径的值,然后创建文件的对象。

您正在尝试使用路径名创建文件对象,而不初始化路径。因为

File myFile = new File(path); 

行将在设置路径之前执行,此时为空。所以,首先设置路径的值,然后创建文件的对象。

它将把路径设置为
null
,而不是
。True。我在那里“屏蔽”了一秒钟。它的路径将设置为
null
,而不是
。True。我在那里“屏蔽”了一秒钟。是的,move
myFile=新文件(路径)
setMyFile
。(将声明
filemyfile;
保留在类级别。)是的,move
myFile=newfile(path)
setMyFile
。(不过,将声明
File myFile;
保留在类级别。)