1

I have 2 files Details.java and TestDetails.java and a data file called 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);

        }
    }
}

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

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.

So what is the problem here? Why am i getting a NullPointerException?

p.s in the setFile() method argumnet, i pass in Details.dat in the args[0] position on the command prompt

0

4 Answers 4

3

You initialize the File first and after that set the file path. Try to use a constructor in the Details class:

public Details(String path)
        this.path = path;
        myFile = new File(path);
}
Sign up to request clarification or add additional context in comments.

Comments

3

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. This means that path is null at the time this line is executed.

You should change your code so that the File object is instantiated only when you need it.

1 Comment

Yep, move myFile = new File(path); to setMyFile. (Leave the declaration File myFile; at the class level, though.)
0
public class Details{
   private  String path;
   File myFile =new File(path);
   ...

How is myFile ever going have its path set to anything other than ""?

2 Comments

It's going to have its path set to null, not "".
True. I "blanked" out there for a second.
0

You are trying to make a File object with path name without initializing path. Because

File myFile = new File(path); 

line will be executed before setting path and at that time it is null. So first set value of path and then make object of File.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.