Java Bean null point exception & invocation target exception

281 views Asked by At

I'm getting a null point exception and an invocation target exception with the following bean. The program is essentially to test putting deposit and withdrawals in a balance and also include a transaction ID. I'm getting those exceptions in the client code.

import BankBeanRemotePkg.BankBeanRemote;
import javax.ejb.Stateful;
import java.math.BigDecimal;
import java.util.Random;
import javax.ejb.Remote;
import javax.interceptor.Interceptors;

/**
 *
 * @author Aneel
 */
@Stateful
@Remote(BankBeanRemotePkg.BankBeanRemote.class)
@Interceptors(BankBeanPkg.BankCallBacks.class)

public class BankBean implements BankBeanRemote {

    private BigDecimal balance;
    @Override
    public BigDecimal initial() {
        BigDecimal initialbal = new BigDecimal("0.00");
        return initialbal.setScale(2, BigDecimal.ROUND_UP);
    }

    @Override
    public BigDecimal Current() {
        return balance.setScale(2, BigDecimal.ROUND_UP);
    }

    @Override
    public int Deposit(BigDecimal amt) {
        BigDecimal updatedBal = amt.add(balance);
        balance = updatedBal;
        Random randomGenerator = new Random();
        int randomID = randomGenerator.nextInt(1000);
        return randomID;
    }

    @Override
    public int Withdraw(BigDecimal amt) {
        BigDecimal updatedBal = balance.subtract(amt);
        balance = updatedBal;
        Random randomGenerator = new Random();
        int randomID = randomGenerator.nextInt(1000);
        return randomID;
    }
}

Client code which just test the simple bank transaction is:

import BankBeanRemotePkg.BankBeanRemote;
import java.math.BigDecimal;


public class BankClient {

    private static BankBeanRemote bankBean;
    public BankClient (String[] args) {
    }

    public static void main(String[] args) {
        BankClient client = new BankClient(args);
        client.test();
    }

    public void test(){
        try {
            BigDecimal initial = bankBean.initial();
            System.out.println("Initial Balance:" + initial);
            BigDecimal deposit = new BigDecimal("5.00");
            int transactionid = bankBean.Deposit(deposit);
            System.out.println("Deposit amt:" + deposit + "Transaction ID:" + transactionid);
            BigDecimal balance = bankBean.Current();
            System.out.println("Current Balance:" + balance);
            BigDecimal withdraw = new BigDecimal("3.00");
            int transactionid2 = bankBean.Withdraw(withdraw);
            System.out.println("Withdraw amt:" + withdraw + "Transaction ID:" + transactionid2);
            System.out.println("Current Balance:" + balance);
        } catch (Exception ex) {
            System.err.println("Caught an unexpected exception!");
        }

    }
}

Anyone know how I can fix my code?

Thanks

1

There are 1 answers

0
hfontanez On BEST ANSWER

You didn't properly instantiate bankBean prior to calling initial() method. You have to do something like this:

// Obtain InitialContext (ctx) object
BankBeanRemote bankBean = ctx.lookup("BankBean");
....
BigDecimal initial = bankBean.initial();

This should get rid of your NPE.