• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime error

 
Arun Seshadri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Arun Seshadri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Fault. I did fix the fields as they have been referencing null values as mentioned previously.

Appreciate your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic