Search code examples
javamultithreadingnullpointerexceptionejbstateless-session-bean

NullPointerException when calling session bean from Thread


I have some problem with my simple exercise program. I want to call a session bean from thread but it become NullPointer exception! but if I call it from a main thread it can run normally

this is warning and exception

Warning: C:\Documents andSettings\User\MyDocuments\NetBeansProjects\ThreadCounter\dist\gfdeploy\ThreadCounter does not exist.
Exception in thread "Thread-2" java.lang.NullPointerException
at threadcounter.Main.run(Main.java:14)
at java.lang.Thread.run(Thread.java:744)

This is runnable class which calling remote session bean

package threadcounter;

import javax.ejb.EJB;
import mysessionbean.CounterSessionBeanRemote;

public class Main implements Runnable {
@EJB
private static CounterSessionBeanRemote counterSessionBean;

@Override
public void run() {
 System.out.println("counter : "+counterSessionBean.getCounter());  //NullPointer
  } 
}

this is my main function

package threadcounter;

public  class Ya {

public static void main(String[] args){
Main t1 = new Main();
new Thread(t1).start();
  }

}

and this is session bean

package mysessionbean;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;

@Stateless
public class CounterSessionBean implements CounterSessionBeanRemote {
private int counter = 0;

@Override
public int getCounter() {
    int temp = counter;
    temp++;
    Random r = new Random();
    try {
    Thread.sleep(r.nextInt(20));
    } catch (InterruptedException ex) {
    Logger.getLogger(CounterSessionBean.class.getName()).log(Level.SEVERE, null, ex);
    }
    counter = temp;
    return counter;
}


}

Solution

  • You cannot inject an EJB with @EJB in a not container manged class like this

    @EJB
    private static CounterSessionBeanRemote counterSessionBean;
    

    Your class should be a container managed to make it work. Otherwise you can do it with jndi lookup

    Context jndiContext = new InitialContext();
    CounterSessionBeanRemote bean = (CounterSessionBeanRemote ) jndiContext.lookup("....");
    

    Another thing is: why would you use @EJB annotation and then assign a value later in the constructor? There is no point in doing this.

    You may want to read through at least this http://docs.oracle.com/javaee/6/tutorial/doc/gipjf.html#girfl to make your simple exercise done well.