-4

working on this code since this morning all day, it a demo from a book online for some reason getting null pointer exception at main() and also logic seems bit not working, can someone spend bit of his time gosh cant find the problem there thanks for help people

class Currency {
    private int amount;

    public Currency(int a) {
        this.amount = a;
    }

    public int getAmount() {
        return amount;
    }
}

interface DispenseChain {
    public void setNextChain(DispenseChain nextChain);

    public void dispense(Currency cur);
}

class Dollar50Dispenser implements DispenseChain {
    private DispenseChain chain;

    @Override
    public void setNextChain(DispenseChain nextChain) {
        this.chain = nextChain;
    }

    @Override
    public void dispense(Currency cur) {
        if (cur.getAmount() >= 50) {
            int num = cur.getAmount() / 50;
            int remainder = cur.getAmount() % 50;
            System.out.println("Dispensing " + num + " 50$ note");
            if (remainder != 0) {
                chain.dispense(new Currency(remainder));
            } else {
                chain.dispense(cur);
            }
        }
    }
}

class Dollar20Dispenser implements DispenseChain {
    private DispenseChain chain;

    @Override
    public void setNextChain(DispenseChain nextChain) {
        this.chain = nextChain;
    }

    @Override
    public void dispense(Currency cur) {
        if (cur.getAmount() >= 20) {
            int num = cur.getAmount() / 20;
            int remainder = cur.getAmount() % 20;
            System.out.println("Dispensing " + num + " 20$ note");
            if (remainder != 0) {
                chain.dispense(new Currency(remainder));
            } else {
                chain.dispense(cur);
            }
        }
    }
}

class Dollar10Dispenser implements DispenseChain {
    private DispenseChain chain;

    @Override
    public void setNextChain(DispenseChain nextChain) {
        this.chain = nextChain;
    }

    @Override
    public void dispense(Currency cur) {
        if (cur.getAmount() >= 10) {
            int num = cur.getAmount() / 10;
            int remainder = cur.getAmount() % 10;
            System.out.println("Dispensing " + num + " 10$ note");
            if (remainder != 0) {
                chain.dispense(new Currency(remainder));
            } else {
                chain.dispense(cur);
            }
        }
    }
}

class DemoChainResponsibilityPattern1 {
    private DispenseChain c1;
    public DemoChainResponsibilityPattern1() {
        c1 = new Dollar50Dispenser();
        DispenseChain c2 = new Dollar20Dispenser();
        DispenseChain c3 = new Dollar10Dispenser();
        // set the the chain of responsibility
        c1.setNextChain(c2);
        c2.setNextChain(c3);
    }

    public static void main(String[] args) {
        DemoChainResponsibilityPattern1 dispenser = new DemoChainResponsibilityPattern1();
        while (true) {
            int amount = 0;
            System.out.println("Enter amount to dispense: ");
            Scanner input = new Scanner(System.in);
            amount = input.nextInt();
            if (amount % 10 != 0) {
                System.out.println("amount must be in multiple of 10s");
                return;
            }
            // process the request
            dispenser.c1.dispense(new Currency(amount));
        }
    }
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Ilhom
  • 103
  • 1
  • 6
  • 3
    could you share the stack trace of this NPE? – Kamil Jarosz Dec 30 '17 at 13:11
  • Welcome to Stack Overflow! Please take the [tour](/tour), have a look around, and read through the [help center](/help) , in particular [How do I ask a good question?](/help/how-to-ask) and [What topics can I ask about here?](/help/on-topic). From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." – Timothy Truckle Dec 30 '17 at 13:16
  • seems everyone teaches here how to live the life, i need codes not talks, ill find free talks from other places, i neeed codes, CODES CODES – Ilhom Jan 02 '18 at 14:48

1 Answers1

0

Issue is with your Dollar10Dispenser, chain object of c3 instance is null. As this is the last chain, so I think you need to update your logic for else block in this piece of code.

DemoChainResponsibilityPattern1: nextChain() method is not called for c3, so chain object is null

public DemoChainResponsibilityPattern1() {
    c1 = new Dollar50Dispenser();
    DispenseChain c2 = new Dollar20Dispenser();
    DispenseChain c3 = new Dollar10Dispenser();
    //set the the chain of responsibility
    c1.setNextChain(c2);
    c2.setNextChain(c3);
}

Dollar10Dispenser: once remainder is 0, it's trying to call the next chain dispenser

    if (cur.getAmount() >= 10) {
        int num = cur.getAmount() / 10;
        int remainder = cur.getAmount() % 10;
        System.out.println("Dispensing " + num + " 10$ note");
        if (remainder != 0) {
            chain.dispense(new Currency(remainder));
        } else {
            chain.dispense(cur);
        }
    }
sayboras
  • 4,897
  • 2
  • 22
  • 40
  • your ans did help but im stuck again, now every time it keeps dispensing double amount, ) – Ilhom Dec 31 '17 at 10:42
  • i am just pointing why NPE is coming, the logic is up to you. If it keeps dispensing double amount, you need to debug your flow/logic. – sayboras Dec 31 '17 at 12:52
  • thank you brother, i keep tried and keep trying to work on logic, ) it would be nice to see some code there not just talks ))) no worries ill solve it one day, its just gonna take some time, the reason i posted it on here, i thought someone would come up with Solution not just talks and abstract direction, – Ilhom Jan 01 '18 at 13:36
  • I shouldn't have answered this question in the first place, as it's not following guideline. You can check the comment above by Timothy. Let me remove my answer shortly, and welcome to stackoverflow. – sayboras Jan 01 '18 at 13:45