Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with Error Debugging

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Error Debugging

    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;
    	}
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with Error Debugging

    The problem isn't that you didn't implement them correctly, but that you forgot to instantiate them in your Producer class.

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Help with Error Debugging


  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with Error Debugging

    meh asking the same question on different forums isn't the same as asking the same question on the same forum.

Similar Threads

  1. debugging classes in jarfile
    By hghayoumi in forum Java Applets
    Replies: 2
    Last Post: December 7th, 2009, 09:51 PM
  2. Debugging
    By Altaf in forum Java IDEs
    Replies: 0
    Last Post: December 6th, 2009, 10:16 PM
  3. Debugging your program
    By helloworld922 in forum Debugging Tutorials
    Replies: 0
    Last Post: November 18th, 2009, 03:14 AM