Reputation: 271
Trying to simply output a list of all employees in a view report from a controller in Spring. Getting an error relating to Freemarker, unsure of how to resolve.
Relevant classes:
Employee.java model
package mvc_course.models;
import java.util.HashSet;
import java.util.Set;
public class Employee {
int employee_number;
String employee_name;
String address;
String ni_number;
String iban_number;
double starting_salary;
int employee_type_id;
int commission_rate;
int total_sales;
public static Set<Employee> employeeList = new HashSet<Employee>();
public Employee(String name, String address, String nin, String iban, double salary) {
this.employee_name = name;
this.address = address;
this.ni_number = nin;
this.iban_number = iban;
this.starting_salary = salary;
}
public int getEmployee_number() {
return employee_number;
}
public void setEmployee_number(int employee_number) {
this.employee_number = employee_number;
}
public String getEmployee_name() {
return employee_name;
}
public void setEmployee_name(String employee_name) {
this.employee_name = employee_name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getNi_number() {
return ni_number;
}
public void setNi_number(String ni_number) {
this.ni_number = ni_number;
}
public String getIban_number() {
return iban_number;
}
public void setIban_number(String iban_number) {
this.iban_number = iban_number;
}
public double getStarting_salary() {
return starting_salary;
}
public void setStarting_salary(double starting_salary) {
this.starting_salary = starting_salary;
}
public int getEmployee_type_id() {
return employee_type_id;
}
public void setEmployee_type_id(int employee_type_id) {
this.employee_type_id = employee_type_id;
}
public int getCommission_rate() {
return commission_rate;
}
public void setCommission_rate(int commission_rate) {
this.commission_rate = commission_rate;
}
public int getTotal_sales() {
return total_sales;
}
public void setTotal_sales(int total_sales) {
this.total_sales = total_sales;
}
}
ReportController.java
package mvc_course.controllers;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import mvc_course.models.Employee;
@Controller
public class ReportController {
@Autowired
private DataSource dataSource;
@RequestMapping(value="showEmployees.mvc")
public String showEmployees(Model m){
List<Employee> employees = new ArrayList<Employee>();
try{
Connection c = dataSource.getConnection();
Statement s = c.createStatement();
String sql = "SELECT * FROM Employees";
ResultSet rs = s.executeQuery(sql);
List<String[]>rows = new ArrayList<String[]>();
while(rs.next()){
String[] row = {
rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getString(6)};
rows.add(row);
}
for (String[] row : rows) {
Employee e = new Employee(row[1], row[2], row[3], row[4], Double.parseDouble(row[5]));
employees.add(e);
System.out.println();
for (String string : row) {
System.out.print(string + " ");
}
}
m.addAttribute("employees");
}catch (Exception e){
System.out.println(e.getMessage());
}
return "EmployeesPerBuReport";
}
}
**EmployeeReport.ftl**
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Employees per BU</title>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css">
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/js/materialize.min.js"></script>
</head>
<body>
<h1>Employees</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>National Insurance Number</th>
<th>Bank Account Number</th>
<th>Starting Salary</th>
</tr>
</thead>
<tbody>
<#list employees as employee>
<tr>
<td>${employee.getName}</td>
<td>${employee.getAddress}</td>
<td>${employee.getNi_number}</td>
<td>${employee.getIban_number}</td>
<td>${employee.getStarting_salary}</td>
</tr>
</#list>
</tbody>
</table>
</body>
</body>
</html>
Error:
FreeMarker template error (DEBUG mode; use RETHROW in production!): The following has evaluated to null or missing: ==> employee.getName [in template "EmployeesPerBuReport.ftl" at line 31, column 39] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${employee.getName} [in template "EmployeesPerBuReport.ftl" at line 31, column 37
Upvotes: 1
Views: 3962
Reputation: 271
Solved.
After changing getters and setters I resolved the problem by changing
<#list employees as employee>
<tr>
<td>${employee.getName}</td>
<td>${employee.getAddress}</td>
<td>${employee.getNi_number}</td>
<td>${employee.getIban_number}</td>
<td>${employee.getStarting_salary}</td>
</tr>
</#list>
to
<#list employees as e>
<tr>
<td>${e.name}</td>
<td>${e.address}</td>
<td>${e.ni_number}</td>
<td>${e.iban_number}</td>
<td>${e.starting_salary}</td>
</tr>
</#list>
Not sure why, but this seemed to fix. Problem was mostly down to badly named getters and field names though.
Upvotes: 1
Reputation: 31162
You don't have getName()
method in the Employee
class, you only have a getEmployee_name()
(a quite strange name). After you have fixed that, you will get a different error, which you can fix if you write ${employee.name}
instead of ${employee.getName}
.
Upvotes: 3