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