I'm pretty new to Java so bear with me. I've got a small bit of code that
checks to see if a currentNode has a property of "fileReference" and returns its value.
Except it doesn't seem like my null check is working because if nothing is in fileReference
I get an error. If a reference to fileReference exists it works fine. Here is the error:
Caused by: javax.jcr.PathNotFoundException: fileReference
Here is my code:
if(currentNode != null){
NodeIterator checkNode = currentNode.getNodes();
while (checkNode.hasNext()) {
Node imageNode = checkNode.nextNode();
printNodeTitle = imageNode.getProperty("fileReference").getString();
}
}
public String getImageNode() { (printNodeTitle != null) ? return printNodeTitle : return ""; }
Any help is greatly appreciated!
I'm pretty confident fileReference is actually a property, not a seperate node (since you are calling properties). Since you know the name of the property you want to get, I suggest getting it directly with a small check if it exists.
if(currentNode != null){
NodeIterator checkNode = currentNode.getNodes();
while (checkNode.hasNext()) {
Node imageNode = checkNode.nextNode();
if(imageNode.hasProperty("fileReference")){
Property fileReferenceProp = imageNode.getProperty("fileReference");
printNodeTitle = fileReferenceProp.getString();
}
}
}
I'm assuming you deal with possible repository exceptions elsewhere
I'm not an expert on sling but try this
if(currentNode != null){
NodeIterator checkNode = currentNode.getNodes();
while (checkNode.hasNext()) {
Node imageNode = checkNode.nextNode();
Iterator<Node> fileReferences = imageNode.getProperties("fileReference");
if(fileReferences.hasNext()) { // You might want to improve this
printNodeTitle = fileReference.next().getString(); // You might want to improve it
}
}
}
You retrieve all nodes with getProperties(String), if no nodes are found and empty Iterator is retrieved.
The line Node fileReference... I just guessed the type (Node) you must change it.
Related
i am trying to find a path in between two nodes(sourcenode and targetnode). i came up with this code but i cant seem to make it recursively find a path. i even set the nodes to null if the target node is found but i keep getting a stack overflow error.
public void findPathBetween(Node sourceNode, Node targetNode){
//find a path between the sourceNode and targetNode
//select the nodes and edges along the path if one exists.
ArrayList<Node> nodesToSearch = new ArrayList<Node>();
nodesToSearch.add(sourceNode);
//basis
if(sourceNode == null || targetNode == null) return;
//recursion
ArrayList<Node> newNodesToSearch = new ArrayList<Node>(); //get nodes for next level to search
for(Node aNode : nodesToSearch) {
for (Node neighbour : aNode.getNeighbours()) {
if (neighbour != targetNode && newNodesToSearch.isEmpty()) {
newNodesToSearch.add(neighbour);
neighbour.setSelected(true);
edgeBetween(aNode, neighbour).setSelected(true);
sourceNode = neighbour;
}
if (neighbour == targetNode) {
sourceNode = null;
targetNode = null;
return;
}
}
}
if(sourceNode != null &&targetNode != null) {
findPathBetween(sourceNode, targetNode);
}
}
You are storing the state inside the recursion function, and don't pass it to the next iteration. Thus you are simply running one function over and over without changing its arguments and the state that affects its execution.
I do not want to correct your code because I would have to guess you intents in order to provide a good explanation.
Anyway I think that you were trying to implement DFS, so I suggest you to take a look at some working implementations. Just google them, there are plenty, e.g. this one comes with a piece of theory, is simple, and was written by Robert Sedgewick, so it can be trusted.
I'm working on this method with linked list.
It's a method that add's an object at the end of the list.
I've got a problem when adding a second object in the linked list.
It gives me a NullPointerException at the while :
while (this.actual.getNext() != null)
I can't see what's wrong and i've been on this for an hour doing junits tests.
Any help ?
here's the complete code :
public boolean addEnd(T element) {
boolean res = false;
this.actual = this.head;
if (element != null) {
if (this.actual == null) {
this.head= new Node<T>(element);
res = true;
nbElm++;
} else if (!hasElement(element)) {
while (this.actual.getNext() != null) { //Gives me an error NullPointeException
this.actual = this.actual.getNext();
}
Node<T> next = new Node<T>(element);
this.actual.setNext(next);
res = true;
nbElm++;
}
}
return res;
}
Looking at this addEnd method, I think this.actual has no reason of being an instance variable. It should be a local variable of the method. Being an instance variable may cause other methods that use it to interfere with addEnd. I'm guessing hasElement modifies this variable, causing this.actual to become null before the start of your while loop.
I am using a binary tree structure here. I am getting a "NullPointerException" from the line containing the while statement. I am completely confused about why that would be.
BinaryTreeNode<CharData> currNode = theTree.findValue(data);
// Move up the Binary Tree to create code.
while(currNode.getParent() != null) {
// The loop does some stuff that doesn't
// affect what is assigned to currNode.
// Move to the parent node for the next iteration.
currNode = currNode.getParent();
} // End the while loop.
return code; // Return the string of binary code.
Find value is a method from my BinaryTree class that searches for and finds the node containing specific data. I know this works from testing it separately outside of this implementation.
The only reason why the while-loop statement can throw a NPE is, when currNode is null. I suspect findValue() returned null.
I guess one fix (when you care about the topmost node) would be:
while(currentNode != null) {
rootNode = currentNode;
currentNode = currentNode.getParent();
}
Or the typical pattern which relies on boolean shortcut evaluation:
while(curentNode != null && currentNode.getParent() != null)
Or my prefered solution using guards:
if (currentNode == null)
throw NotFound(); // or return something
while(curentNode.getParent() != null) {
If you see the code:
BinaryTreeNode<CharData> currNode = theTree.findValue(data);
I guess, currNode is getting some value if findValue() able to search data else it is returning NULL values.
When it returns a NULL value it will throw NPE.
To avoid it, you can modify your code a little bit.
while(currNode != null && currNode.getParent != null) {
// your code here
}
I have a HW assignment and only one small part of it is to make a copy constructor which makes a Deep Copy of the linked list which you have entered in its parameters.
I understand that this means, that the List you have entered remains unchanged, and that the new linked list is isolated from the "old" one. My code gives me a new list which is exactly the same as the old one (the one you enter as a parameter) and this is what I want, but the old one is changed.
Here is the constructor:
public SortedLinkedSet(SortedLinkedSet<T> copy) {
if (copy == null) {
this.firstNode = null;
} else{
SortedLinkedSetNode firstNode1 = new SortedLinkedSetNode(copy.getFirstNode().value);
this.firstNode = firstNode1;
// so basically I am chaining elements from "copy" to firstNode1 and then making "this" = to firstNode1.
while (copy.firstNode.next !=null) {
firstNode1.add(copy.getFirstNode().next.value);
this.firstNode = firstNode1;
copy.firstNode = copy.firstNode.next;
// at the end of this loop I have a successful new linkedList with the same value, but "copy" has been changed
}
}
}
If for example I enter a linked list which has the values (1,2,3) -- with this constructor i get back a new linked list with values 1,2,3 but the old one just has 1.. If someone can help me with why this is going wrong it would be great. Thanks
UPDATE : As Ireeder pointed out, and with a test I did, I am almost sure that the problem is in the statement :
copy.firstNode = copy.firstNode.next;
i deleted the current code, and did the following test:
SortedLinkedSetNode firstNode = new SortedLinkedSetNode(copy.getFirstNode().value);
this.firstNode=firstNode;
firstNode.add(copy.getFirstNode().next.value);
this.firstNode = firstNode;
firstNode.add(copy.getFirstNode().next.next.value);
this.firstNode = firstNode;
and this Works perfectly(but I knew in advance i'm testing with only 3 element list).How would i do it with a while loop without using such a statement as :
copy.firstNode = copy.firstNode.next;
I have to somehow move along the "copy" list ?
It's hard to say what the problem is without seeing the source for SortedLinkedSetNode, but you seem to be modifying your original with this statement:
copy.firstNode= copy.firstNode.next;
This probably advances firstNode to the end of your original linkedset, resulting in the original having one element. Also, confusingly the original is called "copy". You may want to rename it so you can understand your code better.
When creating a deep copy, you shouldn't modify the structure you want to copy.
In this case, you can just use a temporary variable to store the reference to the current node you are on, without modifying your original data structure. Try this:
this.firstNode = firstNode1;
// so basically I am chaining elements from "copy" to firstNode1 and then making "this" = to firstNode1.
SortedLinkedSetNode currentNode = copy.firstNode;
while (currentNode.next !=null) {
firstNode1.add(currentNode.next.value);
this.firstNode = firstNode1;
currentNode = currentNode.next;
}
First the original to copy from, is called copy, like in do copy this one?
You did have some mix-up with the correct nodes and the code was not kept simple enough.
A recursive solution to simplify things seems appropriate:
public SortedLinkedSet(SortedLinkedSet<T> original) {
Objects.requireNotNull(original);
this.firstNode = copyNodes(orignal.firstNode);
}
private SortedLinkedSetNode copy(SortedLinkedSetNode originalNode) {
if (originalNode == null) {
return null;
}
SortedLinkedSetNode node = new SortedLinkedSetNode(originalNode.value);
node.next = copy(originalNode.next);
return node;
}
If the node's value would need deep copying too, that could be done at one place.
A loop would still be simple. One way:
private SortedLinkedSetNode copy(SortedLinkedSetNode originalNode) {
SortedLinkedSetNode firstNode = null;
SortedLinkedSetNode previousNode = null;
while (originalNode != null) {
SortedLinkedSetNode node = new SortedLinkedSetNode(originalNode.value);
if (firstNode == null) {
firstNode = node;
} else {
previousNode.next = node;
}
previousNode = node;
originalNode = originalNode.next;
}
return firstNode;
}
Hi I'm working on an assignment about hash table. Everything is fine except for the remove method.
Here's code:
public boolean remove(K key) throws HashTableException {
//
//IMPLEMENT THIS FUNCTION
//
if (key == null)
throw new HashTableException("Null keys not allowed in the hash table");
int index = getIndex(key);
HashTableNode<K,V> node = FindNode(key,index);
if (node == null) {
return false;
} else {
if (node.getNext() == null) {
node = null;
} else {
node = node.getNext();
}
return true;
}
}
It doesn't remove the key at all. Can anyone help me please? thank you!
node = null; does not "delete" the node, it just sets the value of the variable node in this method to null. It does nothing to the actual node that is in the hashtable somewhere.
And in the next "else" you then have node = node.getNext(); which, again, only change the node variable in this method. But since you return from the method without doing anything more with this variable, all this does is nothing since node is a local variable that only exists in this method.
You should read up on the concepts of local variables and references in java, which would probably lead to an understanding of why this is not working :)
node = node.getNext();
By this line, I think you are just traversing instead of re-writing the Next pointer
Maybe you should try node.getPrev().setNext = node.getNext();
if you can find the previous node and be able to set the next node.
Please make sure hashcode() and equals() are overridden correctly.