需要帮助Java Producer Consumer Problem,NullPointerException

时间:2010-04-16 14:43:06

标签: java multithreading nullpointerexception

这是我的代码:

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

    }
}

出现以下错误:

  

线程“Thread-0”中的异常   线程“Thread-1”中的异常   显示java.lang.NullPointerException           在test.Cons.run(Main.java:84)           在java.lang.Thread.run(Thread.java:619)   显示java.lang.NullPointerException           在test.Prod.run(Main.java:58)           在java.lang.Thread.run(Thread.java:619)

我是Java新手。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

我会看以下几行:

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

我没有看到m.d在哪里被赋予新Data()

的实例

我会向Main添加一个构造函数,用于创建Data实例并将其分配给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();

    }

==添加==

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

==添加==

}

答案 1 :(得分:1)

Main类中的数据d从不初始化,因此调用m.d.consume()会抛出NPE。

答案 2 :(得分:1)

在您的主类中,声明了d但未初始化。

替换:

Data d;

使用:

Data d = new Data();

答案 3 :(得分:0)

同步在哪里?如果您正在使用线程,则还应确保将“关键部分”保存在同步块中或使用其他某种同步方法。在上面的代码中,这将涉及Data的使用和生成方法。