import java.util.*;
public class Accounts {
static Account a[]=new Account[20];       //创建一个Account的静态数组
public static void great(){                 //定义一个静态方法`能生成20个Account的实例,id不同来区分
for(int i=0;i<20;i++)
a[i]=new Account(i);
}

public static void main(String[] args) {
Accounts.great();                   //调用创建实例的静态方法
for(int i=0;i<20;i++){
new Thread(a[i]).start();      //分别为每个实例定义一个线程
}
}
}

class Account implements Runnable{
Account a;                                      //定义一个Account
int money = 2000;
int id;
public Account(int id){
this.id = id;
this.a.id = new Random().nextInt(20);   //为前面定义的Account中的id赋随机值

}

public synchronized int transfer(){                  //此方法为线程中调用的方法
if(this.a.id != this.id){
if(this.money!=0){                  //若this.a.id != this.id
money-=1000;
this.a.money += 1000; //this.money != 0
}
else{                                 //则将该Account里money转1000到a.money里
try{
this.wait();                       //否则线程挂起
}catch(InterruptedException e){
//异常
}
}
}
this.notifyAll();
return this.money;                          //返回当前Account的money
}

public void run(){

System.out.println(this.transfer());

}
}一个主类Accounts  还有一个类是Account
Account实现的功能是`
如果当前的money!=0,并且this.a.id!=this.id
实行转帐功能`
运行后出现异常`
Exception in thread "main" java.lang.NullPointerException
at Account.<init>(Accounts.java:25)
at Accounts.great(Accounts.java:6)
at Accounts.main(Accounts.java:10)请高手指教!!!
不甚感激`

解决方案 »

  1.   

    看来你最近正在学线程的问题啊,最近你这么多线程的问题
    貌似是
     public Account(int id){
            this.id = id;            
            this.a.id = new Random().nextInt(20);   //为前面定义的Account中的id赋随机值
            
        }
    的a这个对象还没有实例化,你对一个空指针的声明的属性赋值当然不对啦
      

  2.   

    试下我的beta版本import java.util.*;
    public class Accounts {
        static Account a[]=new Account[20];       //创建一个Account的静态数组
        public static void great(){                 //定义一个静态方法`能生成20个Account的实例,id不同来区分
            for(int i=0;i<20;i++)
                a[i]=new Account(i); 
            
            for (int i = 0; i < a.length; i++) {
    a[i].setAccount(a);
    }
            
        }
        
        public static void main(String[] args) {
            Accounts.great();                   //调用创建实例的静态方法
            for(int i=0;i<20;i++){
                new Thread(a[i]).start();      //分别为每个实例定义一个线程
                }                
            }
        }
        
            class Account implements Runnable{
        Account a;                                      //定义一个Account
        int money = 2000;
        int id;
        public Account(int id){
            this.id = id;            
            
        }
        public void setAccount(Account[] b){
         this.a=b[new Random().nextInt(20)];
        }
        
        public synchronized int transfer(){                  //此方法为线程中调用的方法
            if(this.a.id != this.id){
                if(this.money!=0){                  //若this.a.id != this.id
                    money-=1000;    
                    this.a.money += 1000;    //this.money != 0        
                }
                else{                                 //则将该Account里money转1000到a.money里
                    try{
                    this.wait();                       //否则线程挂起
                    }catch(InterruptedException e){
                        //异常
                    }
                }
            }
            this.notifyAll();
            return this.money;                          //返回当前Account的money
        }
        
        public void run(){
            
            System.out.println(this.transfer());    
            
        }
    }
      

  3.   


    import java.util.*;
    public class Accounts {
        static Account a[]=new Account[20];       //创建一个Account的静态数组
        public static void great(){                 //定义一个静态方法`能生成20个Account的实例,id不同来区分
            for(int i=0;i<20;i++)
                a[i]=new Account(i); 
            
            for (int i = 0; i < a.length; i++) {
    a[i].setAccount(a);
    }
            
        }
        
        public static void main(String[] args) {
            Accounts.great();                   //调用创建实例的静态方法
            for(int i=0;i<20;i++){
                new Thread(a[i]).start();      //分别为每个实例定义一个线程
                }                
            }
        }
        
            class Account implements Runnable{
        Account a;                                      //定义一个Account
        int money = 2000;
        int id;
        public Account(int id){
            this.id = id;            
            
        }
        public void setAccount(Account[] b){
         this.a=b[new Random().nextInt(20)];
        }
        
        public synchronized int transfer(){                  //此方法为线程中调用的方法
            if(this.a.id != this.id){
                if(this.money!=0){                  //若this.a.id != this.id
                    money-=1000;    
                    this.a.money += 1000;    //this.money != 0        
                }
                else{                                 //则将该Account里money转1000到a.money里
                    try{
                    this.wait();                       //否则线程挂起
                    }catch(InterruptedException e){
                        //异常
                    }
                }
            }
            this.notifyAll();
            return this.money;                          //返回当前Account的money
        }
        
        public void run(){
            
            System.out.println("当前账号:"+id+"  转账给账号:"+this.a.id+
             " ,还剩下:"+this.transfer());    
            
        }
    }
    看看还有什么问题,结果是这样:
    当前账号:0  转账给账号:16 ,还剩下:1000
    当前账号:2  转账给账号:7 ,还剩下:1000
    当前账号:1  转账给账号:8 ,还剩下:1000
    当前账号:4  转账给账号:14 ,还剩下:1000
    当前账号:6  转账给账号:14 ,还剩下:1000
    当前账号:8  转账给账号:12 ,还剩下:2000
    当前账号:3  转账给账号:7 ,还剩下:1000
    当前账号:10  转账给账号:16 ,还剩下:1000
    当前账号:12  转账给账号:6 ,还剩下:2000
    当前账号:14  转账给账号:0 ,还剩下:3000
    当前账号:5  转账给账号:6 ,还剩下:1000
    当前账号:7  转账给账号:6 ,还剩下:3000
    当前账号:9  转账给账号:5 ,还剩下:1000
    当前账号:11  转账给账号:5 ,还剩下:1000
    当前账号:13  转账给账号:10 ,还剩下:1000
    当前账号:15  转账给账号:14 ,还剩下:1000
    当前账号:16  转账给账号:6 ,还剩下:3000
    当前账号:18  转账给账号:16 ,还剩下:1000
    当前账号:17  转账给账号:14 ,还剩下:1000
    当前账号:19  转账给账号:12 ,还剩下:1000
      

  4.   

    此问题是因为。你在Account类中 有 一个Account类型的引用 "a"。也就是this.a.id = new Random().nextInt(20);   关键是这个 this.a .id 这句。 你程序运行当中的a根本没有指向任何Account类型的实例。不报空指针错误才怪那。你可以在Account 的构造函数中 进行一下实例化。或者从外部传进实例化对象给a.
      

  5.   

    我也说一句.File[] files = new File[50];
    System.out.println(files[0]);运行一下上面的代码你就知道为什么NullPointerException了
      

  6.   

    谢谢啊`  帮我解决几个题了`
      for (int i = 0; i < a.length; i++) {
                a[i].setAccount(a);  public void setAccount(Account[] b){
            this.a=b[new Random().nextInt(20)];这个地方有点呦`