File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Exception in list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception in list" Watch "Exception in list" New topic
Author

Exception in list

krishna kumar.s
Greenhorn

Joined: Oct 01, 2011
Posts: 19
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

Joined: Mar 17, 2011
Posts: 8403
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
krishna kumar.s
Greenhorn

Joined: Oct 01, 2011
Posts: 19
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

Joined: Mar 17, 2011
Posts: 8403
    
  23

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

Joined: Oct 01, 2011
Posts: 19
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

Joined: Oct 13, 2005
Posts: 40029
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in list