0

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!

Jacob Moses
  • 163
  • 1
  • 4
  • 12
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Joel Apr 20 '15 at 14:57

3 Answers3

4

Let's look at your Directory class:

public class Directory extends File {
    private ArrayList<File> children;
    Directory(String name){ super(name); }
    void addChild(File f){ children.add(f); }
}

You never initialize children.

Try adding in the constructor:

children = new ArrayList<File>();

So the class would look like:

public class Directory extends File {

    private ArrayList<File> children;

    Directory(String name){
        super(name);
        children = new ArrayList<File>();
    }

    void addChild(File f){ children.add(f); }
}

Alternatively, you can just write:

private ArrayList<File> children = new ArrayList<File>();

In Java 7 and above you can simplify the constructor to just this:

new ArrayList<>()

The type is assumed to be the same.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
3

In Directory,

private ArrayList<File> children;

Declares an ArrayList but doesn't initialize it. You need something like

private ArrayList<File> children = new ArrayList<>();

But I would prefer coding to the List interface like

private List<File> children = new ArrayList<>();
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
3

You need to instantiate children first.

private ArrayList<File> children = new ArrayList<File>
Iswanto San
  • 18,263
  • 13
  • 58
  • 79