Once a programmer had a problem. He thought he could solve it with a regular expression. Now he had two problems.
                    - Anonymous
 
Search
Search

 
 
Navigation menu
Navigation menu
 
 
 
 
Size_box_tl   Size_box_tr
 

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

 

 

 



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