<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
EJB EntityManager results in NullPointerException
</h3>
<span style="margin-bottom: 10px;">
created by <a href="https://community.jboss.org/people/sermilion">Ibragim Gapuraev</a> in <i>EJB3</i> - <a href="https://community.jboss.org/message/825825#825825">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p><span style="color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; background-color: #ffffff;">I have an error that I cannot logically understand. I am implementing RSS processing app. The logic is that user takes link of some RSS feed and adds to database. If the feed already exist in database, it takes the id of feed and adds it to subscriptions of the user. Here is the code:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; background-color: #ffffff;">     <code style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">if (existfeed == null) {<br/>            feed = loadFeed(url, syndfeed);<br/>            em.persist(feed);<br/>            if (feed.getFeedId() == null) {<br/>                return "Persist of feed failed";<br/>            } <br/>                uf.setFeedId(feed);<br/>                uf.setUserId(user_id);<br/>                try {<br/>                    em.persist(uf);<br/>                    loadFeedItems(syndfeed, feed);<br/>                    //boolean ufadded = uf.addUserFeed(em);<br/>                } catch (NoResultException ex) {<br/>                    return "Persist of userfeed failed";<br/>                } catch (PersistenceException ex) {<br/>                    return "Persist of userfeed failed, em";<br/>                }<br/>                //boolean ufadded = uf.addUserFeed(em);<br/><br/>                //return true;                <br/>            return "Feed added";<br/>        } else {<br/>            dbFeed = getFeed(feed.getLink());<br/>            if(dbFeed==null)<br/>                return "Faile to get feed from database";<br/><span>            //</span><a class="jive-link-external-small" href="http://appleinsider.ru/feed" rel="nofollow" target="_blank">http://appleinsider.ru/feed</a><br/>            uf.setFeedId(dbFeed);<br/>            uf.setUserId(user_id);<br/>            try {<br/>                em.persist(uf);<br/>            } catch (PersistenceException ex) {<br/>                //System.out.println(ex.getMessage());<br/>                return "Unable to persist userfeed";<br/>            }<br/>            return "Feed added, existing";<br/>        }</code></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="color: #000000; font-size: 14px; background-color: #ffffff; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">However, if user added a feed, another user cannot add it and there is the error:</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="color: #000000; font-size: 14px; background-color: #ffffff; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">     <code style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"> Caused by: java.lang.NullPointerException<br/>at ejb.FeedBean.addNewFeed(FeedBean.java:203) [Group_7_Impl_uwx09sua-ejb.jar:]<br/>at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [classes.jar:1.6.0_51]<br/>at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [classes.jar:1.6.0_51]<br/>at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [classes.jar:1.6.0_51]<br/>at java.lang.reflect.Method.invoke(Method.java:597) [classes.jar:1.6.0_51]<br/>at org.jboss.as.ee.component.ManagedReferenceMethodInterceptorFactory</code></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="margin: 0 0 1em; font-size: 14px; background-color: #ffffff; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">I just dont see why would it fail. One of my opinion is that possibly, EntityManager somehow is null. But, in first part of if statement it works.</p><p style="margin: 0 0 1em; font-size: 14px; background-color: #ffffff; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">I tried to use EntityManager find method, it resulted same error. So, definitely, EntityManager is somehow null in second part of if statement.</p><p style="margin: 0 0 1em; font-size: 14px; background-color: #ffffff; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">Thanx for all the help.</p><p><span style="color: #000000; font-size: 14px; background-color: #ffffff; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br/></span></p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/825825#825825">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in EJB3 at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>