我得到Null Pointer Exception,无法在这里找到错误

时间:2017-12-30 13:07:31

标签: java

从今天早上起整天都在处理这段代码,这是一本在线书籍的演示  由于某种原因在main()处获得空指针异常 并且逻辑似乎有点不起作用, 有人可以花点时间 天哪找不到问题 感谢帮助人们

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));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题在于您的Dollar10Dispenser,chain实例的c3对象为空。由于这是最后一个链,所以我认为你需要在这段代码中更新else块的逻辑。

DemoChainResponsibilityPattern1 :没有为c3调用nextChain()方法,因此chain对象为空

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 :一旦余数为0,它就试图拨打下一个链式分配器

    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);
        }
    }