0

I have searched around but can't find any help.

I'm trying to create a user but 2 fields keeps being null (firstname and department).

The Gui (html):

<div style="width: 900px; margin-left: auto; margin-right: auto">
        <form action="EmployeesAddManager.jsp" method="post">

            Firstname:<br>
            <input type="text" name="firstname" style="width: 200px"><br>

            Lastname:<br>
            <input type="text" name="lastname" style="width: 200px"><br>

            Gender: 
            <select name="gender">
                <option value="Male">Male</option>
                <option value="Female">Female</option>
            </select><br>

            Email:<br>
            <input type="text" name="email" style="width: 200px"><br>

            Role: 
            <select name="role">
                <option value="Department Leader">Department Leader</option>
                <option value="Assistant">Assistant</option>
            </select><br>

            Department: 
            <select name="department">
                <option value="Tetriz">Tetriz</option>
                <option value="Cube">Cube</option>
            </select><br>

            Image:<br>
            <input type="text" name="image" style="width: 200px"><br>

            Username:<br>
            <input type="text" name="username" style="width: 200px"><br>

            Password:<br>
            <input type="text" name="password" style="width: 200px"><br>

            <input type="submit" value="Create Employee">
        </form>
    </div>

Already in the 'EmployeesAddManager.jsp' I try to print out the inputs, but firstname and department will be null (all the others works):

<%@page import="model.EmployeeModel"%>
<%@page import="model.Employees"%>
<%@ 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=ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <%
        //We get the fulfilled parameters
        String firstname = request.getParameter("firstname");
        String lastname = request.getParameter("lastname");
        String gender = request.getParameter("gender");
        String email = request.getParameter("email");
        String role = request.getParameter("role");
        String department = request.getParameter("department");
        String image = request.getParameter("image");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println("Firstname from EmployeesAddManager.jsp: "+firstname);
        System.out.println("Lastname from EmployeesAddManager.jsp: "+lastname);
        System.out.println("Department from EmployeesAddManager.jsp: "+department);

        //We instantiate an employee and set the parameters
        Employees emp = new Employees(0, firstname, lastname, gender, email, role, department, image, username, password);

        //We call for the method for creating a new employee, and send the new instantiated employee 
        EmployeeModel empModel = new EmployeeModel();
        empModel.newEmployee(emp);

         /*This method is used to redirect client request to some other location
            for further processing ,the new location is available on different server
            or different context.our web container handle this and transfer the request
            using  browser ,and this request is visible in browser as a new request.
            Some time this is also called as client side redirect.
        */
        response.sendRedirect("/Employees_servlet");
        %>
    </body>
</html>

And here the Model thats connect to the database:

public void newEmployee(Employees emp) throws SQLException{

                    try {
                        PreparedStatement ps = DbModel2.getPreparedStatement("INSERT INTO employee_table (Id_employee, Firstname, Lastname, Gender, Email, RoleId_Fk, DepartmentId_Fk, Image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
                        ps.setInt(1, emp.getId());
                        ps.setString(2, emp.getFirstname());
                        ps.setString(3, emp.getLastname());
                        ps.setString(4, emp.getGender());
                        ps.setString(5, emp.getEmail());
                        ps.setString(6, emp.getRole());
                        ps.setString(7, emp.getDepartment());
                        ps.setString(8, emp.getImage());

                        System.out.println("Id: "+emp.getId());
                        System.out.println("Firstname: "+emp.getFirstname()); 
                        System.out.println("Lastname: "+ emp.getLastname());
                        System.out.println("Gender: "+emp.getGender());
                        System.out.println("Email: "+emp.getEmail());
                        System.out.println("Role: "+emp.getRole());
                        System.out.println("Department: "+emp.getDepartment());
                        System.out.println("Image: "+emp.getImage());
                        ps.executeUpdate();
                        System.out.println("EXECUTED");
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


        }

The Employees.java:

public class Employees {
    private int id;
    private String firstname;
    private String lastname;
    private String gender;
    private String email;
    private String role;
    private String department;
    private String image;
    private String username;
    private String password;


    public Employees(int id, String firstname, String lastname, String gender, String email, String role, String department, String image, String username, String password) {
        this.id = id;
        this.firstname = firstname;
        this.lastname = lastname;
        this.gender = gender;
        this.email = email;
        this.role = role;
        this.department = department;
        this.image = image;
        this.username = username;
        this.password = password;

        System.out.println("this.firstname: "+firstname);
        System.out.println("this.department: "+department);

        System.out.println("getFirstname: "+getFirstname());
        System.out.println("getDepartment: "+getDepartment());
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    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 getGender() {
        return gender;
    }


    public void setGender(String gender) {
        this.gender = gender;
    }


    public String getEmail() {
        return email;
    }


    public void setEmail(String email) {
        this.email = email;
    }


    public String getRole() {
        return role;
    }


    public void setRole(String role) {
        this.role = role;
    }


    public String getDepartment() {
        return department;
    }


    public void setDepartment(String department) {
        this.department = department;
    }


    public String getImage() {
        return image;
    }


    public void setImage(String image) {
        this.image = image;
    }


    public String getUsername() {
        return username;
    }


    public void setUsername(String username) {
        this.username = username;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


}
10
  • table column firstname is not nullable. Commented Feb 24, 2017 at 13:26
  • I know that it's not null able, but i'm typing something in it so it shouldn't be. Commented Feb 24, 2017 at 13:27
  • 1
    show the code of ` new Employees` one argument constructor Commented Feb 24, 2017 at 13:27
  • I have pasted it now Commented Feb 24, 2017 at 13:31
  • 1
    you can see the values on logs? Commented Feb 24, 2017 at 13:33

2 Answers 2

1

Because in getters you have done it like

return department change that to this.department and same goes for firstname

Edit: It worked in your case, as I just wanted to enforce the values using this but I need to understand why it didn't work before.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't think that's the problem because in the getter's method body you are returning the object property.
@drgPP agreed on what you said, I just want him to enforce it, let's see that because everything looks fine and that's the only difference i could see
@DanyalSandeelo it's a bit strange and this is because OP said that he got null for firstname and department before constructing an Employees object, but in the phase when he extracted values from the input fields.
0

I did not try this code and this may sound bit childish but did you try changing the name? Have you checked for white spaces?

1 Comment

I can't see any white spaces, but why will changing the name make any difference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.