3

I am trying to make a program in Java that uses multithreading. This program is about a bank account shared between husband and wife who use debit cards.

I wrote the Java program using 2 threads but i am getting java.lang.NullPointerException

I am trying to buildup my knowledge in Java multithreading. Help me in identifying the mistake in this code block.

Here is the program :

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;


// A bank account has a balance that can be changed by deposits and withdrawals.

public class BankAccount {

    public double total = 0, amount = 0;

    public void withdraw(double amount) {
        total -= amount;
        System.out.println("Amount Withdrawn is " + amount);
    }

    public void deposit(double amount) {
        total += amount;
        System.out.println("Amount Deposited is " + amount);
    }

    public double getAccount() {
        System.out.println("Total Amount is " + total);
        return total;

    }
}

class WithdrawalRunnable {

    private static final Lock lock = new ReentrantLock();
    final Condition myCond = lock.newCondition();
    BankAccount myAccount;

    public void amountWithdrawn(double money_Withdrawn) throws InterruptedException {
        lock.lock();
        try {
            while (money_Withdrawn > myAccount.total) {
                myCond.await();  
                //when the condition is satisfied then :
                myAccount.withdraw(money_Withdrawn);
                myAccount.getAccount();
            }
        } finally {
            lock.unlock();
        }
    }
}

class DepositRunnable {

    private static final Lock lock = new ReentrantLock();
    final Condition myCond = lock.newCondition();
    BankAccount myAccount;

    public void amountDeposited(double money_deposited) throws InterruptedException {
        lock.lock();
        try {
            myAccount.deposit(money_deposited);
            myAccount.getAccount();
            myCond.signalAll(); 
        } finally {
            lock.unlock();
        }
    }
}

class Husband implements Runnable {

    DepositRunnable myDeposit;
    WithdrawalRunnable myWithdrawal;
    double amount_deposit;
    double amount_withdraw;

    public Husband(double amount_deposit, double amount_withdraw) {
        this.amount_deposit = amount_deposit;
        this.amount_withdraw = amount_withdraw;
    }

    public void run() {
        try {
            myDeposit.amountDeposited(amount_deposit);
            myWithdrawal.amountWithdrawn(amount_withdraw);
        } catch (InterruptedException ex) {
            Logger.getLogger(Wife.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

class Wife implements Runnable {

    DepositRunnable myDeposit;
    WithdrawalRunnable myWithdrawal;
    double amount_deposit;
    double amount_withdraw;

    public Wife(double amount_deposit, double amount_withdraw) {
        this.amount_deposit = amount_deposit;
        this.amount_withdraw = amount_withdraw;
    }

    public void run() {
        try {
            myDeposit.amountDeposited(amount_deposit);
            myWithdrawal.amountWithdrawn(amount_withdraw);
        } catch (InterruptedException ex) {
            Logger.getLogger(Wife.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

class RunningThreadTest {

    public static void main(String[] args) throws InterruptedException {

        Husband husb = new Husband(100.0, 0.0);
        Wife wif = new Wife(400.0, 0.0);
        Thread thread1 = new Thread(husb);
        Thread thread2 = new Thread(wif);
        thread1.start();
        thread2.start();
    }
}
2
  • 2
    What line is generating the NPE? Commented Sep 19, 2012 at 0:55
  • Exception in thread "Thread-1" java.lang.NullPointerException at javaapplication3.todelete.Wife.run(BankAccount.java:116) at java.lang.Thread.run(Thread.java:662) Exception in thread "Thread-0" java.lang.NullPointerException at javaapplication3.todelete.Husband.run(BankAccount.java:94) at java.lang.Thread.run(Thread.java:662) Commented Sep 19, 2012 at 1:00

6 Answers 6

2

It is a good study case for multithread, I changed your code a little. Please compare BankAccountManager class with your two classes, I hope this would help.

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;

// A bank account has a balance that can be changed by deposits and withdrawals.

class BankAccount {

    private double total = 0;

    public void withdraw(double amount) {
        total -= amount;
    }

    public void deposit(double amount) {
        total += amount;
    }

    public double getAccount() {
        return total;

    }
}

class BankAccountManager {

    private static final Lock lock = new ReentrantLock();
    private Condition myCond = lock.newCondition();
    BankAccount myAccount;

    public BankAccountManager(BankAccount myAccount) {
        this.myAccount = myAccount;
    }

    public void amountWithdrawn(double money_Withdrawn)
            throws InterruptedException {
        lock.lock();
        try {
            if (money_Withdrawn > myAccount.getAccount()) {
                myCond.await();
            }
            // when the condition is satisfied then :
            System.out.println("Amount Withdrawn is " + money_Withdrawn);
            myAccount.withdraw(money_Withdrawn);
            System.out.println("Total Amount is " + myAccount.getAccount());
        } finally {
            lock.unlock();
        }
    }

    public void amountDeposited(double money_deposited)
            throws InterruptedException {
        lock.lock();
        try {
            System.out.println("Amount Deposited is " + money_deposited);
            myAccount.deposit(money_deposited);
            System.out.println("Total Amount is " + myAccount.getAccount());
            myCond.signalAll();
        } finally {
            lock.unlock();
        }
    }
}

class Husband implements Runnable {

    private BankAccountManager manager;
    private double amount_deposit;
    private double amount_withdraw;

    public Husband(BankAccountManager manager, double amount_deposit,
            double amount_withdraw) {
        this.manager = manager;
        this.amount_deposit = amount_deposit;
        this.amount_withdraw = amount_withdraw;
    }

    public void run() {
        try {
            manager.amountDeposited(amount_deposit);
            manager.amountWithdrawn(amount_withdraw);
        } catch (InterruptedException ex) {
            Logger.getLogger(Wife.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

class Wife implements Runnable {

    private BankAccountManager manager;
    private double amount_deposit;
    private double amount_withdraw;

    public Wife(BankAccountManager manager, double amount_deposit,
            double amount_withdraw) {
        this.manager = manager;
        this.amount_deposit = amount_deposit;
        this.amount_withdraw = amount_withdraw;
    }

    public void run() {
        try {
            manager.amountDeposited(amount_deposit);
            manager.amountWithdrawn(amount_withdraw);
        } catch (InterruptedException ex) {
            Logger.getLogger(Wife.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

public class RunningThreadTest {

    public static void main(String[] args) throws InterruptedException {

        BankAccountManager manager = new BankAccountManager(new BankAccount());

        Husband husb = new Husband(manager, 100.0, 0.0);
        Wife wif = new Wife(manager, 400.0, 1.0);
        Thread thread1 = new Thread(husb);
        Thread thread2 = new Thread(wif);
        thread1.start();
        thread2.start();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi goodsong,thanks for your solution.Your solution is working better than mine. I am having incorrect answer when withdrawing and still didn't figure it out yet.Maybe i think that I am not suppose to have same variable "amount" used as data field, for both deposit and withdraw, but still not sure about it. yes your solution is working without a glitch , I already try it and time to reverse engineer it ! :)
1

the field MyAccount in both Deposit and Withdrawal Runnables are not initialized

Comments

1

What I'm seeing is that the attribute

BankAccount myAccount;

That appears in the classes DepositRunnable and WithdrawalRunnable is not being correctly initialized, hence you'll get a null pointer exception for example here:

myAccount.withdraw(money_Withdrawn);

Comments

1

In both DepositRunnable and WithdrawalRunnable, you never initialize the member myAccount, but you attempt to use them in their respective methods. Try initializing those members before you use them.

BankAccount myAccount = new BankAccount();

There may be more examples, but those are the first ones I would look at.

1 Comment

Nope, Initializing myAccount in both classes didn't solve the problem, I am getting :Exception in thread "Thread-1" java.lang.NullPointerException at javaapplication3.todelete.Wife.run(BankAccount.java:116) at java.lang.Thread.run(Thread.java:662) Exception in thread "Thread-0" java.lang.NullPointerException at javaapplication3.todelete.Husband.run(BankAccount.java:94) at java.lang.Thread.run(Thread.java:662)
1

Many of your class member variables are not being initialized. Your specific problem is because Wife.myDeposit has never been initialized, but once you get past that there will just be another NPE waiting for you.

Comments

0

Yes Thank you , all these classes need to be initialized as follows :

DepositRunnable myDeposit = new DepositRunnable();
WithdrawalRunnable myWithdrawal = new WithdrawalRunnable();
BankAccount myAccount = new BankAccount();

And the other mistake that nobody catch it is : "total" must be static .

Thank you everyone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.