Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8548771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:34:19+00:00 2026-06-11T13:34:19+00:00

I am trying to make a program in Java that uses multithreading. This program

  • 0

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:34:21+00:00Added an answer on June 11, 2026 at 1:34 pm

    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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a program in Java that checks for three specific
I'm trying to make a program in Java that can read, compile, and run
I'm trying to make a bash script that will talk to a java program
I'm trying to develop a Java program that does the following: uses a background
I'm trying to make java program that checks ip addresses and it's ports and
I'm trying to make a little java program that executes a system command but
I'm new at Java GUI and I'm trying to make a program that shows
Okay, so what I'm trying to do is create a java program that uses
I'm trying to make a Java program more Groovy. The java code reads an
Im trying to make a program that retrieves an endless amount of numbers that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.