NullPointerException,集合没有存储数据?

时间:2010-03-20 11:00:15

标签: java

我之前发布了这个问题,但没有完整的代码。下面的系列还调用了我在底部包含的其他类Background和Hydro。

我在星号指示的行上有一个Nullpointerexception。这会告诉我收藏集没有正确存储数据。虽然当我检查它们的大小时,它们看似正确。

提前致谢。 PS:如果有人想就如何最好地格式化我的代码以使其可读性给我建议,我们将不胜感激。

埃利奥特

>package exam0607;

>import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Collection;
import java.util.Scanner;
import java.util.Vector;

>import exam0607.Hydro;
import exam0607.Background;// this may not be necessary???? FIND OUT 


>public class HydroAnalysis {

 public static void main(String[] args) {

  Collection<Hydro> hydroList = null;
  Collection<Background> backList = null;

  try{hydroList = readHydro("http://www.hep.ucl.ac.uk/undergrad/3459/exam_data/2006-07/final/hd_data.dat");}
  catch (IOException e){
   e.getMessage();}
  try{backList = readBackground("http://www.hep.ucl.ac.uk/undergrad/3459/exam_data/2006-07/final/hd_bgd.dat");
   //System.out.println(backList.size());
  }

  catch (IOException e){
   e.getMessage();}

  for(int i =0; i <=14; i++ ){ 
   String nameroot = "HJK";  
   String middle = Integer.toString(i);
   String hydroName = nameroot + middle + "X";
   System.out.println(hydroName);
   ALGO_1(hydroName, backList, hydroList);
  }
 }

 public static Collection<Hydro> readHydro(String url) throws IOException {
  URL u = new URL(url); 
  InputStream is = u.openStream(); 
  InputStreamReader isr = new InputStreamReader(is); 
  BufferedReader b = new BufferedReader(isr); 
  String line ="";

  Collection<Hydro> data = new Vector<Hydro>();
  while((line = b.readLine())!= null){
   Scanner s = new Scanner(line);
   String name = s.next(); 
   System.out.println(name);
   double starttime = Double.parseDouble(s.next());
   System.out.println(+starttime);
   double increment = Double.parseDouble(s.next());
   System.out.println(+increment);
   double p = 0;
   double nterms = 0;

   while(s.hasNextDouble()){
    p = Double.parseDouble(s.next());
    System.out.println(+p);
    nterms++;
    System.out.println(+nterms);  
   }
   Hydro SAMP = new Hydro(name, starttime, increment, p);
   data.add(SAMP);  
  }
  return data;
 }

 public static Collection<Background> readBackground(String url) throws IOException {
  URL u = new URL(url); 
  InputStream is = u.openStream(); 
  InputStreamReader isr = new InputStreamReader(is); 
  BufferedReader b = new BufferedReader(isr); 
  String line ="";
  Vector<Background> data = new Vector<Background>();
  while((line = b.readLine())!= null){
   Scanner s = new Scanner(line);
   String name = s.next();    
   //System.out.println(name);
   double starttime = Double.parseDouble(s.next());
   //System.out.println(starttime);
   double increment = Double.parseDouble(s.next());
   //System.out.println(increment);
   double sum = 0;
   double p = 0;
   double nterms = 0;
   while((s.hasNextDouble())){
    p = Double.parseDouble(s.next()); 
    //System.out.println(p);
    nterms++;
    sum += p;
   }
   double pbmean = sum/nterms;
   Background SAMP = new Background(name, starttime, increment, pbmean);
   //System.out.println(SAMP);
   data.add(SAMP);  
  }
  return data;
 }

 public static void ALGO_1(String hydroName, Collection<Background> backgs, Collection<Hydro> hydros){
  //double aMin = Double.POSITIVE_INFINITY;
  //double sum = 0;
  double intensity = 0;
  double numberPN_SIG = 0;
  double POSITIVE_PN_SIG =0;
  //int numberOfRays = 0;
  for(Hydro hd: hydros){
   System.out.println(hd.H_NAME);
   for(Background back : backgs){
    System.out.println(back.H_NAME);
    if(back.H_NAME.equals(hydroName)){//ERROR HERE 
     double PN_SIG = Math.max(0.0, hd.PN - back.PBMEAN);
     numberPN_SIG ++; 
     if(PN_SIG > 0){ 
      intensity += PN_SIG;
      POSITIVE_PN_SIG ++;
     } 
    }    
   }
   double positive_fraction = POSITIVE_PN_SIG/numberPN_SIG;
   if(positive_fraction < 0.5){
    System.out.println( hydroName + "is faulty" );
   }
   else{System.out.println(hydroName + "is not faulty");} 
   System.out.println(hydroName + "has instensity" + intensity);
  }   

 } 
}
  

背景课程

package exam0607;
public class Background {

 String H_NAME;
 double T_START;
 double DT;
 double PBMEAN;



 public Background(String name, double starttime, double increment, double pbmean) {

 name = H_NAME;
 starttime = T_START;
 increment = DT;
 pbmean = PBMEAN;

 }}

和HYDRO CLASS

public class Hydro {

 String H_NAME;
 double T_START;
 double DT;
 double PN;
 public double n;

 public Hydro(String name, double starttime, double increment, double p) {

  name = H_NAME;
  starttime = T_START;
  increment = DT;
  p = PN;
 }

}

1 个答案:

答案 0 :(得分:6)

阿。您的Hydro类完全错误。您将参数分配给未初始化的成员。

e.g。

 public Hydro(String name, double starttime, double increment, double p) {
  name = H_NAME;

但H_NAME未初始化。你需要扭转这些,例如。

 public Hydro(String name, double starttime, double increment, double p) {
   H_NAME = name;

一些提示:

  1. 为每个类编写一个toString()方法,这样就可以有意义地打印出来
  2. 如果您不希望它们更改
  3. ,则将您的方法参数声明为final
  4. 如果您希望您的类是不可变的(不变)
  5. ,则将您的成员变量声明为final
  6. 调查单元测试和JUnit
  7. 在上面,1。使调试更容易2.和3.意味着编译器将阻止你从上面的(太常见)错误(尝试使用final的方法参数之前,看看会发生什么! )4.将强制您在较低级别进行测试并保持代码正确。

    出于调试目的,一次仅推荐一个引用是有用的。这有助于您识别意外的空引用,例如如果以下内容给出NullPointerException

    a.getB().getC().getD()
    

    agetB()getC()中的哪一个给出了空引用?每行取消引用一个更详细但会给你更多信息(对象纯粹主义者会反对上面的内容并引用你Law Of Demeter - 我将忽略这个例子)。