Hi there, 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 hydroList = null;
Collection 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 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 data = new Vector();
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 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 data = new Vector();
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 backgs, Collection 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;
}
}