[jsword-devel] [JIRA] Commented: (JS-170) GenBookBackend.contains(key) throws NPE if key not found

Martin Denham (JIRA) jira at crosswire.org
Mon Mar 7 06:54:32 MST 2011


    [ http://www.crosswire.org/bugs/browse/JS-170?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14072#comment-14072 ] 

Martin Denham commented on JS-170:
----------------------------------

A simple fix is to add a simple null check as below:

    public boolean contains(Key key) {
        checkActive();

        try {
            DataPolice.setKey(key);
            TreeNode node = find(key);

            if (node==null) {
                return false;
            }

            byte[] userData = node.getUserData();

            // Some entries may be empty.
            return userData.length == 8;
        } catch (IOException e) {
            return false;
        } finally {
            DataPolice.setKey(null);
        }
    }


> GenBookBackend.contains(key) throws NPE if key not found
> --------------------------------------------------------
>
>                 Key: JS-170
>                 URL: http://www.crosswire.org/bugs/browse/JS-170
>             Project: JSword
>          Issue Type: Bug
>          Components: o.c.jsword.book.sword
>    Affects Versions: 1.6
>         Environment: All
>            Reporter: Martin Denham
>            Assignee: DM Smith
>
> I think there is a bug in GenBookBackend.contains(Key).  
> The find method will return null if the key is not in the book
>             TreeNode node = find(key);
> which means the next line will throw a NullPointerException
>             byte[] userData = node.getUserData();
> Here is the whole method:
>     @Override
>     public boolean contains(Key key) {
>         checkActive();
>         try {
>             DataPolice.setKey(key);
>             TreeNode node = find(key);
>             byte[] userData = node.getUserData();
>             // Some entries may be empty.
>             return userData.length == 8;
>         } catch (IOException e) {
>             return false;
>         } finally {
>             DataPolice.setKey(null);
>         }
>     }

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira



More information about the jsword-devel mailing list