Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:29:36+00:00 2026-06-11T16:29:36+00:00

I was trying a Producer/Consumer Problem, but I don’t know why I am getting

  • 0

I was trying a Producer/Consumer Problem, but I don’t know why I am getting java.lang.NullPointerException inside the Consumer.

package com ;

import java.util.concurrent.PriorityBlockingQueue;

public class Producer extends CommonClass implements Runnable {

    private int producerNum;

    Producer(PriorityBlockingQueue<Character> queue) {
        queue = queue;
    }

    public void run() {

        char ch;

        for (ch = 'a'; ch <= 'z'; ch++) {
            queue.add(ch);
            System.out.println("Producer" + producerNum + "produced :" + ch);
            try {
                Thread.sleep((int) (Math.random() * 300));

            } catch (InterruptedException e) {
                System.out.println("Error");
            }

        }

    }

}

This is my Consumer class

package com ;

import java.util.concurrent.PriorityBlockingQueue;

public class Consumer extends CommonClass implements Runnable {

    private int consumerNum;

    Consumer(PriorityBlockingQueue<Character> queue )
    {
        queue = queue;  
    }

    public void run() {
        char c;

        for (int i = 0; i < 27; i++) {
            c = queue.poll();
            System.out.println("Consumer" + consumerNum + "consumed:" + c);
            try {
                Thread.sleep((int) (Math.random() * 300));
            } catch (InterruptedException e) {
                System.out.println("Error");
            }
        }

    }
}

package com ;

import java.util.concurrent.PriorityBlockingQueue;

public class CommonClass {

     PriorityBlockingQueue<Character> queue = new PriorityBlockingQueue<Character>();

}

package com ;

import java.util.concurrent.PriorityBlockingQueue;

public class SyncTest {

    public static void main(String[] args) {



         PriorityBlockingQueue<Character> queue = new PriorityBlockingQueue<Character>();

        Producer p1 = new Producer(queue);
        Thread t1 = new Thread(p1);
        t1.start();

        Consumer c1 = new Consumer(queue);
        Thread ct1 = new Thread(c1);
        ct1.start();

    }
}

This is the exception I get:

Exception in thread "Thread-1" java.lang.NullPointerException
    at com.Consumer.run(Consumer.java:18)
    at java.lang.Thread.run(Unknown Source)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:29:37+00:00Added an answer on June 11, 2026 at 4:29 pm

    This is the immediate problem:

    Consumer(PriorityBlockingQueue<Character> queue )
    {
        queue = queue;  
    }
    

    That’s a no-op statement, assigning the parameter’s value back to itself. You want:

    Consumer(PriorityBlockingQueue<Character> queue )
    {
        this.queue = queue;  
    }
    

    Once you’ve fixed that, you’ll then have a potential problem due to calling poll() (decalred in Queue), which will return null if the queue is empty. That null reference will then be unboxed to assign the value to the c variable (of type char).

    Use take() (declared in BlockingQueue) instead, which will block. You may want to specify a timeout, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement producer-consumer model and I got some problem with it. Consider
I am trying to solve a consumer/producer problem, and I have to create three
I'm trying to set up a simple producer-consumer system in Gevent but my script
I'm trying to write a producer-consumer pattern in java. I'm a network client connected
Right now I am trying to create a producer/consumer thread, the producer thread goes
I'm trying to use the producer consumer pattern to process and save some data.
I'm trying to implement the producer-consumer algorithm on ATMega323_WinAVR using FreeRTOS. I get this
I currently have two clients (Producer/Consumer), and I am trying to send a large
I'm using C# TPL and I'm having a problem with a producer/consumer code... for
Trying to use the TThreadedQueue (Generics.Collections) in a single producer multiple consumer scheme. (Delphi-XE).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.