2016-12-24 49 views
3

我是新來的java。我創建了我的第一個Java Spring項目並添加了以下代碼,但似乎有錯誤。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

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

} 

當我運行代碼,登錄得到,似乎很好,但是當我提交表單時,出現錯誤。

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) 

當我調用println usernamepassword線37之前似乎在控制檯中進行顯示。

該錯誤似乎發生在UserExists = user.isValidUser(username, password);行。我不知道我錯了哪裏,但我無法調試。誰能幫忙?提前致謝。

回答

2

你不創建任何用戶對象,因此你得到一個Nullpointerexception。

初始化用戶實例變量,如:

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

好,顯然是空來自user

我假設你想春天初始化user給你,所以看來你錯過了什麼是@Autowired註釋用戶字段聲明

+0

您好,感謝您的回答。我在哪裏添加'@ Autowired'?在聲明用戶?在這裏 - 「公共用戶用戶;'?? –

+0

@slambook - 是的 –