I'm working on a JSF database management system project using Primefaces and I'm currently trying to create a page that has two data selection tables. There will be two different data tables in the page, from one of them user will be able to select multiple data while from the other one user can only select one. After the selection backing bean takes the needed values from both of the selected rows and creates a new data for a third different table. The third different table is also available in the page as a data table with no selection. Unfortunately in my design the backing bean cannot take the value of the selected row of the second data table while it successfully takes multiple selected rows from the first table. Here is my code:
This is the xhtml code for the page:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Ogrenci-Ders Atama Sayfasi</title>
</h:head>
<h:body>
<h:form id="form">
<h:outputText id="msgcomponent" binding="#{msgBean.component}" />
<p:messages showDetail="true" showSummary="true" autoUpdate="true" />
<p:dataTable id="lessonTable" var="lesson"
value="#{lessonView.lessons}" selectionMode="multiple"
selection="#{StudentLessonBean.selectedLessons}"
rowKey="#{lesson.lessonId}" scrollable="true" scrollHeight="150">
<f:facet name="header">
Dersler
</f:facet>
<p:column headerText="Id">
<h:outputText value="#{lesson.lessonId}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{lesson.lessonName}" />
</p:column>
</p:dataTable>
</h:form>
<h:form id="studentTableForm">
<p:dataTable id="studentTable" var="student"
value="#{studentView.students}" selectionMode="single"
selection="#{StudentLessonBean.selectedStudent}"
rowKey="#{student.studentId}" scrollable="true" scrollHeight="150">
<f:facet name="header">
Öğrenciler
</f:facet>
<p:column headerText="Id">
<h:outputText value="#{student.studentId}" />
</p:column>
<p:column headerText="Number">
<h:outputText value="#{student.studentNumber}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{student.studentName}" />
</p:column>
<p:column headerText="Class">
<h:outputText value="#{student.studentClass}" />
</p:column>
</p:dataTable>
</h:form>
<p:commandButton icon="ui-icon-arrowthick-2-e-w"
action="#{StudentLessonBean.InsertStudentLesson()}">
</p:commandButton>
<h:form id="kullanilmayan">
<p:dataTable var="studentLesson"
value="#{StudentLessonBean.studentLessons}"
widgetVar="studentLessonsTable"
emptyMessage="No students found with given criteria"
filteredValue="#{StudentLessonBean.filteredStudentLessons}">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search all fields:" />
<p:inputText id="globalFilter"
onkeyup="PF('studentLessonsTable').filter()"
style="width:150px"
placeholder="Enter keyword" />
</p:outputPanel>
</f:facet>
<p:column filterBy="#{studentLesson.studentLessonId}"
headerText="Student-Lesson Id" footerText="contains"
filterMatchMode="contains">
<h:outputText value="#{studentLesson.studentLessonId}" />
</p:column>
<p:column filterBy="#{studentLesson.studentId}"
headerText="Student Id" footerText="contains"
filterMatchMode="contains">
<h:outputText value="#{studentLesson.studentId}" />
</p:column>
<p:column filterBy="#{studentLesson.lessonId}" headerText="Lesson Id"
footerText="contains" filterMatchMode="contains">
<h:outputText value="#{studentLesson.lessonId}" />
</p:column>
<p:column filterBy="#{studentLesson.score}" headerText="Score"
footerText="contains" filterMatchMode="contains">
<h:outputText value="#{studentLesson.score}" />
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
Here is the bean for getting the selected rows and use them to insert a new data to a third different table (the problem is right here, the program never runs the setSelectedStudent() function eventually leaving it null and throwing a nullpointerexception when it tries to get the selected row's id):
package bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import dao.StudentLessonInsertion;
import egeproject.ConnectionClass;
import model.Lesson;
import model.Student;
import model.StudentLesson;
@ManagedBean(name = "StudentLessonBean")
@RequestScoped
public class StudentLessonBean {
StudentLessonInsertion studentLessonInsertion;
List<Lesson> selectedLessons;
List<StudentLesson> studentLessons;
List<StudentLesson> filteredStudentLessons;
Student selectedStudent;
public List<Lesson> getSelectedLessons() {
return selectedLessons;
}
public void setSelectedLessons(List<Lesson> selectedLessons) {
this.selectedLessons = selectedLessons;
}
public Student getSelectedStudent() {
return selectedStudent;
}
public void setSelectedStudent(Student selectedStudent) {
this.selectedStudent = selectedStudent;
}
public void InsertStudentLesson() throws SQLException {
studentLessonInsertion = new StudentLessonInsertion();
StudentLesson studentLesson = new StudentLesson();
for (int i = 0; i < selectedLessons.size(); i++) {
studentLesson.setLessonId(selectedLessons.get(i).getLessonId());
studentLesson.setStudentId(selectedStudent.getStudentId());
studentLessonInsertion.InsertStudentLesson(studentLesson);
studentLesson.clear();
}
}
public List<StudentLesson> getFilteredStudentLessons() {
return filteredStudentLessons;
}
public void setFilteredStudentLessons(List<StudentLesson> filteredStudentLessons) {
this.filteredStudentLessons = filteredStudentLessons;
}
public List<StudentLesson> getStudentLessons() throws SQLException {
studentLessons = new ArrayList<StudentLesson>();
ConnectionClass connectionClass = new ConnectionClass();
Connection connection = connectionClass.getDBConnection();
String selectionSql = "SELECT \"Student_Id\", \"Lesson_Id\", \"Score\", \"Student_Lesson_Id\"\r\n"
+ " FROM public.\"Student_Lesson_Table\";\r\n" + "";
try {
PreparedStatement preparedStatement = connection.prepareStatement(selectionSql);
ResultSet results = preparedStatement.executeQuery();
while (results.next()) {
StudentLesson newStudentLesson = new StudentLesson();
newStudentLesson.setStudentId(results.getInt("Student_Id"));
newStudentLesson.setLessonId(results.getInt("Lesson_Id"));
newStudentLesson.setScore(results.getInt("Score"));
newStudentLesson.setStudentLessonId(results.getInt("Student_Lesson_Id"));
studentLessons.add(newStudentLesson);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e);
} finally {
connection.close();
}
return studentLessons;
}
}
Here are the models of the used classes:
Student Model:
package model;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import egeproject.StudentView;
@ManagedBean(name = "Student")
@RequestScoped
public class Student {
private Integer studentId;
private Integer studentNumber;
private String studentName;
private String studentClass;
public Integer getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(Integer studentNumber) {
this.studentNumber = studentNumber;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentClass() {
return studentClass;
}
public void setStudentClass(String studentClass) {
this.studentClass = studentClass;
}
public Student createStudent(int studentNumber, String studentName, String studentClass)
{
this.setStudentNumber(studentNumber);
this.setStudentName(studentName);
this.setStudentClass(studentClass);
return this;
}
public Student returnStudent()
{
return this;
}
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public void clear()
{
this.studentId = null;
this.studentClass = null;
this.studentName = null;
this.studentNumber = null;
}
}
Lesson Model:
package model;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name = "Lesson")
@RequestScoped
public class Lesson {
private Integer lessonId;
private String lessonName;
public String getLessonName() {
return lessonName;
}
public void setLessonName(String lessonName) {
this.lessonName = lessonName;
}
public Lesson createLesson(String lessonName) {
this.setLessonName(lessonName);
return this;
}
public Integer getLessonId() {
return lessonId;
}
public void setLessonId(int lessonId) {
this.lessonId = lessonId;
}
public Lesson returnLesson()
{
return this;
}
public void clear()
{
lessonId = null;
lessonName = null;
}
}
Student Lesson Model:
package model;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name = "StudentLesson")
@RequestScoped
public class StudentLesson {
private Integer studentLessonId;
private Integer studentId;
private Integer lessonId;
private Integer score;
public Integer getStudentLessonId() {
return studentLessonId;
}
public void setStudentLessonId(Integer studentLessonId) {
this.studentLessonId = studentLessonId;
}
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public Integer getLessonId() {
return lessonId;
}
public void setLessonId(Integer lessonId) {
this.lessonId = lessonId;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public StudentLesson createReturn(Student student, Lesson lesson)
{
this.lessonId = lesson.getLessonId();
this.studentId = student.getStudentId();
return this;
}
public void clear()
{
this.lessonId=null;
this.score=null;
this.studentId=null;
this.studentLessonId=null;
}
}
Here is the full stack trace:
WARNING: #{StudentLessonBean.InsertStudentLesson()}: java.lang.NullPointerException
javax.faces.FacesException: #{StudentLessonBean.InsertStudentLesson()}: java.lang.NullPointerException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 29 more
Caused by: java.lang.NullPointerException
at bean.StudentLessonBean.InsertStudentLesson(StudentLessonBean.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 30 more
Thank you, sincerely :) Happy coding