开发者

NullPointerException, Collections not storing data?

I posted this question earlier but not with the code in its entirety. The coe below also calls to other classes Background and Hydro which I have included at the bottom.

I have a Nullpointerexception at the line indicate by asterisks. Which would suggest to me that the Collections are not storing data properly. Although when I check their size they seem correct.

Thanks in advance. PS: If anyone would like to give me advice on how best to format my code to make it readable, it would be appreciated.

Elliott

>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(i开发者_如何学Cs); 
  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);
  }   

 } 
}

THE BACKGROUND CLASS

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;

 }}

AND THE 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;
 }

}


Ah. Your Hydro class is completely wrong. You're assigning the parameters to the uninitialised members.

e.g.

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

but H_NAME is uninitialised. You need to reverse these e.g.

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

Some hints:

  1. write a toString() method for each class, so you can print it out meaningfully
  2. declare your method parameters as final if you don't expect them to change
  3. declare your member variables to be final if you expect your class to be immutable (unchanging)
  4. investigate unit tests and JUnit

In the above, 1. makes debugging easier 2. and 3. means that the compiler will prevent you from the (all too common) mistake above (try preceeding your method parameters with final and see what happens!) 4. will force you to test at a low level and keep your code correct.

For debugging purposes it's useful to only deference one reference at a time. This helps you identify unexpected null references e.g. if the following gives a NullPointerException

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

which of a, getB(), getC() gave the null reference ? Dereferencing one per line is more verbose but will give you much more info (object purists will object to the above and refer you to the Law Of Demeter - I shall ignore that for this example).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜