1

My Spring Boot program is compiling without any issues, but whenever I click the registration link it throws NullPointerException error. I am really at a loss as to what may be causing this. Given below is the error:-

java.lang.NullPointerException: null
    at com.concretepage.controller.UserInfoController.registration(UserInfoController.java:32) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_171]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_171]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_171]

The UserinfoController class is given below:-

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.concretepage.entity.ProjectLevel;
import com.concretepage.service.LevelService;
import com.concretepage.service.UserInfoService;

@Controller
@RequestMapping("app")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    private LevelService levelService;

    @GetMapping("login")
    public ModelAndView login() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("custom-login");
        return mav;
    }

    @GetMapping("registration")
    public ModelAndView registration() {
        ModelAndView mav = new ModelAndView();
        List<ProjectLevel> levels = levelService.getAllLevels();
        mav.addObject("Levels", levels);
        mav.setViewName("registration");
        return mav;
    }

    @GetMapping("secure/project-details")
    public ModelAndView getAllUserProjects() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("userProjects", userInfoService.getAllUserProjects());
        mav.setViewName("projects");
        return mav;
    }

Error is at the line -

List<ProjectLevel> levels = levelService.getAllLevels();

The LevelServiceImpl class is as below:-

package com.concretepage.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.concretepage.entity.ProjectLevel;
import com.concretepage.repositories.LevelRepository;

@Service
public class LevelServiceImpl implements LevelService {
    @Autowired
    private LevelRepository levelRepository;

    @Override
    public List<ProjectLevel> getAllLevels() {
        List<ProjectLevel> levelList = levelRepository.findAll();
        return levelList;
    }
}
3
  • 5
    you need @Autowired private LevelService levelService; Commented Oct 30, 2018 at 11:07
  • @sidgate Many many thanks Commented Oct 30, 2018 at 11:17
  • Possible duplicate of java - nullpointer exception Commented Oct 30, 2018 at 13:12

2 Answers 2

7

Just change this piece of code:

@Autowired
private UserInfoService userInfoService;
private LevelService levelService;

to

@Autowired
private UserInfoService userInfoService;
@Autowired
private LevelService levelService;
Sign up to request clarification or add additional context in comments.

Comments

0

You must use separate @Autowired for private LevelService levelService;

Comments

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.