Witam siedzę już cały dzień przy jednej linijce i nie mogę pozbyć się błędu zaznaczonego poniżej. Błagam o pomoc.
java.lang.RuntimeException: Uncompilable source code - variable customer might not have been initialized
edu.wskiz.ajaxzp.r02.Program.doPost(Program.java:41)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Program.java
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/plain; charset=UTF-8");
CustomerService cust = new CustomerService();
Customer customer = cust.create(customer); <b>//linia 41 błąd: variable customer might not have been inicialized</b>
String Name = request.getParameter("txtName");
customer.setName(Name);
String Address = request.getParameter("txtAddress");
customer.setAddress(Address);
String City = request.getParameter("txtCity");
customer.setCity(City);
String State = request.getParameter("txtState");
customer.setState(State);
String Zip = request.getParameter("txtZip");
customer.setZip(Zip);
String Phone = request.getParameter("txtPhone");
customer.setPhone(Phone);
String Email = request.getParameter("txtEmail");
customer.setEmail(Email);
out.print("Dodano klienta o identyfikatorze: "+customer.getCustomerId());
}
CustomerService.java
public Customer create(Customer customer) throws DAOException {
Connection c = null;
PreparedStatement ps = null;
try {
c = ConnectionHelper.getConnection();
ps = c.prepareStatement("INSERT INTO Customers(Name,Address,City,State,Zip,Phone,`E-mail`) VALUES (?, ?, ?, ?, ?, ?)");
ps.setString(1, customer.getName());
ps.setString(2, customer.getAddress());
ps.setString(3, customer.getCity());
ps.setString(4, customer.getState());
ps.setString(5, customer.getZip());
ps.setString(6, customer.getPhone());
ps.setString(7, customer.getEmail());
ps.executeUpdate();
Statement s = c.createStatement();
// MySQL Syntax to get the identity (product_id) of inserted row
ResultSet rs = s.executeQuery("SELECT LAST_INSERT_ID()");
rs.next();
// Update the id in the returned object. This is important as this
// value must get returned to the client.
customer.setCustomerId(rs.getInt(1));
} catch (Exception e) {
e.printStackTrace();
throw new DAOException(e);
} finally {
ConnectionHelper.close(c);
}
return customer;
}