NullPointerException, Collections not storing data?
<p>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.</p>

<p>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. </p>

<p>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. </p>

<p>Elliott</p>

<pre><code>&gt;package exam0607;

&gt;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;

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


&gt;public class HydroAnalysis {

 public static void main(String[] args) {

  Collection&lt;Hydro&gt; hydroList = null;
  Collection&lt;Background&gt; 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 &lt;=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&lt;Hydro&gt; 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&lt;Hydro&gt; data = new Vector&lt;Hydro&gt;();
  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&lt;Background&gt; 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&lt;Background&gt; data = new Vector&lt;Background&gt;();
  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&lt;Background&gt; backgs, Collection&lt;Hydro&gt; 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 &gt; 0){ 
      intensity += PN_SIG;
      POSITIVE_PN_SIG ++;
     } 
    }    
   }
   double positive_fraction = POSITIVE_PN_SIG/numberPN_SIG;
   if(positive_fraction &lt; 0.5){
    System.out.println( hydroName + "is faulty" );
   }
   else{System.out.println(hydroName + "is not faulty");} 
   System.out.println(hydroName + "has instensity" + intensity);
  }   

 } 
}
</code></pre>

<blockquote>
  <p>THE BACKGROUND CLASS</p>
</blockquote>

<pre><code>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;

 }}
</code></pre>

<p>AND THE HYDRO CLASS</p>

<pre><code>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;
 }

}
</code></pre>
