Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NullpointerException Error

Status
Not open for further replies.

Milleniumlegend

IS-IT--Management
Dec 16, 2003
135
I am gettig the following error.
C:\programs\java\queue>java TestBlockingQueue
Exception in thread "Thread-1" java.lang.NullPointerException
at Consumer.run(Consumer.java:21)
Exception in thread "Thread-3" java.lang.NullPointerException
at Consumer.run(Consumer.java:21)
Exception in thread "Thread-4" java.lang.NullPointerException
at Consumer.run(Consumer.java:21)
Exception in thread "Thread-2" java.lang.NullPointerException
at Consumer.run(Consumer.java:21)
Code:
import java.util.concurrent.BlockingQueue;
import java.io.PrintStream;
import java.util.Date;

public class Consumer extends Thread
{
	private BlockingQueue blockingQueue;
	private String consumerName;
	private PrintStream printStream;
	public Consumer(String consumerName, BlockingQueue blockingQueue, PrintStream printStream)
	{
		consumerName = this.consumerName;
		blockingQueue = this.blockingQueue;
		printStream = this.printStream;
	}
	public void run()
	{
		while (true)
		{
			try
			{
				printStream.println(this.consumerName + blockingQueue.take());
				String time = String.format("%tc",new Date());
				printStream.println("Getting item on BlockingQueue at time: " + time);
			}
			catch (InterruptedException ie)
			{
				ie.printStackTrace();
			}
			
		}
	}
}
 
The following is the main method. I am using Java 1.5 and would like to test the blocking queue code. But I cannot get to the consumer code for some reason.

I am getting the print statement from the producer thread which adds strings to the queue but the consumer is not getting initialised for some reason.

Code:
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class TestBlockingQueue
{
	
	public static void main(String args[])
	{	
		BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<String>();
		Producer producer = new Producer(blockingQueue, System.out);
		Consumer consumerA = new Consumer("ConsumerA", blockingQueue, System.out);
		Consumer consumerB = new Consumer("ConsumerB", blockingQueue, System.out);
		Consumer consumerC = new Consumer("ConsumerC", blockingQueue, System.out);
		Consumer consumerD = new Consumer("ConsumerD", blockingQueue, System.out);
			
		producer.start();
		if (consumerA == null)
		{
			Consumer consumerA = new Consumer("ConsumerA", blockingQueue, System.out);
			consumerA.start();
		}
		consumerA.start();
		consumerB.start();
		consumerC.start();
		consumerD.start();
		
	}
}

Many Thanks in advance.
 
Man I hate threads. My question is, when is something ADDED to the queue? And when you do add something, are you sure you're not adding null?

Basically we need to see the producer code as well.

(all considering I counted line 21 right)
 
As requested the following is the code for Producer Class.

Code:
import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.Random;
import java.io.PrintStream;

public class Producer extends Thread
{
	private BlockingQueue<String> blockingQueue;
	private PrintStream printStream;
	
	public Producer(BlockingQueue<String> blockingQueue, PrintStream printStream)
	{
		this.printStream = printStream;
		this.blockingQueue = blockingQueue;
	}

	public void run()
	{
		try
		{
			while(true)
			{
				String time = String.format("%tc",new Date());
				printStream.println("Putting item on BlockingQueue at time: " + time);
				
				blockingQueue.put("Enqueued at: " + time);
				Random generator = new Random();
				int randomSleep = generator.nextInt(5) + 1;
				
				Thread.sleep(randomSleep * 100);
				

			}
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
				
	}
}

Many Thanks
 
Looks like the consumer constructor is causing the null pointer exception.

Could some one tell me whats wrong with this piece of code.

In the main method we create the objects using the following code.

Code:
Consumer consumerA = new Consumer("ConsumerA", blockingQueue, System.out);

The consumer class constructor is like follows.
Code:
private BlockingQueue blockingQueue;
private String consumerName;
private PrintStream printStream;
public Consumer(String consumerName, BlockingQueue    blockingQueue, PrintStream printStream)
  {
	consumerName = this.consumerName;
	blockingQueue = this.blockingQueue;
	printStream = this.printStream;	
  }


Looks like somewhere in the printstream the null object is being created. Could you please advice how this can be fixed.

Many Thanks
 
You are setting the objects the wrong way round.

It should be :

Code:
public Consumer(String consumerName, BlockingQueue    blockingQueue, PrintStream printStream)
  {
    this.consumerName = consumerName;
    this.blockingQueue = blockingQueue;
    this.printStream = printStream;  
  }

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Oh dude, in frot of our noses ...

You're an eagle.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top