Two collections and a for loop. (Urgent help needed) Checking an object variable against an inputted

Posted by Elliott on Stack Overflow See other posts from Stack Overflow or by Elliott
Published on 2010-03-20T01:22:34Z Indexed on 2010/03/20 1:31 UTC
Read the original article Hit count: 222

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about collections

Related posts about data