简体   繁体   中英

Java Null point exception with the implements class?

I am new to java. I created my first Java Spring project and added these following code, but there seems to be error.

This is what I tried.

MyController.java

package org.jpanel.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jpanel.service.User;

//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import org.jpanel.service.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

    public User user;

    @RequestMapping(value="/login",method=RequestMethod.GET)
    public ModelAndView login() {

        String message = "Username";
        return new ModelAndView("login", "message", message);
    }

    @RequestMapping(value="/login",method=RequestMethod.POST)
    public ModelAndView postLogin( HttpServletRequest request, HttpServletResponse response ) {
        String username =   request.getParameter("log");
        String password =   request.getParameter("pwd");

        boolean UserExists;

        if( username != "" || password != "" ) {
            UserExists  =   user.isValidUser( username, password );
        } else {
            UserExists  =   false;
        }

        if( UserExists != false ) {
            return new ModelAndView("login", "message", "Posted");
        } else {
            return new ModelAndView("login", "message", "<div class = 'notification jp-login-failed'>Username or Password Incorrect</div>");
        }
    }
}

User.java

package org.jpanel.service;

public interface User {

    public boolean isValidUser( String username, String password );

    public int createUser( String username, String password );

}

UserActions.java

package org.jpanel.service.impl;

import org.jpanel.service.User;
import org.springframework.stereotype.Repository;

@Repository
public class UserActions implements User {

    @Override
    public boolean isValidUser( String username, String password ) {
        System.out.println( username );
        if( username != "" || password != "" ) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int createUser( String username, String password ) {
        // TODO Auto-generated method stub
        return 0;
    }

}

When I run the code, the login get, seems to be fine, but When I sumbmit the form, there is an error.

java.lang.NullPointerException
org.jpanel.controller.MyController.postLogin(MyController.java:37)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

When I println username and password before line 37 it seems to be appearing in the console.

The error seems to be occur in UserExists = user.isValidUser( username, password ); line. I don't know where I went wrong, but I am unable to debug this. Can anyone help? Thanks in advance.

Well, clearly the null comes from user .

I assume you want spring to initialize user for you, so it seems that what you miss is @Autowired annotation on the user field declaration

You dont create any User object and therefore you are getting a Nullpointerexception.

Initialise your user instance variable like:

user = new UserActions();
user.isValidUser(log, pwd);
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM