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:
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 {
}
}
}
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();
}
}
}
- Dennis