0

I am trying to create a simple web application with Spring Boot MVC using entity framework.I am using PostGre as my database.

Here is my Login Controller

package com.Jahan.Task_Management.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.Jahan.Task_Management.helper.LoginHelper;
import com.Jahan.Task_Management.helperModel.UserHelper;
import com.Jahan.Task_Management.repo.UserRepository;

//Login Controller for checking and validation of user input

@Controller
public class LoginController {
    @Autowired(required = true)
    UserRepository userrepo;
    @RequestMapping(value="/Login",method=RequestMethod.GET)
    public String Login(Model model){

        User aUser=new User();
        model.addAttribute("aUser",aUser);
        // return view of the login page.
        return "/Product/Login";
    }


    @RequestMapping(value="/Login",method=RequestMethod.POST)
    public ModelAndView process(@ModelAttribute("aUser") User aUser){

        LoginHelper aloginhelper= new LoginHelper();
        String atext= aloginhelper.CheckUser(aUser);
        if(atext.equals("Success"))
        {
            // redirect to find all user page.
            return new ModelAndView("redirect:/findall");
        }
        else
        {
            return null;
        }
    }
}

Here is my Login Helper class

package com.Jahan.Task_Management.helper;
import com.Jahan.Task_Management.model.User;
import com.Jahan.Task_Management.repo.UserRepository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.Jahan.Task_Management.helperModel.UserHelper;
@Component
public class LoginHelper {
    @Autowired
    UserRepository userRepos;
    //User's Name and password checking.
    public String CheckUser(UserHelper aUserHelper){
        List<User> UserList =userRepos.findByuserName(aUserHelper.userName);
        if(UserList.size()==1) 
        {
            for(User auser : UserList)
            {
                if(auser.getpassword().equals(aUserHelper.getpassword()))
                {
                    //Matched password for user.
                    return "Success";
                }
            }
        }
        return "Fail";
    }
    //helper function for saving user info to database.
    public void saveUser(UserHelper aUserHelper){
        if(!aUserHelper.userName.equals("") && !aUserHelper.email.equals("") && !aUserHelper.password.equals("") && aUserHelper.role>0) 
        {
            User aUser=new User(aUserHelper.userName,aUserHelper.password,aUserHelper.email,aUserHelper.role);
            userRepos.save(aUser);
        }
    }
    //helper function for deleting user entity from database.
    public void DeleteUser(long id){

        if(id!=0) 
        {   
            userRepos.delete(id);
        }
    }
}

Here is my entity model class properties

package com.Jahan.Task_Management.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

//Entity model for user_tb of TaskDb

    @Entity
    @Table(name = "user_tb")
    public class User implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private long userId;

        @Column(name = "userName")
        private String userName;

        @Column(name = "password")
        private String password;

        @Column(name = "email")
        private String email;

        @Column(name = "role")
        private int role;


        public User() {
        }

        public User(String userName, String password,String email,int role) {
            this.userName = userName;
            this.password = password;
            this.email=email;
            this.role=role;
        }

        public long getuserId() {
            return userId;
        }

        public String getuserName() {
            return userName;
        }

        public void setuserName(String userName) {
            this.userName = userName;
        }

        public String getpassword() {
            return password;
        }

        public void setpassword(String password) {
            this.password = password;
        }

        public String getemail() {
            return email;
        }

        public void setemail(String email) {
            this.email = email;
        }


        public int getrole() {
            return role;
        }

        public void setrole(int role) {
            this.role = role;
        }

        @Override
        public String toString() {
            return String.format("User[userName='%s', email='%s']",userName, email);
        }

    }

Here is my application properties

# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
spring.datasource.url=jdbc:postgresql://localhost:5432/TaskDB
spring.datasource.username=postgres
spring.datasource.password=******
spring.jpa.generate-ddl=true

For PostGre, i have added below dependency in pom.xml

<dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

When i try to run login page and submit it with some value, its show java.lang.NullPointerException. It is occurs from below line in login controller

String atext= aloginhelper.CheckUser(aUser);

Error :

2017-08-27 15:01:37.196 ERROR 1568 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null

I am not sure what is the problem with my code here. I was going through some different guides but i didn't find anything related to this problem.I tried to deliver all information.let me know if anything misses.

  • `java.lang.NullPointerException: null` means that the application is trying to call a method from a null object. Try to debug the application or add previous validation to see if `aUserHelper!=null` – Daniel C. Aug 27 '17 at 05:54
  • I have use aUserHelper!=null for validation. It is passing through the validation and showing the same error. – Sawgath Jahan Mehedy Aug 27 '17 at 06:14

0 Answers0