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)请高手指教!!!
不甚感激`
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)请高手指教!!!
不甚感激`
貌似是
public Account(int id){
this.id = id;
this.a.id = new Random().nextInt(20); //为前面定义的Account中的id赋随机值
}
的a这个对象还没有实例化,你对一个空指针的声明的属性赋值当然不对啦
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());
}
}
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
System.out.println(files[0]);运行一下上面的代码你就知道为什么NullPointerException了
for (int i = 0; i < a.length; i++) {
a[i].setAccount(a); public void setAccount(Account[] b){
this.a=b[new Random().nextInt(20)];这个地方有点呦`