Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.31/5 (3 votes)
See more:
Hello,

I have a problem with my Sleeping Barber problem code, and have looked at it for almost 12 hours now, and can't see whats wrong (maybe just a small mistake, that I could't see).

I will appreciate if anyone will look at it, and give me a hint about whats wrong :)

When i run the program, it says:

java.lang.NullPointerException
at BarberShop.barber(BarberShop.java:20) - the wait() method
at BarberShop.run(BarberShop.java:64) -
at BarberShop.main(BarberShop.java:55)

The code can be seen here on this link:

Java
public class BarberShop
{
    private Barber barber;
    private Customer customer;
    private int waiting;
    private int chairs;
    private boolean isAvailable;
 
    public BarberShop()
    {
        waiting = 0;
        chairs = 5;
    }
 
    public void barber()
    {
        synchronized(this) {
            while(true) {
                try {
                    customer.wait();
                }
                catch (InterruptedException exception) {
                }
 
                waiting--;
                this.notify();
                barber.cutHair();
            }
        }
    }
 
    public void customer()
    {
        synchronized(this) {
            if(waiting < chairs) {
                waiting++;
                this.notify();
 
                try {
                    barber.wait();
                }
                catch (InterruptedException exception) {
                }
                customer.getHairCut();
            }
            else {
                //shop is full
            }
        }
    }
   
    public static void main(String args[])
    {
        BarberShop barberShop = new BarberShop();
        barberShop.run();
    }
 
    public void run()
    {
        final int barbers = 1;
 
        for(int i = 0 ; i < barbers ; i++) {
            barber = new Barber();
            barber();
        }
 
        int customerID = 1;
 
        while(true) {
            customer = new Customer(customerID++);
            customer();
 
            //             try {
            //                 //sleep();
            //             }
            //             catch (InterruptedException exception) {
            //             }
        }
 
    }
}


- Dennis
Posted
Updated 8-Apr-14 5:14am
v5
Comments
[no name] 8-Apr-14 10:56am    
No one is going to download your unknown code from an unknown source to debug it for you. Describe your problem and post the code that is relevant to your problem.
Sergey Alexandrovich Kryukov 9-Apr-14 0:50am    
The key here (or one of the key problems) is the wait method (Customer.wait); and you are not showing it.
So far, the whole question does not make sense.
—SA

You are calling customer.wait() before the initialisation of the customer object. It is not easy to understand your code owing to the use of the same name for an object and a method.
 
Share this answer
 
You Are Not Initializing The Customer Object To Customer Reference Type

Thats Why You Are Getting The Null Pointer Exception

For Every Object Default Literal Is 'null' value


Initialize You Customer Object Then Your Problem Will Solve
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900