简体   繁体   English

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

[英]null pointer exception while trying to load file using the File class

I have 2 files Details.java and TestDetails.java and a data file called Details.dat 我有2个文件Details.java和TestDetails.java和一个名为Details.dat的数据文件

Details.java 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);

        }
    }
}

TestDetails.java TestDetails.java

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());
        }
    }
}

Details.dat Details.dat

4

a 26 bsc

b 22 bcom

c 50 ba

d 60 bsc

Whenever i try to run the TestDetails.java file i get a NullPointerException and the stack trace points the stack trace towards the File object. 每当我试图运行TestDetails.java文件我得到一个NullPointerException异常和堆栈跟踪点指向File对象的堆栈跟踪。

So what is the problem here? 那么这是什么问题呢? Why am i getting a NullPointerException? 为什么我会收到NullPointerException?

ps in the setFile() method argumnet, i pass in Details.dat in the args[0] position on the command prompt ps在setFile()方法argumnet中,我在命令提示符下的args [0]位置传递了Details.dat

The problem is this: 问题是这样的:

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

The File myFile = new File(path) line will be executed when the object is constructed. 构造对象时,将执行File myFile = new File(path)行。 This means that path is null at the time this line is executed. 这意味着, path为空,在执行该行的时间。

You should change your code so that the File object is instantiated only when you need it. 从而使你应该改变你的代码File ,只有当你需要它的对象实例化。

You initialize the File first and after that set the file path. 您首先初始化文件,然后再设置文件路径。 Try to use a constructor in the Details class: 尝试在Details类中使用构造函数:

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

How is myFile ever going have its path set to anything other than "" ? myFile如何将其路径设置为除""以外的其他值?

You are trying to make a File object with path name without initializing path. 您试图使用路径名创建File对象而不初始化路径。 Because 因为

File myFile = new File(path); 

line will be executed before setting path and at that time it is null. 行将在设置路径之前执行,此时为null。 So first set value of path and then make object of File. 因此,首先设置path的值,然后将其作为File的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试加载文件时Java中的空指针异常 - Null pointer exception in java when trying to load file 尝试将CS​​V文件上传到服务器时出现空指针异常 - Null pointer exception while trying to upload a CSV file to the server 在FILE类中获取空指针异常 - Getting null pointer exception In FILE class 尝试使用Jersey 2.7和Spring 3加载Spring应用程序上下文时获取空指针异常 - Getting null pointer exception while trying to load spring application context using jersey 2.7 and spring 3 尝试从另一个类修改JFrame时出现空指针异常? - Null pointer exception while trying to modify a JFrame from another class? 尝试在另一个 class 中使用一种方法时 Null 指针异常 - Null pointer exception while trying to use one method in another class 尝试从文件中替换字符时出现空指针异常 - Null Pointer Exception, when trying to replace characters from file 使用 MockitoSugar 在 Scala 中模拟类时获取空指针异常 - Getting Null Pointer Exception while mocking a class in scala using MockitoSugar 尝试冒泡排序时出现空指针异常 - Null Pointer Exception while trying to Bubble Sort 写入 Excel 文件时出现空指针异常 - While Writing into Excel file getting Null pointer Exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM