aspose file tools*
The moose likes Threads and Synchronization and the fly likes Runtime error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Runtime error " Watch "Runtime error " New topic
Author

Runtime error

Arun Seshadri
Greenhorn

Joined: Dec 22, 2005
Posts: 18
I am getting a runtime exception on line 40 when running the code.

the following is the 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
{
if (blockingQueue != null)
{
printStream.println(this.consumerName + blockingQueue.take());
}
else {
printStream.println("Blocking Queue Seems to be empty ");
}
//String time = String.format("%tc",new Date());
//printStream.println("Getting item on BlockingQueue at time: " + time);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
}
}


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

}
}

public class TestBlockingQueue
{

public static void main(String args[])
{
BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<String>();
Producer producer = new Producer(blockingQueue, System.out);
//Producer producerB = 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();
//producerB.start();
if (consumerA == null)
{
consumerA = new Consumer("ConsumerA", blockingQueue, System.out);
consumerA.start();
}
consumerA.start();
//consumerB.start();
//consumerC.start();
//consumerD.start();
}


}// End Class TestBlockingQueue


I am not sure what is wrong but the code would not work when executed. except that the producer class works but the consumer would not throwing an Nullpointer Exception. Could someone please help.

Many Thanks
Arun
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Hi,

Welcome to JavaRanch!

In the "Consumer" class's constructor, you're assigning the (null) contents of the member variables to the constructor's parameters, when you surely meant to do the opposite:

consumerName = this.consumerName;
blockingQueue = this.blockingQueue;
printStream = this.printStream;

As a result, the member "printStream" is null and the Consumer will abort when the first message is printed.

(In the future, note that it would be helpful to us if you indicated which line was the "line 40" you're referring to -- you've actually listed the contents of several files here.)


[Jess in Action][AskingGoodQuestions]
Arun Seshadri
Greenhorn

Joined: Dec 22, 2005
Posts: 18
Thanks for your prompt reply. Sorry about the line numbers I was not thinking.

I am still a bit confused. Is that the printStream that is causing the error or the private fields of Consumer Class. I thought I was passing the correct parameter to the consumer class.

Many thanks
Arun
[ January 10, 2006: Message edited by: Arun Seshadri ]
Arun Seshadri
Greenhorn

Joined: Dec 22, 2005
Posts: 18
My Fault. I did fix the fields as they have been referencing null values as mentioned previously.

Appreciate your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime error