Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: NullPointerException

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Angry NullPointerException

    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.




    Last edited by javapenguin; September 28th, 2010 at 08:54 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: NullPointerException

    I hope you don't expect your questions to always be answered within an hour (or in this case ~15 minutes) of when you post it. If this is an assignment, I would strongly recommend that you ask well in advance (about 48 hours in advance). Don't bump excessively or relatively soon after your original post (please give at least ~24 hours before bumping, I would recommend even longer as bumping is quite annoying)

    The problem is coming because your top-most directory is using null to denote it's the top-most directory. Simply check to see if the parent is null. If it is, you can stop recursively trying to find the path. If it's not, keep searching.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    javapenguin (September 28th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: NullPointerException

    Quote Originally Posted by helloworld922 View Post
    I hope you don't expect your questions to always be answered within an hour (or in this case ~15 minutes) of when you post it. If this is an assignment, I would strongly recommend that you ask well in advance (about 48 hours in advance). Don't bump excessively or relatively soon after your original post (please give at least ~24 hours before bumping, I would recommend even longer as bumping is quite annoying)

    The problem is coming because your top-most directory is using null to denote it's the top-most directory. Simply check to see if the parent is null. If it is, you can stop recursively trying to find the path. If it's not, keep searching.

    It's not supposed to be doing that. The top directory should still have a path of

    Assignment

    However, the top directory has no parent. But how do I fix that problem? I've been trying for a few hours already.

    How would I get it to know if it's the top directory?

    if(topdirectory)
    {
    stop
    }

    else
    {
    keep searching
    }

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: NullPointerException

    I hope you don't expect your questions to always be answered within an hour (or in this case ~15 minutes) of when you post it. If this is an assignment, I would strongly recommend that you ask well in advance (about 48 hours in advance). Don't bump excessively or relatively soon after your original post (please give at least ~24 hours before bumping, I would recommend even longer as bumping is quite annoying)
    I agree with this. I try to get on this forum a few times a day when I have spare time. I always go through all of the newly created threads since my last visit. If the questions are reasonable or have not been answered, I try to provide as much help as I can. Others appreciate it, I reinforce my understanding by teaching others, and I sometimes learn new things (usually from Norm). Since your question was worded correctly and you could identify the problem, but just needed the solution or an explanation of the problem, I would usually respond with assistance, but I'm not sure if I can add much more to what helloworld922 said.

  6. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: NullPointerException

    Quote Originally Posted by aussiemcgr View Post
    I agree with this. I try to get on this forum a few times a day when I have spare time. I always go through all of the newly created threads since my last visit. If the questions are reasonable or have not been answered, I try to provide as much help as I can. Others appreciate it, I reinforce my understanding by teaching others, and I sometimes learn new things (usually from Norm). Since your question was worded correctly and you could identify the problem, but just needed the solution or an explanation of the problem, I would usually respond with assistance, but I'm not sure if I can add much more to what helloworld922 said.
    That's exactly what I do!

  7. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,669
    Thanks
    25
    Thanked 327 Times in 304 Posts

    Default Re: NullPointerException

    Quote Originally Posted by javapenguin View Post
    It's not supposed to be doing that. The top directory should still have a path of

    Assignment

    However, the top directory has no parent. But how do I fix that problem? I've been trying for a few hours already.

    How would I get it to know if it's the top directory?

    if(topdirectory)
    {
    stop
    }

    else
    {
    keep searching
    }
    Well the Top Directory should have some sort of Parent, right? I mean, if we are talking about a real file system, I think the only Directory without a Parent would be the Drive(if you consider that to be a Directory).

    Where is the null pointer occurring specifically? I dont actually see anywhere in your code where a null pointer would likely occur (since you don't seem to do anything with parent directories yet).

    If we are going with the assumption that the Top Directory has no Parent, you could continue to recurse through the Directories until you reach one with a null parent.
    if(directory.getParent()==null)
    {
    stop
    }
     
    else
    {
    keep searching
    }
    Last edited by aussiemcgr; September 28th, 2010 at 10:14 PM.

  8. The Following User Says Thank You to aussiemcgr For This Useful Post:

    javapenguin (September 28th, 2010)

  9. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: NullPointerException

    Quote Originally Posted by aussiemcgr View Post
    Well the Top Directory should have some sort of Parent, right? I mean, if we are talking about a real file system, I think the only Directory without a Parent would be the Drive(if you consider that to be a Directory).

    Where is the null pointer occurring specifically? I dont actually see anywhere in your code where a null pointer would likely occur (since you don't seem to do anything with parent directories yet).

    If we are going with the assumption that the Top Directory has no Parent, you could continue to recurse through the Directories until you reach one with a null parent.
    if(directory.getParent()==null)
    {
    stop
    }
     
    else
    {
    keep searching
    }
    Hey, now there's an idea:

    C:/Users can be the top directory

    but how exactly do I get it to stop at C:/Users and not keep going into NullVille?

  10. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: NullPointerException

            Directory d1 = new Directory("Assignment", null);
    This is where you're running into problems.

    Yes Assignment is indeed the top directory, but your program is still looking for the parent of Assignment.

    Judging by the way you had this declared I simply assumed that you used null to indicate that a directory was the root directory (i.e. it had no parent).

    So your get path method needs to account for this fact.

    	public String getPath()
    	{
    	        if(/* TODO: some check needs to go here to determine if this item has a parent or not*/)
    	        {
    	               // we have no parent.
    	                return "";
    	        }
    	        else
    	        {
    		String path = this.getParent().getPath() + "/" + this.getName();
    		return path;
    	        }
    	}

  11. The Following User Says Thank You to helloworld922 For This Useful Post:

    javapenguin (September 29th, 2010)

  12. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Thumbs up Re: NullPointerException

    Quote Originally Posted by helloworld922 View Post
    This is where you're running into problems.

    Yes Assignment is indeed the top directory, but your program is still looking for the parent of Assignment.

    Judging by the way you had this declared I simply assumed that you used null to indicate that a directory was the root directory (i.e. it had no parent).

    So your get path method needs to account for this fact.

    	public String getPath()
    	{
    	        if(/* TODO: some check needs to go here to determine if this item has a parent or not*/)
    	        {
    	               // we have no parent.
    	                return "";
    	        }
    	        else
    	        {
    		String path = this.getParent().getPath() + "/" + this.getName();
    		return path;
    	        }
    	}




    What goes in the if statement?

    if(this.getParent() == null)

    ?

    What's bumping?
    Last edited by javapenguin; September 29th, 2010 at 10:45 AM.

  13. #10
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: NullPointerException

    Bumping is posting "Can anyone help me", "bump", or anything that is useless and proves how impatient you are.

  14. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: NullPointerException

    Quote Originally Posted by javapenguin View Post
    What goes in the if statement?

    if(this.getParent() == null)

    ?
    Yes, that's exactly right. If the directory's parent is null, it has no parent.

    Quote Originally Posted by javapenguin View Post
    What's bumping?
    bumping is a posting in essence a rubbish/useless post so the list that shows new posts will put your post higher up because there's a newer post. Sometimes it's useful if you haven't gotten any answers for your thread and it's been several days (note: days, not minutes), but if used too often or too soon it's very annoying.

  15. The Following User Says Thank You to helloworld922 For This Useful Post:

    javapenguin (September 29th, 2010)

  16. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: NullPointerException

    I've updated the code: However, how do I get a toString() method of Directory to return the number of Documents it has in it and their names?

    I think I need an ArrayList, but don't know how to set it up. All other stuff now works, except it still can't even get the right # of documents and I have no clue how to get it to return their names either.

    /**This is class Document.  It has a method to return its name.  The name is set by the
     * constructor.  It has a method to return its parent.  Its parent is also set by the constructor
     * 
     * 
     */
    package Assignment3;
     
    /**
     * @author pradcoc
     *
     */
    public class Document {
     
    	private Directory parent;
    	private String name;
    	private String name2 = "";
    	private Directory parent2;
    	private String parent3 = "";
     
    	public Document(String name, Directory parent)
    	{
     
    		 name2 = name;
    		parent2 = parent;
    	}
     
    	// returns the parent of the Document, if any.  
    	public Directory getParent()
    	{
    		return(parent2);
    	}
     
    	// returns the name of the Document
    	public String getName()
    	{
    		// name2 = this.name;
    		return(name2);
    	}
     
    	// this method should, when fixed, set the parent, again, so that I call
    	// call it and get a Directory to know how many Documents it has in it.  
    	public void setParent(Directory dir)
    	{
    		parent3 = dir.getName();
     
    	}
     
    	// returns the path of the Document
    	public String getPath()
    	{
    		// this is in case the Document is the root Directory
    		if (this.getParent() == null)
    		{
    			String pth = this.getName();
    			return(pth);
    		}
     
    		// this is if it's not the root Directory  
    		else
    		{
    		String path = this.getParent().getPath() + "/" + this.getName();
    		return(path);
    		}
    	}
    }

    /**
     * 
     */
    package Assignment3;
     
    /**
     * @author pradcoc
     *This is directory.  It uses most of the parent methods, but has a toString() method
     *and since Object already has one, it's silly to make Document abstract for that.
     *It can add Documents and sets itself as the parent of those Documents.  
     */
    public class Directory extends Document {
    private int numOfDocs = 0;
     
    	public Directory(String name, Directory parent)
    	{
    		super(name, parent);
     
     
     
    	}
     
    	// this is supposed to add a Document to a Directory, set that Directory as
    	// the parent of the Document, unless the Document is the root directory, 
    	// and also add up from 0 the number of Documents it has in it.  And I need
    	// a way to return the names of the Documents in it too.
     
    	public void add(Document doc)
    	{
    		// if the Document is a Directory
    		if (doc instanceof Directory)
    		{
    			// if it's not the root directory
    			if(doc.getParent() != null)
    			{
    				doc.setParent(this);
    			numOfDocs = numOfDocs + 1; // adds one to this Directory's number of Documents
    			doc.getParent().numOfDocs = doc.getParent().numOfDocs + 1; // should also add
    			// one to parent Directory's number of Documents as well.  Not quite working nicely.
    			}
     
    			// if it's the root directory
    			else
    			{
    				doc.setParent(this); // sets this directory, the root directory, as the parent
    				numOfDocs = numOfDocs + 1; // adds 1 to the number of Documents this Directory has
     
    			}
     
    		}
     
    		else
    		{
     
    			if (doc.getParent().getParent() != null)
    			{
    				doc.setParent(this);
    				doc.getParent().numOfDocs = doc.getParent().numOfDocs + 1;
    				doc.getParent().getParent().numOfDocs = doc.getParent().getParent().numOfDocs + 1;
    			}
     
    			else
    			{
    				doc.setParent(this);
    				doc.getParent().numOfDocs = doc.getParent().numOfDocs + 1;
    			}
    		}
    	}
     
    	public int returnNumberOfDocs()
    	{
    		return(numOfDocs);
    	}
     
    // my test program can set a parent, but it can't tell if a directory is a parent of another directory or file.
     
     
    }

    /**
     * This is class File.  It takes the superclass parameters plus a String extension.
     * 
     */
    package Assignment3;
     
    import java.io.IOException;
     
    /**
     * @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);
    	}
     
    	public String toString()
    	{
    		String str = "File: " + getName() + this.getExtension();
    		return(str);
    	}
     
    	public void open()
    	{
    		try 
    		{
    			String param = "rundll32 url.dll,FileProtocolHandler " + getPath()+ this.getExtension();
     
    			Process p = Runtime.getRuntime().exec(param);
    		} catch (IOException e) 
    		{
    			return;
    		}
    	}
    }

    /**
     * 
     */
    package Assignment3;
     
    /**
     * @author Paul Adcock
     * Assignment 3
     * last worked on: September 29, 2010
     *
     */
    public class TestProgram {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String s = System.getProperty("user.dir") + "/" + "Assignment";
    		Directory d1 = new Directory(s, 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());
    		System.out.println(f3.getPath());
    		f3.open();
    		f2.open();
    		f1.open();
    		d1.add(d2);
    		d1.add(d3);
    		d2.add(f1);
    		d3.add(f2);
    		d3.add(f3);
    		System.out.println(d1.returnNumberOfDocs());
    		System.out.println(d2.returnNumberOfDocs());
     
    	}
     
    }
    Last edited by javapenguin; September 29th, 2010 at 06:07 PM.

  17. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: NullPointerException

    Just look at the Directory class. That's the one I need to fix.

    For the root directory, it needs to be outputting one more than it is.

    I got rid of

    doc.getParent().getParent().numOfDocs = doc.getParent().getParent().numOfDocs + 1;

    in Directory.

    But I'm getting that the root directory has 4 Documents, but it should be outputting 5.

    Also, no idea how to get it to return the names for each of the Documents. Perhaps an ArrayList, but beyond that, I don't know.

    I'm using a second post because the previous one is rather big and it's easier to figure out what I've found out so that it's easier to fix the problem with the previous code
    if you can find what I'm saying a lot quicker.

    I need a toString() for the Directory class, that includes, among things I can already do, the names of the Documents that each directory has and the number of Documents each Directory has.

    I've nearly got the # problem fixed, but am still a bit confused on what to do about the names.
    Last edited by javapenguin; September 29th, 2010 at 06:38 PM.

  18. #14
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    491
    Thanks
    8
    Thanked 47 Times in 45 Posts

    Default Re: NullPointerException

    Continued here:
    Fixed Null Pointer Exception but still have another problem

    db

Similar Threads

  1. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM
  2. Funny NullPointerException
    By Marcus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2009, 09:14 AM
  3. [SOLVED] NullPointerException.CLARIFICATION
    By chronoz13 in forum Exceptions
    Replies: 8
    Last Post: August 28th, 2009, 03:24 PM
  4. [SOLVED] NullPointerException in Poker's program
    By dean_martin in forum Exceptions
    Replies: 10
    Last Post: April 21st, 2009, 06:40 AM
  5. NullPointerException in Java
    By jazz2k8 in forum Exceptions
    Replies: 9
    Last Post: June 1st, 2008, 05:59 PM