• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

ways to deal with null poingt exceptions

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm doing a program for class. getting an average from acollection. We are using test Junits. one of the test involves null enteret into the collection. this causes a nullpointexception. I tried to use the try/catch thing to catch the null so it dosent crash the program but it dosen't work. Is there any way to deal with this thing so it will pass the Junit test? I tried couple things but it didn't work out. Heres the code for my class and the Junit test class.
Here is the error I get when I run the test.
:java.lang.nullpointerException
on lines 44 and 40.


Junit

My Program Statistics class.

[ September 22, 2007: Message edited by: Jim Yingst ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did "didn't work" look like? An exception or incorrect result?

Try moving more of your code inside the try and catch blocks. Put all the "happy path" stuff in the try, all the error code in the catch. See if that clears things up. It should let you remove a bunch of code!

And try the CODE button when you post code. That will preserve your indenting and make things easier on readers.
[ September 22, 2007: Message edited by: Stan James ]
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well,, obviously the first option is to have a check for null before adding to the collection.

Also, I dont get this part of the code:



it.remove()? whats the point in removing from the iterator? Shouldn't you do a "acollection.remove()"?

The logic is right though, iterate through the collection, remove all nulls, and then try to do any operation on it so that a nullPointerException is not thrown.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's horrible. Nearly everyone thinks aforehand that if it is later, then it is greater. You hear comments sometimes that Hashtable should not be used, explaing that it was written earlier. Well, I got news for ya: That would be like saying Object should not be used because it was written in an earlier time ~ another era, actually, and has been superceded by the Bloat-O-Matic Code Hog that swamps all known page-demand algorithms.

The answer to your queston is best approached by a tradional idiom that I saw recently and have now learned to place in my code, instinctively.

if(s == null )handle;
That prevents exceptions from originating at that point in the code.

Now, if you get one [an exception] you handle it in the catch clause. Say for an example a String is held in your collection. If you encounter a null, just set that string to a new empty string as:

if(s == null )s = new String("");

Or call next on the iterator or something.

You handle exceptions in a catch clause.
[ September 22, 2007: Message edited by: Nicholas Jordan ]
 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Ah! Such a simple mistake , I was reading something about iterators today and I remembered something really simple.
If you want to remove something while iterating through a collection, you have to iterate through the next element and then call it.remove().

ie, the above code would be:

 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really sure, but it is going to go something like that. More experience coders may know the correct way or I can look it up for you easily if you want, but in general all ops on an Iterator pass through to the underlying collection: If you called next, then remove, you will likely remove the next element, not the one it was just pointing at. Collections behave somewhat differently, from one to the next - but these are underlying basic mechanics of computer science rising like an air bubble in a coffee pot ---> once they get to the top, you don't see anything.
 
reply
    Bookmark Topic Watch Topic
  • New Topic