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

大家伙,帮帮忙看看 java.lang.nullpointerexception
public class ScoreManager{
  private double[] score;
  int count;
  double max,min,avg;

  public ScoreManager(double...arr){
   
  for(int i=0;i<score.length;i++)
  score[i]=0;
  }

  public void add(double n){
  count=0;
  for(int i=0;i<score.length;i++){
  if(score[i]==0)
  score[i]=n;
  }
  count++;
  }


  public double getMax(){
  max=score[0];
  for(int i=0;i<score.length;i++){
  if(score[i]>max)
  max = score[i];
  }
  return max;
  }


  public double getMin(){
  min=score[0];
  for(int i=0;i<score.length;i++){
  if(score[i]<min)
  min=score[i];
  }
  return min;
  }


  public double getAvg(){
  int sum=0;
  for(int i=0;i<score.length;i++)
  sum+=score[i];
  avg = sum/score.length;
  return avg;
  }
}


import java.util.Scanner;

public class View{
  double[] arrays; 
  ScoreManager sm = new ScoreManager(arrays);
  public void Test(){
  Scanner scanner = new Scanner(System.in);
  System.out.print("输入:");
  double input = scanner.nextDouble();
  sm.add(input);
  System.out.println(sm.getMax());
  }


  public static void main(String[] args){
  View v = new View();
  v.Test();
  }
}

这个错误怎么改正啊

------解决方案--------------------
Java code
private double[] score; // 数组score要先初始化才能使用: double[] score = new double[100],像这样
  int count;
  double max,min,avg;

  public ScoreManager(double...arr){
    
  for(int i=0;i<score.length;i++)
  score[i]=0;
  }

------解决方案--------------------
Java code
private double[] score;
  int count;
  double max,min,avg;

  public ScoreManager(double...arr){
  score = new double[arr.length]; // 在这里初始化数组  
  for(int i=0;i<score.length;i++)
  score[i]=0;
  }

------解决方案--------------------
把完整的代码贴出来啊
public ScoreManager(double...arr){

那个arr参数是做什么用的?
------解决方案--------------------
private double[] score;
int count;
double max,min,avg;

public ScoreManager(double...arr){
score = new double[arr.length]; // 在这里初始化数组
for(int i=0;i<score.length;i++)
score[i]=0;
}

public class View{
double[] arrays; // 在这里初始化数组
ScoreManager sm = new ScoreManager(arrays);