This discussion is archived
11 Replies Latest reply: Mar 9, 2010 8:14 AM by 3004 RSS

Exception in thread "Producer" java.lang.NullPointerException Error

843789 Newbie
Currently Being Moderated
I am trying to implement the producer consumer problem by creating my own semaphores. When compiling and running the program I am getting the error :

Exception in thread "Producer" java.lang.NullPointerException
     at Producer.run(Producer.java:23)
Exception in thread "Consumer" java.lang.NullPointerException
     at Consumer.run(Consumer.java:24)

I know that this has something to do with the methods valueConsumed.P() and valueProduced.P(). Did I not implement these methods right in the Producer, Consumer, and Semaphore Classes?

Producer
public class Producer extends Thread{
     private Buffer sharedLocation;
     private Semaphore valueProduced;
     private Semaphore valueConsumed;
     
     public Producer (Buffer shared) {
          super("Producer");
          sharedLocation = shared;
     }
     
     public void run() {
          for (int count = 1; count <= 4; count++){
               try {
                    Thread.sleep((int) (Math.random() * 3001));
                    valueConsumed.P();
                    //check to see if the value has been consumed P( valueConsumed );
                    sharedLocation.set(count);
                    //let consumer thread know it can consume the value
                    valueProduced.V();
               }
               catch (InterruptedException exception) {
                    exception.printStackTrace();
               }
          }
          System.err.println(getName() + " done producing.");
     }
}
Consumer
public class Consumer extends Thread{
     private Buffer sharedLocation;
     private Semaphore valueProduced;
     private Semaphore valueConsumed;
     
     public Consumer (Buffer shared) {
          super("Consumer");
          sharedLocation = shared;
          
     }
     public void run() {
          int sum = 0;
          for (int count=1; count<=4; count++) {
               try{
                    Thread.sleep((int) (Math.random() * 3001));
                    valueProduced.P();
                    sum += sharedLocation.get();
                    valueConsumed.V();
               }
               catch (InterruptedException exception) {
                    exception.printStackTrace();
               }
          }
          System.err.println(getName() + " done consuming. and sum= " + sum);
     }
}
Semaphore
public class Semaphore{
     private int value;
     public Semaphore(int initval)
     {
          value=initval;
     }
     public void P(){
          if( value>0)
               value=value-1;
          else
               try {
                    Thread.sleep((int) (Math.random() * 3001));
               }
          catch (InterruptedException exception){
               exception.printStackTrace();
          }
     }
     public void V(){
          value=value+1;
     }
}
  • 1. Re: Exception in thread "Producer" java.lang.NullPointerException Error
    3004 Newbie
    Currently Being Moderated
    Hard to tell what the error is without you telling us which lines they're on. (The line numbers don't help us.)

    Look at the indicated lines and see what could possibly be null. Not what you think can be null, because clearly you're already assuming nothing is. Rather, what are the references being dereferenced that could even theoretically be null. At least one of them actually is null.
  • 2. Re: Exception in thread "Producer" java.lang.NullPointerException Error
    843789 Newbie
    Currently Being Moderated
    The error is with valueConsumed.P() and valueProduced.P()
    The buffer is empty at this point but based on my code the producer should put the first value in the buffer. For example the first value is 1. The producer calls valueconsumed.P(); The value is 1 so it decrements value. Then puts the value in the buffer.

    This is the semaphore
         public void P(){
              if( value>0)
                   value=value-1;
              else
                   try {
                        Thread.sleep((int) (Math.random() * 3001));
                   }
              catch (InterruptedException exception){
                   exception.printStackTrace();
              }
         }
         public void V(){
              value=value+1;
         }
    This is the producer
    public void run() {
              for (int count = 1; count <= 4; count++){
                   try {
                        Thread.sleep((int) (Math.random() * 3001));
                        valueConsumed.P();
                        //check to see if the value has been consumed P( valueConsumed );
                        sharedLocation.set(count);
                        //let consumer thread know it can consume the value
                        valueProduced.V();
                   }
                   catch (InterruptedException exception) {
                        exception.printStackTrace();
                   }
              }
              System.err.println(getName() + " done producing.");
         }
    This is the consumer
    public void run() {
              int sum = 0;
              for (int count=1; count<=4; count++) {
                   try{
                        Thread.sleep((int) (Math.random() * 3001));
                        valueProduced.P();
                        sum += sharedLocation.get();
                        valueConsumed.V();
                   }
                   catch (InterruptedException exception) {
                        exception.printStackTrace();
                   }
              }
              System.err.println(getName() + " done consuming. and sum= " + sum);
         }
  • 3. Re: Exception in thread "Producer" java.lang.NullPointerException Error
    3004 Newbie
    Currently Being Moderated
    bananasplitkids wrote:
    The error is with valueConsumed.P() and valueProduced.P()
    Then valueConsumed/Produced are null.

    This is not surprising, since I don't see anywhere in your code where you set either of them.

    Edited by: jverd on Mar 8, 2010 11:42 AM
  • 4. Re: Exception in thread "Producer" java.lang.NullPointerException Error
    843789 Newbie
    Currently Being Moderated
    In the Shared buffer class, I set them to 0 and 1.
    public class SharedBufferTest {
         public static void main( String [] args)
         {
         
         Buffer sharedLocation = new UnsynchronizedBuffer();
              
              
         Producer producer = new Producer( sharedLocation);
         Consumer consumer = new Consumer( sharedLocation );
         Semaphore valueProduced=new Semaphore(0);
         Semaphore valueConsumed = new Semaphore(1);
         
              producer.start();
              consumer.start();
         
         }
    }
  • 5. Re: Exception in thread "Producer" java.lang.NullPointerException Error
    3004 Newbie
    Currently Being Moderated
    bananasplitkids wrote:
    In the Shared buffer class, I set them to 0 and 1.
    public class SharedBufferTest {
         public static void main( String [] args)
         {
         
         Buffer sharedLocation = new UnsynchronizedBuffer();
              
              
         Producer producer = new Producer( sharedLocation);
         Consumer consumer = new Consumer( sharedLocation );
         Semaphore valueProduced=new Semaphore(0);
         Semaphore valueConsumed = new Semaphore(1);
         
              producer.start();
              consumer.start();
         
         }
    }
    That sets your local variables. It doesn't affect the member variables, which are completely separate.
    class Foo {
      int x; // member variable
      int y; // member variable
    
      void bar() {
        int x = 1; // declares and initializes local variable, which hides member variable
        y = 2; // member variable. See the difference between this line and previous line?
    
        this.x = 3; // sets member variable
      }
    }
    Like I said: You need to review variable scope and the difference between locals and members.

    Edited by: jverd on Mar 8, 2010 11:51 AM
  • 6. Re: Exception in thread "Producer" java.lang.NullPointerException Error
    843789 Newbie
    Currently Being Moderated
    So I thought that when I declared
    Semaphore valueProduced=new Semaphore(0);
    Semaphore valueConsumed = new Semaphore(1);
    that it sets valueProduced to 0 and valueConsumed to 1. Then when I call:
    public Semaphore(int initval)
         {
              values=initval;
         }
    it sets it to 0. However based on what you sent I would need to declare valueProduced and valueConsumed as 0 and 1 in the Semaphore class?
  • 7. Re: Exception in thread "Producer" java.lang.NullPointerException Error
    3004 Newbie
    Currently Being Moderated
    bananasplitkids wrote:
    So I thought that when I declared
    Semaphore valueProduced=new Semaphore(0);
    Semaphore valueConsumed = new Semaphore(1);
    that it sets valueProduced to 0 and valueConsumed to 1. Then when I call:
    public Semaphore(int initval)
         {
              values=initval;
         }
    it sets it to 0. However based on what you sent I would need to declare valueProduced and valueConsumed as 0 and 1 in the Semaphore class?
    No, you're missing the point.

    You have two distinct, completely unrelated valueProduced variables, and two distinct, completley unrelated valueConsumed variables.
    public static void main( String [] args)
         {
         
         Buffer sharedLocation = new UnsynchronizedBuffer();
              
              
         Producer producer = new Producer( sharedLocation);
         Consumer consumer = new Consumer( sharedLocation );
         Semaphore valueProduced=new Semaphore(0);
         Semaphore valueConsumed = new Semaphore(1);
         
              producer.start();
              consumer.start();
         
         }
    In the above, you have local vP and vC variables. However, your P and C classes also have member variables. Those member variables happen to have the same name as the above locals, but they're completely unrelated, and they're never being set--they retain their default null values.

    (As a side note, it seems like wonky design for the P and C classes to even have these variables in the first place, but then, I don't know what your overall goals are.)
  • 8. Re: Exception in thread "Producer" java.lang.NullPointerException Error
    843789 Newbie
    Currently Being Moderated
    What Im trying to do is based on what the semaphore value is it determines if the thread needs to wait or if it needs it is able to access its critical section.
  • 9. Re: Exception in thread "Producer" java.lang.NullPointerException Error
    3004 Newbie
    Currently Being Moderated
    bananasplitkids wrote:
    What Im trying to do is based on what the semaphore value is it determines if the thread needs to wait or if it needs it is able to access its critical section.
    That doesn't really tell me anything. At this point, I would recommend first making sure you understand why you're getting the compilation errors and how to fix them, as this is a very basic, fundamental part of Java. Then I'd go back and reexamine my design, asking questions here if need be.
  • 10. Re: Exception in thread "Producer" java.lang.NullPointerException Error
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Habitual double poster
    [http://www.javaprogrammingforums.com/object-oriented-programming/3634-help-error-debugging.html]

    db
  • 11. Re: Exception in thread "Producer" java.lang.NullPointerException Error
    3004 Newbie
    Currently Being Moderated
    Okay, no more help for this kid from me.