大家好!
我正在使用Hibernate Crud运行Maven Web应用程序以实现不同的关系。我有一对多和多对一的关系。 它分为三类:ServiceRequest,Party和ContactHistory。对方的ServiceRequest是多对一的,对方历史记录的一方是一对一的。 我可以为表Party和ContactHistory添加表,尽管Servicerequest表不会将数据添加到数据库中。 当我单击Show.jsp时,我得到:org.apache.jasper.JasperException:java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:579)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:495)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
controller.CallCenterController.doPost(CallCenterController.java:120)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.NullPointerException
org.apache.jsp.Show_jsp._jspService(Show_jsp.java:259)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
controller.CallCenterController.doPost(CallCenterController.java:120)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
我试图确保我对属性的命名正确,并且还试图注释servicerequest代码以获取和设置参数,以找出问题出在哪里。我似乎无法弄清楚问题所在。我需要一些帮助。
@Entity @Table(name="ServiceRequest") public class ServiceRequest{
private static final long serialVersionUID = 1L;
@Id @TableGenerator(name="tab", initialValue=10000) @GeneratedValue(strategy=GenerationType.TABLE, generator="tab") @Column(name="customerId") private int customerId;
@Column(name="customerName") private String customerName;
@Column(name="dateOfCall") private String dateOfCall;
@Column(name="requestContent") private String requestContent;
@ManyToOne(targetEntity=Party.class, cascade=CascadeType.ALL) @JoinColumn(name="firstName",referencedColumnName="firstName") private Party party;
@ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="cellPhoneNumber",referencedColumnName="cellPhoneNumber") private ContactHistory contactHistory;
public int getCustomerId() { return customerId; }
public void setCustomerId(int customerId) { this.customerId = customerId; }
public String getCustomerName() { return customerName; }
public void setCustomerName(String customerName) { this.customerName = customerName; }
public String getDateOfCall() { return dateOfCall; }
public void setDateOfCall(String dateOfCall) { this.dateOfCall = dateOfCall; }
public String getRequestContent() { return requestContent; }
public void setRequestContent(String requestContent) { this.requestContent = requestContent; }
public Party getParty() { return party; }
public void setParty(Party party) { this.party = party; }
public ContactHistory getContactHistory() { return contactHistory; }
public void setContactHistory(ContactHistory contactHistory) { this.contactHistory = contactHistory; }
public ServiceRequest(int customerId, String customerName, String dateOfCall, String requestContent, Party party, ContactHistory contactHistory) { super(); this.customerId = customerId; this.customerName = customerName; this.dateOfCall = dateOfCall; this.requestContent = requestContent; this.party = party; this.contactHistory = contactHistory; }
public ServiceRequest() { super(); }
@Override public String toString() { return "ServiceRequest [customerId=" + customerId + ", customerName=" + customerName + ", dateOfCall=" + dateOfCall + ", requestContent=" + requestContent + ", party=" + party + ", contactHistory=" + contactHistory + "]"; }
}
@Entity
@Table(name="Partys")
public class Party {
private static final long serialVersionUID = -1798070786993154676L;
@Id
@Column(name="firstName")
private String firstName;
@Column(name="lastName")
private String lastName;
@Column(name="emailAddress")
private String emailAddress;
@OneToMany(cascade=CascadeType.ALL)
private Set<ServiceRequest> serviceRequest;
@OneToOne(cascade=CascadeType.ALL)
private ContactHistory contactHistory;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public Set<ServiceRequest> getServiceRequest() {
return serviceRequest;
}
public void setServiceRequest(Set<ServiceRequest> serviceRequest) {
this.serviceRequest = serviceRequest;
}
public ContactHistory getContactHistory() {
return contactHistory;
}
public void setContactHistory(ContactHistory contactHistory) {
this.contactHistory = contactHistory;
}
public Party(String firstName, String lastName, String emailAddress, Set<ServiceRequest> serviceRequest,
ContactHistory contactHistory) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.emailAddress = emailAddress;
this.serviceRequest = serviceRequest;
this.contactHistory = contactHistory;
}
public Party() {
super();
}
@Override
public String toString() {
return "Party [firstName=" + firstName + ", lastName=" + lastName + ", emailAddress=" + emailAddress
+ ", serviceRequest=" + serviceRequest + ", contactHistory=" + contactHistory + "]";
}
}
@Entity
@Table(name="ContactHistorys")
public class ContactHistory {
private static final long serialVersionUID = 1354545L;
@Id
@Column
private int cellPhoneNumber;
@Column
private int telephoneNumber;
@OneToOne(targetEntity=Party.class, cascade=CascadeType.ALL)
@JoinColumn(name="firstName",referencedColumnName="firstName")
private Party party;
@OneToMany(cascade=CascadeType.ALL)
private Set<ServiceRequest> servicerequest;
public int getCellPhoneNumber() {
return cellPhoneNumber;
}
public void setCellPhoneNumber(int cellPhoneNumber) {
this.cellPhoneNumber = cellPhoneNumber;
}
public int getTelephoneNumber() {
return telephoneNumber;
}
public void setTelephoneNumber(int telephoneNumber) {
this.telephoneNumber = telephoneNumber;
}
public Party getParty() {
return party;
}
public void setParty(Party party) {
this.party = party;
}
public Set<ServiceRequest> getServicerequest() {
return servicerequest;
}
public void setServicerequest(Set<ServiceRequest> servicerequest) {
this.servicerequest = servicerequest;
}
public ContactHistory(int cellPhoneNumber, int telephoneNumber, Party party, Set<ServiceRequest> servicerequest) {
super();
this.cellPhoneNumber = cellPhoneNumber;
this.telephoneNumber = telephoneNumber;
this.party = party;
this.servicerequest = servicerequest;
}
public ContactHistory() {
super();
}
@Override
public String toString() {
return "ContactHistory [cellPhoneNumber=" + cellPhoneNumber + ", telephoneNumber=" + telephoneNumber
+ ", party=" + party + ", servicerequest=" + servicerequest + "]";
}
}
public class CallCenterDAOImpl implements CallCenterDAO {
SessionFactory sessionFactory;
public CallCenterDAOImpl() {
this.sessionFactory = new Configuration().configure().buildSessionFactory();
}
public void saveCustomer(ContactHistory contactHistory) {
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.save(contactHistory);
transaction.commit();
session.close();
}
public List<ContactHistory> showAllCustomer() {
List<ContactHistory> callcenterList = new ArrayList();
Session session = sessionFactory.openSession();
Query query = session.createQuery("From ContactHistory");
callcenterList = query.list();
return callcenterList;
}
public void updateCustomer(int CustomerId, String CustomerName, String DateOfCall, String RequestContent) {
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
ServiceRequest callcenter = (ServiceRequest)session.load(ServiceRequest.class, CustomerId);
callcenter.setCustomerName(CustomerName);
callcenter.setRequestContent(RequestContent);
session.update(callcenter);
transaction.commit();
session.close();
}
public boolean deleteCustomer(ServiceRequest callcenter) {
boolean delete = false;
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.delete(callcenter);
delete = true;
transaction.commit();
session.close();
System.out.println(delete);
return delete;
}
}
/**
* Servlet implementation class Call_CenterController
*/
public class CallCenterController extends HttpServlet {
private static final long serialVersionUID = 102831979L;
CallCenterDAO callcenterDao;
@Override
public void init() throws ServletException {
callcenterDao = new CallCenterDAOImpl();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("addCustomer")!=null){
String firstName= request.getParameter("firstName");
String lastName= request.getParameter("lastName");
String emailAddress= request.getParameter("emailAddress");
Party party=new Party();
party.setFirstName(firstName);
party.setLastName(lastName);
party.setEmailAddress(emailAddress);
int customerId = 0;
String customerName = request.getParameter("customerName");
String requestContent=request.getParameter("requestContent");
ServiceRequest callcenter = new ServiceRequest();
callcenter.setCustomerId(customerId);
callcenter.setCustomerName(customerName);
callcenter.setRequestContent(requestContent);
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String dateOfCall = dateFormat.format(date);
callcenter.setDateOfCall(dateOfCall);
System.out.println(date.toString());
int cellPhoneNumber=Integer.parseInt(request.getParameter("cellPhoneNumber"));
int telephoneNumber=Integer.parseInt(request.getParameter("telephoneNumber"));
ContactHistory contacthistory=new ContactHistory();
contacthistory.setCellPhoneNumber(cellPhoneNumber);
contacthistory.setTelephoneNumber(telephoneNumber);
contacthistory.setParty(party);
callcenterDao.saveCustomer(contacthistory);
System.out.println("here it is:" +contacthistory);
RequestDispatcher rd=request.getRequestDispatcher("CallCenterAdd.jsp");
rd.forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("Show")!=null)
{
List<ContactHistory> callcenterList = new ArrayList<ContactHistory>();
CallCenterDAO callcenterDaoImpl = new CallCenterDAOImpl();
callcenterList = callcenterDaoImpl.showAllCustomer();
request.setAttribute("callcenterList", callcenterList);
RequestDispatcher rd = request.getRequestDispatcher("Show.jsp");
rd.forward(request, response);
/*List<Passport> callcenterList = new ArrayList<Passport>();
CallCenterDAO callcenterDaoImpl = new CallCenterDAOImpl();
callcenterList = callcenterDaoImpl.showAllCustomer();
request.setAttribute("callcenterList", callcenterList);
RequestDispatcher rd = request.getRequestDispatcher("Show.jsp");
rd.forward(request, response);*/
}
else if(request.getParameter("updateCustomer")!=null){
int CustomerId = Integer.parseInt(request.getParameter("customerId"));
String CustomerName = request.getParameter("customerName");
String RequestContent=request.getParameter("requestContent");
ServiceRequest callcenter = new ServiceRequest();
callcenter.setCustomerName(CustomerName);
callcenter.setRequestContent(RequestContent);
callcenter.setCustomerId(CustomerId);
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String dateOfCall = dateFormat.format(date);
callcenter.setDateOfCall(dateOfCall);
System.out.println(date.toString());
callcenterDao.updateCustomer(CustomerId, CustomerName, dateOfCall, RequestContent);
System.out.println("here it is: " +callcenter);
RequestDispatcher rd = request.getRequestDispatcher("CallCenterAdd.jsp");
rd.forward(request, response);
}
else if(request.getParameter("deleteCustomer")!=null){
int CustomerId = Integer.parseInt(request.getParameter("CustomerId"));
ServiceRequest callcenter=new ServiceRequest();
callcenter.setCustomerId(CustomerId);
boolean deleted = callcenterDao.deleteCustomer(callcenter);
if(deleted)
{
List<ContactHistory> callcenterList = new ArrayList<ContactHistory>();
CallCenterDAO callcenterDaoImpl = new CallCenterDAOImpl();
callcenterList = callcenterDaoImpl.showAllCustomer();
request.setAttribute("callcenterList", callcenterList);
request.setAttribute("Success", "User successfully deleted");
RequestDispatcher rd = request.getRequestDispatcher("Show.jsp");
rd.forward(request, response);
}
else
{
List<ContactHistory> callcenterList = new ArrayList<ContactHistory>();
CallCenterDAO callcenterDaoImpl = new CallCenterDAOImpl();
callcenterList = callcenterDaoImpl.showAllCustomer();
request.setAttribute("callcenterList", callcenterList);
request.setAttribute("Error", "User not successfully deleted");
RequestDispatcher rd = request.getRequestDispatcher("Show.jsp");
rd.forward(request, response);
}
RequestDispatcher rdd = request.getRequestDispatcher("CallCenterAdd.jsp");
rdd.forward(request, response);
}
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Call_Center</title>
<style>
body {
background-color:beige;
}
h1 {
color:goldenrod;
text-align: center;
}
</style>
</head>
<head>
<title>CSS Website Layout</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
}
/* Style the header */
.header {
padding: 5px;
background-color:beige;
text-align: center;
}
</style>
</head>
<body>
<div class="header">
<h1>ADD CUSTOMER</h1>
</div>
</body>
</html>
<style>
.footer {
background-color: #F1F1F1;
text-align: center;
margin-top:200px;
padding: 20px;
}
</style>
<div id="add">
<!-- <h1 align="center"> ADD CUSTOMER </h1> -->
<form action="CallCenterController" method="GET">
<table>
<td>Customer Name:</td>
<td><input type="text" name="customerName" placeholder="Enter CustomerName" ></td>
<tr>
<td>Request Content: </td>
<td><input type="text" name="requestContent" placeholder="Enter Request Content" ></td>
</tr>
<tr>
<td>First Name: </td>
<td><input type="text" name="firstName" placeholder="Enter First Name" ></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type="text" name="lastName" placeholder="Enter Last Name" ></td>
</tr>
<tr>
<td>Email Address: </td>
<td><input type="text" name="emailAddress" placeholder="Enter Email Address" ></td>
</tr>
<tr>
<td>Cellphone Number: </td>
<td><input type="text" name="cellPhoneNumber" placeholder="Enter CellPhone Number" ></td>
</tr>
<tr>
<td>Telephone Number: </td>
<td><input type="text" name="telephoneNumber" placeholder="Enter Telephone Number" ></td>
</tr>
<td><input type="submit" name="addCustomer" value="addCustomer"></td>
</table>
</form>
</div>
<form action="CallCenterController" method="POST">
<input type="submit" name="Show" value="Show"> <br>
</form>
<div class="footer">
<p>Designed by Yashlin Naidoo</p>
</div>
</body>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.tcs.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style>
body {
background-color: beige;
}
h1 {
color: goldenrod;
text-align: center;
}
</style>
</head>
<%
String Success = (String) request.getAttribute("Success");
if (Success == null) {
Success = " ";
}
%>
</head>
<body>
<header>
<p align="center"></p>
<h1 align="center">CALL CENTER DATABASE</h1>
<p><%=Success%></p>
</header>
<%
ArrayList<ContactHistory> list = (ArrayList<ContactHistory>) request.getAttribute("callcenterList");
%>
<form action="CallCenterController" method="POST">
<table align="center" border=1>
<tr>
<th>First Name</th>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Date Of Call</th>
<th>RequestContent</th>
</tr>
<%
for (ContactHistory s : list) {
%>
<%
Iterator<ServiceRequest> iterator = s.getServicerequest().iterator();
%>
<%
while (iterator.hasNext())
{
%>
<tr>
<td><%=iterator.next().getParty().getFirstName()%></td>
<td><%=iterator.next().getCustomerId()%></td>
<td><%=iterator.next().getCustomerName()%></td>
<td><%=iterator.next().getDateOfCall()%></td>
<td><%=iterator.next().getRequestContent()%></td>
</tr>
<%
}
%>
<%
}
%>
</table>
<table align="center" border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Emailaddress</th>
</tr>
<%
for (ContactHistory s : list) {
%>
<tr>
<td><%=s.getParty().getFirstName()%></td>
<td><%=s.getParty().getLastName()%></td>
<td><%=s.getParty().getEmailAddress()%></td>
</tr>
<%
}
%>
</table>
<table align="center" border=1>
<tr>
<th>First Name</th>
<th>Cellphone Number</th>
<th>Telephone Number</th>
<tr>
<%
for (ContactHistory s : list) {
%>
<tr>
<td><%=s.getParty().getFirstName()%></td>
<td><%=s.getCellPhoneNumber()%></td>
<td><%=s.getTelephoneNumber()%></td>
</tr>
<%
}
%>
</table>
<label for="customerId">Customer ID</label>
<select id="customerId" name="customerId">
<%
for (ContactHistory s : list) {
%>
<%
Iterator<ServiceRequest> iterator = s.getServicerequest().iterator();
%>
<%
while (iterator.hasNext())
{
%>
<option value=<%=iterator.next().getCustomerId()%>><%=iterator.next().getCustomerId()%></option>
<%
}
%>
<%
}
%>
</select>
Update Customer Name:<input id="CustomerName" type="text" name="CustomerName"> <br><br>
Update Request Content:<input id="RequestContent" type="text" name="RequestContent"> <br><br>
<div>
<button type="submit" name="updateCustomer" value="updateCustomer">Update</button> <br> <br>
<button type="submit" name="deleteCustomer" value="deleteCustomer">Delete</button>
</div>
<a href="CallCenterAdd.jsp">Back</a>
</form>
</body>
</html>