Results 1 to 7 of 7
  1. #1
    TimHuey is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default Programing Technique question - Try or If

    I have snippet of code that has the possibility of generating a nullpointerexception. Here is the code:

    Java Code:
    ArrayList overnightCities = ((SeqObject) seqHashTable.get(String.valueOf(seq))).getCities();
    If the item exists in the hastable it is retrieved. If it doesn't, it generates a nullpointerexception. A miss keyed data file can cause the item to not be created. In the past I've tested before I issued the assignment command with:

    Java Code:
    if (seqHashTable.containsKey(String.valueOf(seq))) {
    A try Catch block would do just as well. Which is the preferred method of coding the possible error.

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    personally, I go with the if method of handling it, as I've been told try-catch blocks can create large overheads, and also because I've had difficulty catching RuntimeExceptions, notable ClassCastExceptions (so I use if (var instanceof MyClass) instead of a catch block). However, this is really just my personal preference.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    TimHuey is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    Thanks Boyo, I like the idea of keeping the overhead down.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Quote Originally Posted by TimHuey View Post
    Thanks Boyo, I like the idea of keeping the overhead down.
    You shouldn't worry about any perceived overhead. You should code in a way that makes sense, logic-wise.

    In your case, is it an error if seqHashTable.get(key) returns null?

    In this situation, though, all you need to do is check for null. Try not to squeeze too much onto a single line:

    Java Code:
    ArrayList overnightCities = ((SeqObject) seqHashTable.get(String.valueOf(seq))).getCities();
    should probably be:

    Java Code:
    SeqObject seq = seqHashTable.get(String.valueOf(seq));
    if (seq == null) // Do something here, maybe throw exception?
    ArrayList overnightCities = seq.getCities();
    What you do in the if is entirely dependent on the structure of your code. Since this will likely be a simple method an exception here might be a reasonable choice. It would probably be mine.

  5. #5
    TimHuey is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    Thanks for the heads up on one big line of code. I was wondering that as well. "If you can squeeze everything into one line, should you." The second parsed out way is more readable but not as glamorous. I don't know much about programming or Java and any chance I have of showing some sibilance of prowess I jump at. I have several instances where I grab an seq object like that and as a matter of fact here is some code from my program:

    Java Code:
    displayaline((String) ((JList) evt.getSource()).getSelectedValue()); //Wow, evt.getSource retrieves the object that experienced the EVENT, then since you know it will be a JList
                                                              //you cast it as (JList) that way you can access the functions of the JList item, namely ".getSelectedValue()"
                                                              //that returns the value that you just mouseclicked on. Since we know it will be a String we castit as (String)
                                                              //all this is seperate out below but Ithought I would try and combine into one statement, you know for fun!
    //    JList temp = (JList) evt.getSource();
    //    String lineselected = (String) temp.getSelectedValue();
    //    displayaline(lineselected);
    As you can see from above I bounced back and forth trying to decide which way to retrieve the object. I will stretch things out a bit from now on.
    Last edited by TimHuey; 09-15-2009 at 05:00 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    I prefer open code.
    It's easier to debug, and easier to maintain (as in your null problem above).
    There are times when a oneliner is OK, the Hibernate Criteria stuff immediately springs to mind, but in that case you know what you've got (no nulls to bite you) since you're simply adding things in.
    eg:

    Java Code:
    criteria
    	.add(Restrictions.eq("userId", userId))
    	.add(Restrictions.ge("orderDate", fromDate))
    	.addOrder(org.hibernate.criterion.Order.desc("orderDate"));

  7. #7
    TimHuey is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    Well, your right Tolls, and I am writing this software in the hopes that others where I work will take up the torch and continue the development. It will definitely be to our advantage if it is as readable as possible.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Improve my programing in applet
    By Knights.innas in forum Java Applets
    Replies: 4
    Last Post: 03-24-2009, 10:28 PM
  3. socket programing outside the network
    By Omarero in forum Networking
    Replies: 4
    Last Post: 02-16-2009, 06:15 AM
  4. Hi, I am new to programing!
    By Zrob in forum Introductions
    Replies: 1
    Last Post: 09-14-2008, 05:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •