0

I am trying to learn EJB by refering to this course : https://www.udemy.com/share/101soaA0QcdVpQQ3w=/

I have my model class as such :


@Stateless
@LocalBean
public class Flights {

    private String flightId = "LA123";
    private String origin = "Houston";
    private String destination = "Los Angeles";
    private String airplaneModel = "Boeing 737";



    public String getFlightId(){
        return flightId;
    }

    public void setFlightId(String flightId) {
        this.flightId = flightId;
    }

    public String getOrigin() {
        return origin;
    }

    public void setOrigin(String origin) {
        this.origin = origin;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getAirplaneModel() {
        return airplaneModel;
    }

    public void setAirplaneModel(String airplaneModel) {
        this.airplaneModel = airplaneModel;
    }

    @Override
    public String toString() {
        return "Flights{" +
                "flightId=" + flightId +
                ", origin='" + origin + '\'' +
                ", destination='" + destination + '\'' +
                ", airplaneModel='" + airplaneModel + '\'' +
                '}';
    }
}

I keep receiving NPE when either trying to get or set values in the model class.

I am building the application on Intellij with Tomcat Server.

And here is my servlet where I have injected EJB as such:

@WebServlet("/flights")
public class BookedFlights extends HttpServlet {


    @EJB
    private Flights flights;

    public BookedFlights()
    {
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {

        PrintWriter out = response.getWriter();
        out.println("The flights details servlet has been called...");
        out.println(flights.getAirplaneModel());                        //NPE
       out.println(flights.getOrigin());                                //NPE

        request.getRequestDispatcher("/WEB-INF/flights.jsp")
               .forward(request,response);
    }
}
user1462617
  • 413
  • 1
  • 13
  • 23
  • 1
    Tomcat is only a servlet container, it cannot run EJBs (there is s special version TomEE that can, I haven't followed it). The course you link specifically mentions Glassfish which is a full blown JEE application server and can run EJBs. – Nikos Paraskevopoulos May 10 '21 at 06:31

1 Answers1

-1

Are you first sending a POST request to set the data ? I can only see a GET request here. Also, while sending a POST request and upon a status code of 200 or 201, you need to call setters of your model class. Clear if I am misled somewhere please. :)

mr-possible
  • 109
  • 1
  • 9
  • no I am not sending a POST request. The variables in the model class have hard coded values. I am just trying to get those as output. – user1462617 May 10 '21 at 04:29
  • Got this from tutorialspoint.com - “A stateless session bean is a type of enterprise bean, which is normally used to perform independent operations. A stateless session bean as per its name does not have any associated client state, but it may preserve its instance state. EJB Container normally creates a pool of few stateless bean's objects and use these objects to process client's request. Because of pool, instance variable values are not guaranteed to be same across lookups/method calls. “ You might wanna try removing @Stateless annotation ? – mr-possible May 10 '21 at 04:31