I have a problem with a null Pointer Exception. Also, given that I need to get a Directory to add either a Directory or a File to it, how do I do this, plus set it as its parent?
In this assignment you will implement the structure of a very simple file system. This assignment is relatively easy to implement, but you must pay attention to the design more carefully. Design includes decisions like which method to write and which class to best put it in, public/private access modifiers, etc. There are primarily two types of documents in your file system. The first is a directory (folder) that contains a list of other documents contained within it. The other type of document is a file, which has an extension (i.e. .pdf, .txt, etc.). Each document has a name (without an extension) and exactly one parent (for instance, the parent of a file is the directory in which that file is). You can assume that a directory can hold at most 100 other documents (i.e. directories or files). The toString method of a directory returns a String that identifies it as a directory, its name and contains a list of all the names of the documents that the directory contains, along with the number of documents. The toString method of a file returns a string that identifies it as a file and contains its name along with extension (e.g. “File: Assignment 3.pdf”). Each document should have a method that returns its “path” as a String object (e.g. C:\Users\ashesh\Documents\teaching\isu\ITK 179\Fall 2010\assignments) (Hint: the path of a document can be expressed in terms of the path of its parent). Finally each file has a method “open” that opens the file in the appropriate program. For example, if the file represents a pdf file, the open method will open it in Acrobat Reader. In order to accomplish this, a helper method has been provided to you that will work only on Windows. In a separate class with a main method, emulate the folder structure provided. It should represent all the folders and files exactly as specified. If you then access the appropriate object for a file and call its “open” method, it should open the file from your Java program. To do this, unzip the provided file and place the unzipped contents in your Eclipse project folder (for this assignment). Set the name of the first directory (i.e. “Assignment”) as the complete path of this directory on your machine. In order to do this from within the Java program, call the method System.getProperty(“user.dir”). This method returns a string that has the complete path of the current folder (i.e. in your case it will return the path to your Assignment 3 Eclipse project folder).
I added that in full so that you'd understand I wasn't talking about the File and Path and Directory, if there is such a class, classes in Java. Also, so you'd understand what I was asking earlier about the add method.
/** * */ package Assignment3; /** * @author pradcoc * */ public class Document { private Directory parent; private String name; private String name2 = ""; private Directory parent2; public Document(String name, Directory parent) { name2 = name; parent2 = parent; } public Directory getParent() { return(parent2); } public String getName() { // name2 = this.name; return(name2); } public String getPath() { String path = this.getParent().getPath() + "/" + this.getName(); // this line here is throwing the // Null Pointer Exception. But why is this.getParent().getPath() null and how can I fix that? return(path); }
/** * */ package Assignment3; /** * @author pradcoc * */ public class Directory extends Document { public Directory(String name, Directory parent) { super(name, parent); } public void add(Directory d) { } public void add(File f) { } // my test program can set a parent, but it can't tell if a directory is a parent of another directory or file. }
/** * */ package Assignment3; /** * @author pradcoc * */ public class File extends Document { private String extension2 = ""; public File(String name, Directory parent, String extension) { super(name,parent); extension2= extension; } public String getExtension() { return(extension2); } }
/** * */ package Assignment3; /** * @author pradcoc * */ public class TestProgram { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Directory d1 = new Directory("Assignment", null); Directory d2 = new Directory("code", d1); Directory d3 = new Directory("Description", d1); File f1 = new File("RectangleTest", d2, ".java"); File f2 = new File("Assignment 1", d3, ".docx"); File f3 = new File("Assignment 1", d3, ".pdf"); System.out.println(f2.getName()); System.out.println(f2.getParent()); // System.out.println(f3.getPath()); System.out.println(f3.getParent().getPath()); } }
The open() method is:
public void open() { try { String param = "rundll32 url.dll,FileProtocolHandler "; //the above line merely uses the Windows utility that determines which program to open //to open a file "C:\user\A2.pdf", the above string has to be // "rundll32 url.dll, FileProtocolHandler C:\user\A2.pdf" //thus modify this method accordingly Process p = Runtime.getRuntime().exec(param); } catch (IOException e) { return; } }
Something has to be changed in it in order to get it to use the path and the extension.