0

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]

enter image description here

Jack Goodwin
  • 99
  • 1
  • 6
  • 1
    share exception/error trace – Vaibs Aug 05 '21 at 13:13
  • @Vaibs I edited the post with the error, sorry. – Jack Goodwin Aug 05 '21 at 13:17
  • 1
    First thing, why the method is private? Make it public and try, anyway it is getting called from client. Try to autowire ProjectService and try? – Vaibs Aug 05 '21 at 13:22
  • @Vaibs I tried to make it public but it still doesn't work and ProjectService is already auto wired. The method is being called too because I can print "Hello world" at the top of it. – Jack Goodwin Aug 05 '21 at 13:33
  • 1
    you are saying you are able to call listProjectReachedTarget method. But error shows something different. It's weired. contributionRepo,projectRepo object has created? You can share the repo link, so I can try on my machine. – Vaibs Aug 05 '21 at 13:38
  • @Vaibs I have added a picture to the question showing the System.out.pritntln() being called within the method and in the console. That is not possible unfortunately, thanks though. – Jack Goodwin Aug 05 '21 at 13:55
  • contributionRepo object is null.Please check whether componentScan , scans that package and contriRepo class is annotated with @service/@component. If not place annotation on class. – Vaibs Aug 05 '21 at 13:58
  • @Vaibs How would I check componentScan? I tried to add service and then component to contributionrepo but it hasn't effected anything, thanks. – Jack Goodwin Aug 05 '21 at 14:21
  • https://www.baeldung.com/spring-component-scanning check this. – Vaibs Aug 05 '21 at 14:27

0 Answers0