title: Initializing a semaphore as null in running class
body: <p>I'm trying to setup a simple Semaphore with 0 and 1, where a wait() function and a Signal() function are there to allow only a thread in the critical section.</p>
  
  <pre><code>public class Producer extends BaseThread
  {
      Semaphore semaphore = null;
      private char block; // block to be returned
  
      public Producer(Semaphore x)
      {
          this.semaphore = x;
      }
      public Producer()
      {
          // empty constructor
      }
      public void run()
      {
  
          try{
              this.semaphore.wait(); // enter the critical section
              for (int i = 0; i &lt; 3; i++){
                  this.block = StackManager.stack.pick();
                  StackManager.stack.pick(); // return value on top
                  StackManager.stack.push(block++); // pushes the next char value on the stack
              }
          }
          catch(Exception e){
              allException(e);
              System.exit(1);
          }
          finally
          {
              this.semaphore.Signal(); // end critical section
          }
  
      }
  }
  </code></pre>
  
  <p>The semaphore is declared in main as follows:</p>
  
  <pre><code> static Semaphore lock = new Semaphore(-1); // create semaphore
   Consumer consumerSemaphore = new Consumer(lock);
   Producer producerSemaphore = new Producer(lock);
  </code></pre>
  
  <p>Where the producer and consumer semaphores are supposed to alternate in main to not cause any anomalies in the stack. (I omitted the Consumer class as it is essentially written the same way as Producer, but doing pop() instead).</p>
  
  <p>Now the problem here is that I keep running into NullPointerException errors for both Consumer and Producer classes when running my code, which I can't seem to find an answer to. What am I missing here?</p>
  
  <p>EDIT: 
  The code should be pushing the next char value of the top of StackManager.stack, but since there's another class called Consumer also doing as many pop() operations, they need to be executing synchronized. I can't use anything else but the Semaphore, which is this class I've created:</p>
  
  <pre><code>class Semaphore {
      private int value;
  
      public Semaphore(int value) {
          this.value = value;
      }
  
  
      public Semaphore() {
          this(0);
      }
  
  
      public synchronized void Wait() {
          while (this.value &gt;= 0) { //changed to greater or equals since we're doing the opposite with natural numbers
              try {
                  wait();
              }
              catch(InterruptedException e) {
                  System.out.println ("Semaphore::Wait() - caught InterruptedException: " + e.getMessage() );
                  e.printStackTrace();
              }
          }
          this.value++; // the semaphore value gets incremented since it needs to go to zero from -1 or lower
      }
  
      public synchronized void Signal() {
          --this.value; // signal decrements the value since it brings the lock back to -1 for the next wait operation
          notify();
      }
  
      public synchronized void P() {
          this.Wait();
      }
  
      public synchronized void V() {
          this.Signal();
      }
  }
  </code></pre>
  
  <p>Notice the semaphore is initialized in main() as -1, since for the complexity of the course, we need to create a Semaphore class where at -1 means ready for next thread and 0 means some thread is in its critical section. That means the wait() method I wrote increments and Signal decrements.</p>
  
  <p>Edit 2: Added Consumer class</p>
  
  <pre><code>public class Consumer extends BaseThread {
      Semaphore semaphore = null;
      private char copy; // A copy of a block returned by pop()
  
      public Consumer(Semaphore x) {
          this.semaphore = x;
      }
  
      public Consumer() {
          // empty constructor
      }
  
      public void run() {
          try {
              this.semaphore.wait(); // enter the critical section
  
              System.out.println("Consumer thread [TID=" + this.iTID + "] starts executing.");
              for (int i = 0; i &lt; StackManager.iThreadSteps; i++) {
                  this.copy = StackManager.stack.pop(); // pop the top of the stack
                  System.out.println("Consumer thread [TID=" + this.iTID + "] pops character =" + this.copy);
              }
              System.out.println("Consumer thread [TID=" + this.iTID + "] terminates.");
          } catch (Exception e) {
              allException(e);
              System.exit(1);
          } finally {
              this.semaphore.Signal(); // end the critical section
  
          }
      }
  }
  </code></pre>
  
  <p>EDIT 3:</p>
  
  <pre><code>class StackManager {
      // The Stack
      public static CharStack stack = new CharStack();
      private static final int NUM_ACQREL = 4; // Number of Producer/Consumer threads
      private static final int NUM_PROBERS = 1; // Number of threads dumping stack
      public static int iThreadSteps = 3; // Number of steps they take
      // Semaphore declarations. Insert your code in the following:
      static Semaphore lock = new Semaphore(-1); // create semaphore
      Consumer consumerSemaphore = new Consumer(lock);
      Producer producerSemaphore = new Producer(lock);
      CharStackProber stackProberSemaphore = new CharStackProber(lock);
      // The main
  
  
      public static void main(String[] argv)
      {
          // Some initial stats...
          try
          {
              System.out.println("Main thread starts executing.");
              System.out.println("Initial value of top = " + stack.getTop() + ".");
              System.out.println("Initial value of stack top = " + stack.pick() + ".");
              System.out.println("Main thread will now fork several threads.");
          }
          catch(CharStackEmptyException e)
          {
              System.out.println("Caught exception: StackCharEmptyException");
              System.out.println("Message : " + e.getMessage());
              System.out.println("Stack Trace : ");
              e.printStackTrace();
          }
                      /*
                     * The birth of threads
                      */
  
  
          Consumer ab1 = new Consumer();
          Consumer ab2 = new Consumer();
          System.out.println ("Two Consumer threads have been created.");
          Producer rb1 = new Producer();
          Producer rb2 = new Producer();
          System.out.println ("Two Producer threads have been created.");
          CharStackProber csp = new CharStackProber();
          System.out.println ("One CharStackProber thread has been created.");
                    /*
                   * start executing
                    */
          ab1.start();
          rb1.start();
          ab2.start();
          rb2.start();
          csp.start();
                   /*
                    * Wait by here for all forked threads to die
                   */
          try
          {
              ab1.join();
              ab2.join();
              rb1.join();
              rb2.join();
              csp.join();
              // Some final stats after all the child threads terminated...
              System.out.println("System terminates normally.");
              System.out.println("Final value of top = " + stack.getTop() + ".");
              System.out.println("Final value of stack top = " + stack.pick() + ".");
              System.out.println("Final value of stack top-1 = " + stack.getAt(stack.getTop() - 1) + ".");
              System.out.println("Stack access count = " + stack.getAccessCounter());
          }
          catch(InterruptedException e)
          {
              System.out.println("Caught InterruptedException: " + e.getMessage());
              System.exit(1);
          }
          catch(Exception e)
          {
              System.out.println("Caught exception: " + e.getClass().getName());
              System.out.println("Message : " + e.getMessage());
              System.out.println("Stack Trace : ");
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>The stacktrace is as follow:</p>
  
  <pre><code>    Main thread starts executing.
  Initial value of top = 3.
  Initial value of stack top = d.
  Main thread will now fork several threads.
  Two Consumer threads have been created.
  Two Producer threads have been created.
  One CharStackProber thread has been created.
  Caught exception : java.lang.NullPointerException
  Caught exception : java.lang.NullPointerException
  Message          : null
  Caught exception : java.lang.NullPointerException
  Caught exception : java.lang.NullPointerException
  Caught exception : java.lang.NullPointerException
  Message          : null
  Stack Trace      : 
  Stack Trace      : 
  Message          : null
  Stack Trace      : 
  Message          : null
  Stack Trace      : 
  Message          : null
  Stack Trace      : 
  java.lang.NullPointerException
      at Producer.run(Producer.java:18)
  java.lang.NullPointerException
      at CharStackProber.run(CharStackProber.java:18)
  java.lang.NullPointerException
      at Consumer.run(Consumer.java:19)
  java.lang.NullPointerException
      at Consumer.run(Consumer.java:19)
  </code></pre>
  
  <p>For the sake of keeping this post shorter, I've omitted the CharStackProber class as it is just a copy of Producer, where the run() method is exactly the same but the inside of the for-loop is only to print out the elements of the StackManager.stack</p>