Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in list

 
krishna kumar.s
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to solve problem 23 of project euler "http://projecteuler.net/problem=23" . The problem is i have to find sum all numbers that could not be written as sum of two abundant numbers.

The logic i used was ,1) find all the abundant numbers below 28123(this is the upper limit as mentioned in problem) store them in array add num+next and find whether it is equal to value any of value of list containing 1-28123. if eqauls remove the number . but the below programme throws an exception.

can anybody help?, is the logic correct?
Exception in thread "main" java.lang.NullPointerException
at Exercise23.main(Exercise23.java:44)
 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishna kumar.s wrote:can anybody help?, is the logic correct?
Exception in thread "main" java.lang.NullPointerException
at Exercise23.main(Exercise23.java:44)

Well first thing is to use the code tags correctly. The 'slash-code' tag should go at the end of the code. I'd suggest editing your post.

However, to answer your question, the line:
while(iterator.hasNext()){
looks like the culprit to me. You've clearly set 'iterator' to null, but I can't see anywhere where you set it to anything else.

As for the logic, it looks a bit tortured to me. Firstly:
for (int i=1;i<=num/2;i++) ...
is likely to make your program run far longer than it needs to.

I'd also break it up a bit more, into nicely named methods. You'll probably find it far easier to read yourself (and we certainly will).

Also, the standard loop for processing an array arr is:
for(int i = 0; i < arr.length; i++) { ...

HIH

Winston
 
krishna kumar.s
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, The problem is very simple i used one method to get adundant numbers . Now my modified code is throwing Exception in thread "main" .I am sorry that giving difficult in reading , i am new to programme and learning myself , first i am trying with solving project euler (i have completed 25 problems and moved to level 1)

 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishna kumar.s wrote:I am sorry that giving difficult in reading , i am new to programme and learning myself...

That's fine, but please read what I said about your code tags and correct them. If you don't understand, please check the UseCodeTags page. I'll try and answer your question as soon as you correct your post.

Winston
 
krishna kumar.s
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help, The issue is resolved , i used code button and tag " "at the begging as well as end
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a LinkedList rather than an ArrayList?
You should have your Iterator come into scope just before your loop and go out of scope just after your loop. If there is an Iterator in scope when you add to a List, you ought to get a ConcurrentModificationException.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic