0

I'm new to EJB/ Java EE, trying to do an example referring this tutorial.

I've deployed the ojdbc8.jar to JBOSS and created a JNDI/datasources, I couldn't succeed in doing it manually updating standalone.xml, module and all, however from server logs it appears that data source is working correctly, but somehow EntityManager is not getting injected by @PersistenceContext getting java.lang.NullPointerException, don't know where it is going wrong, can anyone please help?

Toolkit:

IDE : NetBeans 12 Server : Wildfly 8.2

Configuration file of Wildfly is standalone-full.xml

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence                  
                                http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">

  
    <persistence-unit name="EjbCompPersistPU" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:/jdbc/datasources/ejbcomppersist</jta-data-source>
        <class>com.tanmshar.ejbcomppersistsrvr.Books</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
    <property name="hibernate.hbm2ddl.auto" value="update" />
                <property name="hibernate.show_sql" value="true" />
                <property name="hibernate.format_sql" value="true"/>                      
        </properties>    
    </persistence-unit>
    
</persistence>

EjbSBPersistStatelessLocal.java

@Local
public interface EjbSBPersistStatelessLocal {
        
        public String printHelloWorld(String str);
        
        void addBook(String bookName, String bookAuthor);
        List getBooks();                
}

EjbSBPersistStateless.java

@Stateless
public class EjbSBPersistStateless implements EjbSBPersistStatelessLocal {
        
        @PersistenceContext(unitName="EjbCompPersistPU")
        private EntityManager entityManager;         

        @Override
        public String printHelloWorld(String str){                        
                System.out.println("Hello World from EjbSBStatelessLocal "+str);                
                return "Hello World from EjbSBStatelessLocal "+str;
        }
        
        
        @Override
        public void addBook(String bookName, String bookAuthor){
                //bookShelf.add(new Books(bookName,bookAuthor));
                Books book = new Books();                
                book.setBookName(bookName);
                book.setBookAuthor(bookAuthor);
                System.out.println(org.hibernate.Version.getVersionString());
                System.out.println(book.getBookId()+" "+book.getBookName()+" "+book.getBookAuthor());
                System.out.println("entityManager: "+ entityManager); 
                entityManager.persist(book);
                System.out.println("EM done!");
        }
......        
        
}


Books.java

@Entity
@Table(name="books")
public class Books implements Serializable{
        
        @Id
        @GeneratedValue(strategy= GenerationType.IDENTITY)
        @Column(name="bookId")
        private long bookId;        
        
        @Column(name="bookName")
        private String bookName;
        
        @Column(name="bookAuthor")
        private String bookAuthor;
.....        
}

Application deploy logs from server

19:41:22,716 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 2) JBAS015003: Found EjbCompPersistSrvr-1.0-SNAPSHOT.jar in deployment directory. To trigger deployment create a file called EjbCompPersistSrvr-1.0-SNAPSHOT.jar.dodeploy
19:41:37,769 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-3) JBAS015876: Starting deployment of "EjbCompPersistSrvr-1.0-SNAPSHOT.jar" (runtime-name: "EjbCompPersistSrvr-1.0-SNAPSHOT.jar")
19:41:37,788 INFO  [org.jboss.as.jpa] (MSC service thread 1-5) JBAS011401: Read persistence.xml for EjbCompPersistPU
19:41:37,807 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 86) JBAS011409: Starting Persistence Unit (phase 1 of 2) Service 'EjbCompPersistSrvr-1.0-SNAPSHOT.jar#EjbCompPersistPU'
19:41:37,807 INFO  [org.hibernate.jpa.internal.util.LogHelper] (ServerService Thread Pool -- 86) HHH000204: Processing PersistenceUnitInfo [
    name: EjbCompPersistPU
    ...]
19:41:37,828 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016002: Processing weld deployment EjbCompPersistSrvr-1.0-SNAPSHOT.jar
19:41:37,844 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-1) JNDI bindings for session bean named EjbSBPersistStateless in deployment unit deployment "EjbCompPersistSrvr-1.0-SNAPSHOT.jar" are as follows:

    java:global/EjbCompPersistSrvr-1.0-SNAPSHOT/EjbSBPersistStateless!com.tanmshar.ejbcomppersistsrvr.EjbSBPersistStatelessLocal
    java:app/EjbCompPersistSrvr-1.0-SNAPSHOT/EjbSBPersistStateless!com.tanmshar.ejbcomppersistsrvr.EjbSBPersistStatelessLocal
    java:module/EjbSBPersistStateless!com.tanmshar.ejbcomppersistsrvr.EjbSBPersistStatelessLocal
    java:global/EjbCompPersistSrvr-1.0-SNAPSHOT/EjbSBPersistStateless
    java:app/EjbCompPersistSrvr-1.0-SNAPSHOT/EjbSBPersistStateless
    java:module/EjbSBPersistStateless

19:41:37,856 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016005: Starting Services for CDI deployment: EjbCompPersistSrvr-1.0-SNAPSHOT.jar
19:41:37,859 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016008: Starting weld service for deployment EjbCompPersistSrvr-1.0-SNAPSHOT.jar
19:41:37,865 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 86) JBAS011409: Starting Persistence Unit (phase 2 of 2) Service 'EjbCompPersistSrvr-1.0-SNAPSHOT.jar#EjbCompPersistPU'
19:41:37,876 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 86) HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
19:41:37,882 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (ServerService Thread Pool -- 86) HHH000397: Using ASTQueryTranslatorFactory
19:41:37,896 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 86) HHH000228: Running hbm2ddl schema update
19:41:37,896 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 86) HHH000102: Fetching database metadata
19:41:38,376 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 86) HHH000396: Updating schema
19:41:38,570 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 86) HHH000261: Table found: MYSPACE.BOOKS
19:41:38,570 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 86) HHH000037: Columns: [bookauthor, bookname, bookid]
19:41:38,570 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 86) HHH000108: Foreign keys: []
19:41:38,570 INFO  [org.hibernate.tool.hbm2ddl.TableMetadata] (ServerService Thread Pool -- 86) HHH000126: Indexes: [sys_c008055]
19:41:38,571 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 86) HHH000232: Schema update complete
19:41:38,658 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "EjbCompPersistSrvr-1.0-SNAPSHOT.jar" (runtime-name : "EjbCompPersistSrvr-1.0-SNAPSHOT.jar")

Client side NullPointerException stack trace

**********************
Welcome to Book Store
**********************
Options 
1. Add Book
2. Exit 
Enter Choice: 1
Harry Potter
4.3.7.Final
0 Harry Potter JK
entityManager: null

java.lang.NullPointerException
    at com.tanmshar.ejbcomppersistsrvr.EjbSBPersistStateless.addBook (EjbSBPersistStateless.java:39)
    at com.tanmshar.ejbcomppersistclient.EjbPersistLibrary.ejbLibraryFunction (EjbPersistLibrary.java:63)
    at com.tanmshar.ejbcomppersistclient.Main.main (Main.java:24)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:254)
    at java.lang.Thread.run (Thread.java:748)
------------------------------------------------------------------------
BUILD FAILURE

Update -1:

Client side code EjbPersistLibrary.java

calling this from main method 

EjbPersistLibrary ejbLibrary = new EjbPersistLibrary();
ejbLibrary.ejbLibraryFunction();



public class EjbPersistLibrary {
        
        @EJB
        private EjbSBPersistStatelessLocal stateLessLocalBean; 
        private BufferedReader brConsoleReader;

        public EjbPersistLibrary() {
                stateLessLocalBean= new EjbSBPersistStateless();                
                try{
                        brConsoleReader = new BufferedReader(new InputStreamReader(System.in));                
                }catch(Exception e){
                        System.out.println("Message "+e.getMessage());
                }
        }
        
        
        public void ejbLibraryFunction() {
                int choice=1;
                String strChoice, bookName;                
                
                try{
                        while(choice !=2){
                        showGUI();
                                                        
                                strChoice = brConsoleReader.readLine();

                                choice = Integer.parseInt(strChoice);
                                if(choice==1){
                                        bookName = brConsoleReader.readLine();
                                        stateLessLocalBean.addBook(bookName, "JK");
                                }else
                                        {break;}                                                        
                        }
                        
                        if(brConsoleReader!=null)
                                {brConsoleReader.close();}
                        
                }catch(IOException ioe)
                        {ioe.printStackTrace();}

ray.10may
  • 1
  • 3
  • How are you creating the bean `EjbSBPersistStateless`? – perissf Jul 01 '20 at 14:42
  • @perissf after creating the EjbCompPersistSrvr > right click on package com.tanmshar.ejbcompersistsrvr > New > Session Bean (Stateless and Local) – ray.10may Jul 01 '20 at 15:40
  • Sorry I mean: how have you istanciated it in your code? Using `new()` or using `InitialContext.lookup()`? – perissf Jul 01 '20 at 16:00
  • @perissf no...could you please suggest how to do? – ray.10may Jul 01 '20 at 16:05
  • @EJB private EjbSBPersistStatelessLocal stateLessLocalBean=new EjbSBPersistStateless(); – ray.10may Jul 01 '20 at 16:17
  • This is the most relevant part of all your code, particularly because it's the only part that differs from the tutorial you linked. And also because it contains the error (EJBs cannot be instantiated in that way). Please add it to your question and in order to make it readable, delete all the boilerplate of getter and setters, xml files and so on. – perissf Jul 01 '20 at 19:18
  • @perissf included client side code, and remove xml/getter/setter – ray.10may Jul 02 '20 at 08:11
  • Does this answer your question? [EntityManager is null](https://stackoverflow.com/questions/17835042/entitymanager-is-null) – perissf Jul 02 '20 at 08:16
  • @perissf Thanks for your reply but it couldn't help me, I even tried with stateLessLocalBean = (EjbSBPersistStatelessLocal)intialContext.lookup("java:module/EjbSBPersistStateless!com.tanmshar.ejbcomppersistsrvr.EjbSBPersistStatelessLocal") instead of new EjbSBPersistStateless() but no luck, may be I've missed something, could you please help. – ray.10may Jul 02 '20 at 10:15

0 Answers0