-1

In this assignment I am supposed to have three threads, these threads are supposed to count up from 0, printing the count each second and printing messages at seven and fifteen seconds.

I can't figure out my error, I think it is that my threads don't know what they are referring to but I'm not sure.

The error:

Exception in thread "Thread-0" Exception in thread "Thread-1" Exception in thread "Thread-2" java.lang.NullPointerException
at TimePrinter.run(TimePrinter.java:7)
at java.lang.Thread.run(Unknown Source)

java.lang.NullPointerException
at MessagePrinter2.run(MessagePrinter2.java:8)
at java.lang.Thread.run(Unknown Source)

java.lang.NullPointerException
at MessagePrinter1.run(MessagePrinter1.java:8)
at java.lang.Thread.run(Unknown Source)


public class Counter{ 
    private int counter;
    private Lock counterLock; 
    private Condition sevenCondition;
    private Condition fifteenCondition;
    public Counter() {
        counter = 0;
        counterLock = new ReentrantLock();
        sevenCondition = counterLock.newCondition(); 
        fifteenCondition = counterLock.newCondition();
    }
    public void incrementCounter(){
        counterLock.lock();
        try{

            counter++;
            if (counter%7 == 0) 
                sevenCondition.signal();
            //else sevenCondition.await();
            if (counter%15 == 0) 
                fifteenCondition.signal();
            //else fifteenCondition.await();
            System.out.print(" " + counter + " ");

        } finally {counterLock.unlock();}
    }

    public void sevenSecondMessage()throws InterruptedException{
        counterLock.lock();
        try{
            while (counter%7 != 0){
                sevenCondition.await();
            System.out.println("Seven Second Message");
            }
        } finally {counterLock.unlock();}
    }

    public void fifteenSecondMessage() throws InterruptedException{
        counterLock.lock();
        try{
            while (counter%15 != 0){
                fifteenCondition.await();
            System.out.println("Fifteen Second Message");
            }
        } finally {counterLock.unlock();}
    }
}


public class TimePrinter implements Runnable{
        private Counter counter;
        public synchronized void run(){
            try {
                counter.incrementCounter();

                Thread.sleep(1000);
            } catch (InterruptedException e){}
        }
    }



    public class MessagePrinter1 implements Runnable{
    private Counter counter;
    public synchronized void run(){
        try {

            counter.sevenSecondMessage();

            }catch (InterruptedException e){}

        } 
    }

    public class MessagePrinter2 implements Runnable{
        private Counter counter;
        public synchronized void run(){
            try {

                counter.fifteenSecondMessage();

                }catch (InterruptedException e){}

            } 
        }

    public class Main2 {

    public static void main(String[] args){

        Runnable timeprinter = new TimePrinter();
        Runnable message1 = new MessagePrinter1();
        Runnable message2 = new MessagePrinter2();

        new Thread(timeprinter).start();
        new Thread(message1).start();
        new Thread(message2).start();
    }
}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
BSouth
  • 1
  • 1

1 Answers1

1
public class TimePrinter implements Runnable{
    private Counter counter;
    public synchronized void run(){
        try {
            counter.incrementCounter();

            Thread.sleep(1000);
        } catch (InterruptedException e){}
    }
}

counter is never initialized. You need counter = new Counter() somewhere.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578