views:

23

answers:

1

Hi there,

I'm relatively new to java, I'm certain the error is trivial. But can't for the life of me spot it. I have an end of term exam on monday and currently trying to get to grips with past papers!

Anyway heregoes, in another method (ALGO_1) I search over elements of and check the value H_NAME equals a value entered in the main. When I attempt to run the code I get a null pointer exception, also upon trying to print (with System.out.println etc) the H_NAME value after each for loop in the snippet I also get a null statement returned to me.

I am fairly certain that the collection is simply not storing the data gathered up by the Scanner. But then again when I check the collection size with size() it is about the right size. Either way I'm pretty lost and would appreciate the help.

Main questions I guess to ask are:

  • from the readBackground method is the data.add in the wrong place?
  • is the snippet simply structured wrongly?
  • oh and another point when I use System.out.println to check the Background object values name, starttime, increment etc they print out fine.

Thanks in advance.(PS im guessing the formatting is terrible, apologies.)

snippet of code:

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)){ //get error here  




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();  
double starttime = Double.parseDouble(s.next());  
double increment = Double.parseDouble(s.next());  
double sum = 0;  
double p = 0;  
double nterms = 0;  
while((s.hasNextDouble())){  
p = Double.parseDouble(s.next());  
nterms++;  
sum += p;  
}  
double pbmean = sum/nterms;  
Background SAMP = new Background(name, starttime, increment, pbmean);  
data.add(SAMP);  
}  
return data;  
}  

Edit/Delete Message

A: 

If you get a NullPointerException (NPE) when you call back.H_NAME.equals(xxx), it probably means either back is null or back.H_NAME is null.

You say that when you print back.H_NAME, you get null, so that suggests that back.H_NAME is actually null. You haven't shown us any code that describes the class Background, so it's hard to give you any more help.

John
True I thought I may have missed something. Here's the code for Background. Thanks.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; } }
Elliott
Yeah, I think its definitely null by the time I get to the method checking the H_NAME variable, I just cant work out why. Since if I print out the steps of the method that creates <background> the data is definitely being read...
Elliott