Please, help me with this, is an exercise proposed by me, which is to make an ecommerce page, surely, payments, everything, but I'm stuck on this problem., since I already use the JSP relatively, I wanted to get into the use of JSF and propose challenges, I know that it can be done directly with the Managed Bean all the interaction with the database, but I would like to add another layer to the exercise, that's where the servlet appears, and make my bean to connect between the JSF, and the Servlet, but it does not stop working, it gives me an error of null .
DP: I would like you to recommend a JavaEE book to me in depth, please, sorry for my bad English, it is not my language.
This is the servlet.
enter code here
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("---------------------Begin-----------------------------------------------");
try {
Section_Model sm = (Section_Model) request.getSession().getAttribute("section_Model");
sm.getSection().setNameAlias(request.getParameter("nameAlias"));
sm.getSection().setCodeSS((Sections) sm.findSpecific(Integer.parseInt(request.getParameter("codeSS"))));
sm.getSection().setVisible(Boolean.getBoolean(request.getParameter("visible")));
sm.create();
request.getRequestDispatcher("WEB-INF/testJSF.xhtml").forward(request, response);
} catch (Exception ex) {
Logger.getLogger(inputSections.class.getName()).log(Level.SEVERE, null, ex);
}
}
this is Managed Bean.
@Named(value = "section_Model")
@SessionScoped
public class Section_Model implements Serializable {
@Inject
private Section_Model section_Model;
@EJB
private SectionsFacadeLocal sectionsFacade;
@PersistenceContext(unitName = "ShopOnlinePU")
private EntityManager em;
@Resource
private javax.transaction.UserTransaction utx;
Sections section;
SectionsJpaController controller;
/**
* Creates a new instance of Section_Model
*/
public Section_Model() {
section= new Sections();
}
public Sections getSection() {
return section;
}
public void setSection(Sections section) {
this.section = section;
}
public void init(){
controller=new SectionsJpaController(utx, em.getEntityManagerFactory());
}
public void create () throws Exception{
init();
controller.create(section);
}
public List<Sections> select_all(){
init();
return controller.findSectionsEntities();
}
public void create2(){
sectionsFacade.create(section);
}
public List<Entity.Sections> subSections (Sections s){
return sectionsFacade.subSection(s);
}
public List<Entity.Sections> findSpecific(int id){
return sectionsFacade.findSpecificS(1);
}
public Collection<Entity.Sections> topSection (){
return sectionsFacade.TopSection();
}
public int countSection(){
return sectionsFacade.count();
}
This is JSF
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<h:head>
<title>TEST Title</title>
</h:head>
<h:body>
<form action="inputSections" method="post">
<input type="text" name="nameAlias" title="NameAlias" required="true" requiredMessage="The NameAlias field is required."/>
<input type="text" name="codeSS" title="CodeSS" />
<input type="text" name="visible" title="Visible" />
<input type="submit" value="Ok"/>
</form>
<c:forEach items="#{section_Model.findSpecific(1)}" var="item">
<f:view>
<h:form>
<h:dataTable value="#{section_Model.subSections(item)}" var="items">
<h:column>
<f:facet name="header">
<h:outputText value="CodeS"/>
</f:facet>
<h:outputText value="#{items.codeS}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="NameAlias"/>
</f:facet>
<h:outputText value="#{items.nameAlias}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Visible"/>
</f:facet>
<h:outputText value="#{items.visible}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="CodeSS"/>
</f:facet>
<h:outputText value="#{items.codeSS}"/>
</h:column>
</h:dataTable>
</h:form>
</f:view>
</c:forEach>
</h:body>
</html>
The error is
GRAVE: java.lang.NullPointerException
at View.inputSections.doPost(inputSections.java:79)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:706)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1622)
at `org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:824)`