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 751127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:37:51+00:00 2026-05-14T14:37:51+00:00

This is my code: package test; import java.util.logging.Level; import java.util.logging.Logger; class Data{ int ar[]=new

  • 0

This is my code:

package test;

import java.util.logging.Level;
import java.util.logging.Logger;

class Data{
    int ar[]=new int[50];
    int ptr;

    Data()
    {
        for(int i=0;i<50;i++)
            ar[i]=0;
        ptr=0;
    }

    public int produce()
    {
        if(this.ptr<50)
        {
            this.ar[this.ptr]=1;
            this.ptr++;
            return this.ptr;
        }
        else return -1;
    }

    public int consume()
    {
        if(this.ptr>0)
        {
            this.ar[this.ptr]=0;
            this.ptr--;
            return this.ptr;
        }
        else
            return -1;

    }
}
class Prod implements Runnable{

    private Main m;

    Prod(Main mm)
    {
        m=mm;
    }

    public void run()
    {

            int r = m.d.produce();
            if (r != -1) {
                System.out.println("Produced, total elements: " + r);
            } else
        {
                try {
                wait();
            }
                catch (InterruptedException ex) {
            Logger.getLogger(Prod.class.getName()).log(Level.SEVERE, null, ex);
        }
        }

    }
}

class Cons implements Runnable{

    private Main m;

    Cons(Main mm)
    {
        m=mm;
    }
    public void run()
    {
        int r=m.d.consume();
        if(r!=-1)
            System.out.println("Consumed, total elements: " + r);
         else
        {
                try {
                wait();
            }
                catch (InterruptedException ex) {
            Logger.getLogger(Prod.class.getName()).log(Level.SEVERE, null, ex);
        }

        }
        notify();
    }

}
public class Main{
    Data d;
    public static void main(String s[]) throws InterruptedException{
        Main m = new Main();
        Prod p = new Prod(m);
        Cons c = new Cons(m);
        new Thread(p).start();
        new Thread(c).start();

    }
}

It is giving following errors:

Exception in thread “Thread-0”
Exception in thread “Thread-1”
java.lang.NullPointerException
at test.Cons.run(Main.java:84)
at java.lang.Thread.run(Thread.java:619)
java.lang.NullPointerException
at test.Prod.run(Main.java:58)
at java.lang.Thread.run(Thread.java:619)

I am new to Java. Any help will be appreciated.

  • 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-05-14T14:37:51+00:00Added an answer on May 14, 2026 at 2:37 pm

    I would look at the following lines:

    public void run()
    {
        int r=m.d.consume();
    

    I don’t see where m.d is ever given an instance of new Data()

    I would add a constructor to Main that creates an instance of Data and assigns it to d.

    public class Main{
        Data d;
        public static void main(String s[]) throws InterruptedException{
            Main m = new Main();
            Prod p = new Prod(m);
            Cons c = new Cons(m);
            new Thread(p).start();
            new Thread(c).start();
    
        }
    

    == added ==

    public Main(){
        d = new Data();
    }  
    

    == added ==

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

Sidebar

Related Questions

Consider this code: package Prova; import java.util.ArrayList; public class Prova { private ArrayList<String> people;
Source Code of LoginAction.java package com.test; import java.util.ArrayList; import java.util.List; public class LoginAction {
Please review code: /* Run1.java */ package test; import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet;
I am using the following code for 2D-QR code decoder. package com.test.rim; import java.util.*;
I have this code: package org.optimization.geneticAlgorithm; import org.optimization.geneticAlgorithm.selection.Pair; public abstract class Chromosome implements Comparable<Chromosome>
Given the following not-very-useful code: package com.something; import java.util.ArrayList; import java.util.Collection; //Not a generic
I have this code: package com.powergroupbd.timer; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer;
This is my code: package hello.project; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.content.Context; import android.opengl.GLSurfaceView.Renderer;
This is my MyTasteActivity code: package MyTaste; import android.app.Activity; import android.content.Context; import android.os.Bundle; import
This piece of code is from Adobe docs : package { import flash.display.Loader; import

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.