0

EDIT: I believe the problem is in adding nodes. When I start the fetch the nodes they come back null. I believe it is because head is not get setting as the impl.getHeadNode() == null never occurs.

Constructor:

 public CashDonationLinkedListImpl() {
 iterNode = headNode = new CashDonationNode(null); 
}

Add Method:

public void add(Object obj) {
    CashDonation cashDonation = (CashDonation)obj; 
    CashDonationLinkedListImpl impl = new CashDonationLinkedListImpl(); 

    CashDonationNode node = new CashDonationNode();
        node.setCashDonation(cashDonation); 
        //node.setNext(null);
    if(impl.getHeadNode() == null){

        System.out.println("Head node"); 
        impl.setHeadNode(node); 

      }  
      else{
        //node.setCashDonation(cashDonation); 
        //node.setNext(null); 
        for (CashDonationNode cashNode = headNode;cashNode.getNext() != null;cashNode = cashNode.getNext()) {
           // currNode.setNext(null);
        }        

            //cashNode.setCashDonation(cashDonation);

        cashNode.setNext(node);

      }

Original: The getter returns two zeros while the setter takes in 100, 100, and 10000. I'm sure I'm forgetting to do something but can't put my finger on it. Why isn't the getter returning the values from the setter?

public class CashDonationNode {
    CashDonation cashDonation; 
    CashDonationNode next; 

    public CashDonationNode() {
      cashDonation = new CashDonation();
      next = null; 
    }

    public CashDonationNode(CashDonation cashDonation, CashDonationNode next) {
        this.cashDonation = cashDonation;
        this.next = next;
    }

    public CashDonationNode getNext(){
        return next; 
    }
    public void setNext(CashDonationNode nextValue){
        this.next = nextValue;
    }
    public void setCashDonation(CashDonation cashDonation){
        this.cashDonation =  cashDonation;
        System.out.print("node storage");
        System.out.println(this.cashDonation.getDonationAmount()); 
    }

    public CashDonation getCashDonation(){
        System.out.println("node retrieval " + cashDonation.getDonationAmount()); 
                return this.cashDonation; 
    }
}

Snippets from the Linked List below. Returns the same zeros:

CashDonationNode iterNode = new CashDonationNode(); //declared in class
public void iterator() {
  iterNode = headNode;
}

@Override
public CashDonation next() {
CashDonation cashDonation = iterNode.getCashDonation();
System.out.println("Impl: " + cashDonation.getDonationAmount()); 
  iterNode = iterNode.getNext();
  return cashDonation;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Nothing sticks out here. Please include the code that exercises this class plus the `CashDonation` class (and remove any irrelevant code). A [mcve], so we can see the problem for ourselves. – John Kugelman Oct 30 '15 at 03:35
  • Not sure what all would be appropriate to include. The node is part of a Linked List, called by a Manager. The manager is called from a portion of the Main Class. CashDonation class was working fine when dealing with arrays and haven't changed it since.Start with including the portions of those dealing with next()? – Nicholas Yount Oct 30 '15 at 03:56
  • @Nicholas Yount Can you provide the `CashDonation` class? so that we can figure out why the getter returns the unexpected value. – Thanigai Arasu Oct 30 '15 at 05:09

1 Answers1

0

Corrected add():

public void add(Object obj) {
    CashDonation cashDonation = (CashDonation)obj; 
    CashDonationNode node = new CashDonationNode(cashDonation);
    this.setIterNode(node);

    if(this.getHeadNode() == null){
        this.setHeadNode(node);
        node.setNext(null); 
      }  
      else{
        CashDonationNode cashNode; 
        for (cashNode = headNode;cashNode.getNext() != null;cashNode = cashNode.getNext()) {
        }        
        cashNode.setNext(node);
      }
    numElements++; 

}