John
John

Reputation: 750

java.lang.NullPointerException while calling from controller

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;
    }
}

Upvotes: 1

Views: 4651

Answers (2)

Ashoka
Ashoka

Reputation: 26

You must use separate @Autowired for private LevelService levelService;

Upvotes: 0

Jayesh Choudhary
Jayesh Choudhary

Reputation: 798

Just change this piece of code:

@Autowired
private UserInfoService userInfoService;
private LevelService levelService;

to

@Autowired
private UserInfoService userInfoService;
@Autowired
private LevelService levelService;

Upvotes: 7

Related Questions