实现类的Java Null点异常?

时间:2016-12-24 23:06:19

标签: java spring hibernate

我是java的新手。我创建了我的第一个Java Spring项目并添加了以下代码,但似乎有错误。

这就是我的尝试。

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

}

当我运行代码时,登录get,似乎没问题,但是当我总结表单时,出现错误。

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)

当我在第37行之前打印usernamepassword时,它似乎出现在控制台中。

错误似乎发生在UserExists = user.isValidUser( username, password );行。我不知道哪里出错了,但我无法调试。有人可以帮忙吗?提前谢谢。

2 个答案:

答案 0 :(得分:6)

嗯,很明显,null来自user

我假设您希望spring为您初始化user,所以您错过的是用户字段声明中的@Autowired注释

答案 1 :(得分:2)

您不会创建任何User对象,因此您将获得Nullpointerexception。

初始化您的用户实例变量,如:

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