[英]Java Null point exception with the implements class?
I am new to java. 我是java的新手。 I created my first Java Spring project and added these following code, but there seems to be error.
我创建了我的第一个Java Spring项目并添加了以下代码,但似乎有错误。
This is what I tried. 这是我试过的。
MyController.java 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 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 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. 当我运行代码时,登录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)
When I println username
and password
before line 37 it seems to be appearing in the console. 当我在第37行之前打印
username
和password
,它似乎出现在控制台中。
The error seems to be occur in UserExists = user.isValidUser( username, password );
UserExists = user.isValidUser( username, password );
似乎发生错误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
. 好吧,显然null来自
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 我假设您希望spring为您初始化
user
,因此您似乎错过的是用户字段声明中的@Autowired
注释
You dont create any User object and therefore you are getting a Nullpointerexception. 您不会创建任何User对象,因此您将获得Nullpointerexception。
Initialise your user instance variable like: 初始化您的用户实例变量,如:
user = new UserActions();
user.isValidUser(log, pwd);
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.