日期:2014-05-20  浏览次数:20789 次

编程时遇到个小问题,请大侠们指教下
//这个程序是模仿生产者-消费者写的;编译时没错,但运行时显示一下的错误:
Exception in thread "Thread-1" java.lang.NullPointerException
at com.JinCheng.DataManage.run(MeasureControl.java:33)
以下是源程序:
import ConsoleReader.*;
class DataGather extends Thread{
Our_Monitor om;
public DataGather(){

}
public DataGather(Our_Monitor o_m){
om=o_m;
}
public void run(){
//for(int i=0;i<26;i++){
// char value=(char)(Math.random()*26+'A');
ConsoleReader console=new ConsoleReader(System.in);
String value=console.readLine();
om.push(value);
try{
Thread.sleep(1000);
}catch(Exception e){}
//}
}

}
class DataManage extends Thread{
Our_Monitor om;
public DataManage(){

}
public DataManage(Our_Monitor o_m){
om=o_m;
}
public void run(){
String value=om.pop();
try{
Thread.sleep(1000);
}catch(Exception e){}

//}

}
}
class Our_Monitor {
static final int N=9;
private String[] buffer=new String[N];
private int index=0,in=0,out=0;
public synchronized void push(String value){
if(index==buffer.length){
try{
this.wait();
}catch(Exception e){}
}
buffer[in]=value;
in=(in+1)%N;
index=index+1;
 
  this.notify();
//System.out.println("数据采集:"+value);
}
public synchronized String pop(){
String value;
if(index==0){
try{
this.wait();
}catch(Exception e){}
}
value=buffer[out];
out=(out+1)%N;
index=index-1;
if(index==N-1)
notify();
//return value;
System.out.println("数据处理:"+value);
return buffer[out];
}
}
public class MeasureControl {



public static void main(String args[]){
DataGather dg=new DataGather();
DataManage dm=new DataManage();
dg.start();
dm.start();


}



一下是ConsoleReader类
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;

/**



 *一个方便的键盘输入程序,可以在其他程序中import

 **/

public class ConsoleReader

{
private BufferedReader reader;

public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(new InputStreamReader(inStream));
}

public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);

return n;
}

public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);

return x;
}

public String readLine() {
String inputLine = "";

try {
inputLine = reader.readLine();
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}

return inputLine;
}

/*
public static void main(String s[]){

int i;
System.out.println("Please input a integer :");
ConsoleReader reader=new ConsoleReader(System.in);
i=reader.readInt() ;

System.out.println(i);

}
*/

}



------解决方案--------------------
这是一个空指针异常,om.pop(); 有问题