I am currently trying to help a friend on their Java classwork, and as someone not intimately acquainted with Java, I have run into some trouble. The code I have is as follows:
File Class
public abstract class File {
private String name;
private Directory parent;
public File(String name){this.name=name;}
Directory getParent(){return parent;}
// String getPath(){}
}
TextFile Class
public class TextFile extends File{
private String content;
TextFile(String name, String content){ super(name); }
String getContent(){ return content;}
void setContent(String content){this.content=content;}
}
Directory Class
import java.util.ArrayList;
public class Directory extends File{
private ArrayList<File> children;
Directory(String name){super(name);}
void addChild(File f){children.add(f);}
ArrayList<File> getChildren(){ return children; }
}
Main Class
public class Driver {
public static void main(String[] args){
Directory root = new Directory("/");
Directory a = new Directory("a");
if(a == null){
System.out.println("A is null. Program will not continue.");
}
else{
root.addChild(a);
TextFile b = new TextFile("b", "hello world");
}
}
}
The problem I am having is that when I attempt to run the code, I get a null pointer exception at:
Directory.addChild(Directory.java:8)
Driver.main(Driver.java:12)
I cannot for the life of me think of why this exception is happening. I can't think why at any point a
would be null. Any assistance is greatly appreciated. Thanks!