views:

103

answers:

4

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.

A: 

Data d in the Main class is never initialized so the call m.d.consume() throws an NPE.

BrennaSoft
A: 

In you Main class, d is declared but not initialized.

Replace:

Data d;

With:

Data d = new Data();
streetpc
A: 

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 ==

}
Kelly French
int r = m.d.produce();andint r = m.d.consume();are producing errors..Kelly, can you suggest a solution?
absk
Thanks Kelly, got it. Still have some logical errors.. will sort 'em out though.
absk
A: 

Where is the synchronization? If you're using Threads, you should also make sure that the "Critical Sections" are kept in a synchronized block or using some other synchronization method. In the above code, this would concern the Data's consume and produce methods.

Javaguru