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 4 of 4

Thread: Fixed Null Pointer Exception but still have another problem

  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 Fixed Null Pointer Exception but still have another problem

    I'd like a loop that will increment it so that when I add a document to a Directory, it'll also add that Document to the parent of the Directory, and add one to its parent, till it reaches the root Directory, then it stops.

    Then I somehow need to add the names of each of these Documents to an ArrayList, or an array, -preferably an ArrayList, and return all the names with the Number of Documents.

    However, since there could be at most 100 Documents in a Directory, how do I do it without having to create 100 arrays, 98 or which won't be used if I do and will return Null Pointer Exceptions?

    /**
     * 
     */
    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().numOfDocs = doc.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.
     
     
    }

    I need a method called toString() that'll return the names of all the Documents in each Directory and the number of Documents in each Directory.

    I know I could create an array of size numberOfDocumentsInDirectory and get it to return the names that way, but how do I do that for potentially 100 Documents in each Directory, all of which, I suppose, could be Directories?



  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Fixed Null Pointer Exception but still have another problem

    I'd like a loop that will increment it so that when I add a document to a Directory, it'll also add that Document to the parent of the Directory, and add one to its parent, till it reaches the root Directory, then it stops.
    Many ways to do this: for, while, recursion. Choose your poison. You could ceate a method in your Directory class which returns a boolean indicating it is the root directory, then loop up until this returns the appropriate value indicating it is the root.

    Then I somehow need to add the names of each of these Documents to an ArrayList, or an array, -preferably an ArrayList, and return all the names with the Number of Documents.

    However, since there could be at most 100 Documents in a Directory, how do I do it without having to create 100 arrays, 98 or which won't be used if I do and will return Null Pointer Exceptions?
    It almost sounds like you wish to create a tree data structure. If this term is foreign language to you, I'd encourage you to do a web search on them. They are fast, powerful, and in my opinion a must know for programming.

  3. #3
    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: Fixed Null Pointer Exception but still have another problem

    Quote Originally Posted by copeg View Post
    Many ways to do this: for, while, recursion. Choose your poison. You could ceate a method in your Directory class which returns a boolean indicating it is the root directory, then loop up until this returns the appropriate value indicating it is the root.


    It almost sounds like you wish to create a tree data structure. If this term is foreign language to you, I'd encourage you to do a web search on them. They are fast, powerful, and in my opinion a must know for programming.
    It's foreign to me. We're not covering those till later. I doubt he'd be expecting me to use those yet.

    How would this boolean method work? I'm kind of short on time. It's just the toString() that won't do what it's supposed to. All other parts of the program work fine.

  4. #4
    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: Fixed Null Pointer Exception but still have another problem

    Well, I had to turn it in.

Similar Threads

  1. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  2. HttpURLConnection null pointer exception
    By chopficaro in forum Java Theory & Questions
    Replies: 0
    Last Post: May 10th, 2010, 10:19 AM
  3. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  4. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM