When I coded this only I and God knew what it was doing, now only God knows what it does
                                - Anonymous
 
Пошук
Пошук

 
 
Навігаційне меню
Навігаційне меню
 
 
Проекти
Проекти
 
 
 
 
Size_box_tl   Size_box_tr
 
Unfortunately, there is no translation for this page. We have redirected you to the english version of the site. We are sorry for any inconvenience caused...

We are double checking everything, well almost...

 

Take a lot a the snippet below on how to correctly check the parameters of an object before working with it:

public class DAssetAdapter extends AbstractAdapterImpl {
   

   ...


    /**
     * {@inheritDoc}
     */
    public void assemble(final DAssetDTO source, final Sku target) {
        target.setEnabled(source.isEnabled());
        if (source.isEnabled()) {
            DAsset dAsset = createDAsset(target);

            checkLimit(source.getMaxDownloads());
            checkExpiry(source.getExpiry());
            
            dAsset.setFileName(source.getFileName());
            dAsset.setExpiry(source.getExpiry());
            if (source.getMaxDownloads() != null) {
                dAsset.setMaxDownloads(source.getMaxDownloads());
            }
            target.setDAsset(dAsset);            
        }
    }

    /**
     * Checks if maxDownloads is negative.
     * 
     * @param maxDownloads the maxDownloadTimes parameter
     */
    void checkDownloadLimit(final int maxDownloads) {
        if (maxDownloads < 0) {
            throw new AssemblyException();
        }
    }

   ...

}

 

To me - it looks solid at a glance. Just before you start noticing things:

1. We check for checkLimit(source.getMaxDownloads()); there we check for a primitive integer 0 but then

2. We check source.getMaxDownloads() != null a little further down the code? Umm... What happens if it really is a null?

3. BINGO, we've got a winner:

Could not import. See Log for details. Associated exception: java.lang.NullPointerException

 

So today's lesson:

If you work with objects be sure to check for nulls prioir using them not after :-) to avoid IDD comming into your code

 

 

 

Ця сторінка була оновлена: 05/11/2009 07:41


© Inspire Software, Denys Pavlov, 2005-2012
© Inspire Software, Denys Pavlov, 2005-2012
 
Size_box_bl   Size_box_br