The functionality implemented is to get the projects which have their amountContributed field >= target field and display the list of these projects on screen. This worked originally before I tried to refactor the controller to use a service for SoC and readability. A null error is now thrown with the new implementation when the user navigates to that route, when before it displayed the project(s) on screen correctly.
Any help would be greatly appreciated, thanks.
Old controller:
@GetMapping("/user/{userID}/projects/target-reached")
private String getUsersContributedProjectsThatReachedTarget(@PathVariable String userID, Model model) {
//Get contributions from userID.
List<Contribution> contributionList = contributionRepo.findAllByContributorUserID(Long.parseLong(userID));
System.out.println(contributionList.get(0).toString());
//Get projects from contributions.
ArrayList<Project> projectList = new ArrayList<Project>();
//For each contribution add the corresponding project to the projectList.
for (Contribution contribution : contributionList) {
Project project = projectRepo.findByProjectID(contribution.getProjectID());
System.out.println(project.toString());
int amountContributed = project.getAmountContributed();
int target = Integer.parseInt(project.getTargets());
System.out.println(amountContributed + " " + target);
//If the target has been reached add the project to the projectList.
if (amountContributed >= target) {
projectList.add(project);
}
}
//Add projectList to model.
model.addAttribute(projectList);
return "target-reached-projects";
}
New controller + service:
@GetMapping("/user/{userID}/projects/target-reached")
private String getUsersContributedProjectsThatReachedTarget(@PathVariable String userID, Model model) {
ProjectService projectService = new ProjectService();
ArrayList<Project> projectList = projectService.listProjectsReachedTarget(userID);
//Add projectList to model.
model.addAttribute(projectList);
return "target-reached-projects";
}
public ArrayList<Project> listProjectsReachedTarget(String userID) {
//Get contributions from userID.
List<Contribution> contributionList = contributionRepo.findAllByContributorUserID(Long.parseLong(userID));
System.out.println(contributionList.get(0).toString());
//Get projects from contributions.
ArrayList<Project> projectList = new ArrayList<Project>();
//For each contribution add the corresponding project to the projectList.
for (Contribution contribution : contributionList) {
Project project = projectRepo.findByProjectID(contribution.getProjectID());
System.out.println(project.toString());
int amountContributed = project.getAmountContributed();
int target = Integer.parseInt(project.getTargets());
System.out.println(amountContributed + " " + target);
//If the target has been reached add the project to the projectList.
if (amountContributed >= target) {
projectList.add(project);
}
}
return projectList;
}
Error:
java.lang.NullPointerException: null
at com.funding.crowdfunding.domain.ProjectService.listProjectsReachedTarget(ProjectService.java:30) ~[main/:na]
at com.funding.crowdfunding.web.ProjectsController.getUsersContributedProjectsThatReachedTarget(ProjectsController.java:140) ~[main/:na]