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?
Code :
/**
*
*/
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?
:-<:-s:-s:!!^:)^^:)^^:)^^:)^^:)^b-(b-(b-(b-(b-(=((
Re: Fixed Null Pointer Exception but still have another problem
Quote:
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.
Quote:
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.
Re: Fixed Null Pointer Exception but still have another problem
Quote:
Originally Posted by
copeg
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.
Re: Fixed Null Pointer Exception but still have another problem
Well, I had to turn it in.