2015-04-29 83 views
-2

我試圖做一個非常簡單的登錄屏幕。它必須檢查密碼和用戶名。還有一個默認用戶,在init()中設置。奇怪的是,它仍然給我NullPointerExceptionrd.forward();
有人可以解釋一下嗎?我很抱歉我的英語不好,讓我知道是否需要詳細說明。Javaservlet nullpointerexception

package servlets; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.Scanner; 
import javax.servlet.FilterConfig; 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletConfig; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import classes.user; 

public class loginServlet extends HttpServlet { 
ArrayList<user> alleUsers = new ArrayList<>(); 
private String defaultUser="", defaultPassword=""; 
private user userding; 

public void init(ServletConfig config) throws ServletException{ 
    super.init(config); 
    defaultUser = config.getInitParameter("defaultUser"); 
    defaultPassword = config.getInitParameter("defaultPassword"); 
    user userding = new user(defaultUser, defaultPassword); 
} 

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
    String a = req.getParameter("back"); 
    String b = req.getParameter("username"); 
    String c = req.getParameter("password"); 
    Object o = getServletContext().getAttribute("arraylist"); 
    RequestDispatcher rd = null; 

    if (a.equals("Register")) { 
     rd = req.getRequestDispatcher("registerScherm.jsp"); 
    } 

    if (a.equals("Login")) { 
     System.out.println(b + " "+ c); 
     if(!b.equals(null) && !c.equals(null)){ 
      if (o instanceof ArrayList) { 
        alleUsers = (ArrayList) o; 
        alleUsers.add(userding); 
        if (alleUsers != null) { 
         for (user u : alleUsers) { 
          if (b.equals(u.getUsername()) && c.equals(u.getPassword())) { 
           getServletContext().setAttribute("naam", b); 
           rd = req.getRequestDispatcher("ingelogdScherm.jsp"); 
           break; 
          } else { 
           rd = req.getRequestDispatcher("loginScherm.jsp"); 
          } 
         } 
        } 
       } 
      }else{ 
       rd = req.getRequestDispatcher("loginScherm.jsp"); 
      } 
     } 
    rd.forward(req, resp); 
    //It gives an error on this forward 
    } 
} 

這裏是我的loginscreen jsp頁面:

<body> 
    <% 
     String userName = ""; 
     for (Cookie c : request.getCookies()) { 
      if (c.getName().equals("username")) { 
      userName = c.getValue(); 
      break; 
     } 
    } 

%> 
<div id="account"> 
<div id="inputbox"> 
    <form action="loginServlet.do" method="post"> 
     <input class="ltf" type="text" name="username" value="<%= userName %>" /><br/> 
     <input class="ltf" type="password" name="password" /><br/> 
     <input type="submit" name="back" value="Login" /> 
     <input type="submit" name="back" value="Register" /> 
    </form> 
</div> 

好吧,我想我固定它。我刪除了一些警衛,這有所幫助。我仍然困惑爲什麼它沒有通過他們,但它現在起作用。謝謝!

rd.forward(req, resp); 

這可能是爲什麼你得到一個NullPointerException異常的原因,RD尚未初始化並沒有得到:

+0

您可以顯示堆棧跟蹤? – Stultuske

+0

你在'if'嵌套條件中初始化你的'RequestDispatcher'。有可能是未初始化的路徑。你應該自己調試一下。 – Mena

+0

但如果這些字段爲空,它不應該在那裏初始化? – RandomStranger

回答

0

如果a值不等於「登錄」或「註冊」,然後rd仍然null所以rd.forward(req, resp);會給NullPointerException
可以通過打印的a值調試它或任何你使用req.getRequestDispatcher("name.jsp");像下面你可以添加rd.forward(req, resp);

package servlets; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.Scanner; 
import javax.servlet.FilterConfig; 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletConfig; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import classes.user; 

public class loginServlet extends HttpServlet { 
ArrayList<user> alleUsers = new ArrayList<>(); 
private String defaultUser="", defaultPassword=""; 
private user userding; 

public void init(ServletConfig config) throws ServletException{ 
    super.init(config); 
    defaultUser = config.getInitParameter("defaultUser"); 
    defaultPassword = config.getInitParameter("defaultPassword"); 
    user userding = new user(defaultUser, defaultPassword); 
} 

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
    String a = req.getParameter("back"); 
    String b = req.getParameter("username"); 
    String c = req.getParameter("password"); 
    Object o = getServletContext().getAttribute("arraylist"); 
    RequestDispatcher rd = null; 

    if (a.equals("Register")) { 
     rd = req.getRequestDispatcher("registerScherm.jsp"); 
     rd.forward(req, resp); 
    } 

    if (a.equals("Login")) { 
     System.out.println(b + " "+ c); 
     if(!b.equals(null) && !c.equals(null)){ 
      if (o instanceof ArrayList) { 
        alleUsers = (ArrayList) o; 
        alleUsers.add(userding); 
        if (alleUsers != null) { 
         for (user u : alleUsers) { 
          if (b.equals(u.getUsername()) && c.equals(u.getPassword())) { 
           getServletContext().setAttribute("naam", b); 
           rd = req.getRequestDispatcher("ingelogdScherm.jsp"); 
           rd.forward(req, resp); 
           break; 
          } else { 
           rd = req.getRequestDispatcher("loginScherm.jsp"); 
           rd.forward(req, resp); 
          } 
         } 
        } 
       } 
      }else{ 
       rd = req.getRequestDispatcher("loginScherm.jsp"); 
       rd.forward(req, resp); 
      } 
     } 
    } 
} 
0

縱觀你的條件語句RD的混亂局面時執行該語句可能不會被初始化在內存中分配了任何空間。所有它真的在這一點上是一個指向null的指針。

如果這是可以或允許在應用程序中發生的情況下,就可以避免通過添加以下條件

if(rd != null) 
     rd.forward(req, resp); 

得到例外,你可能需要通知客戶端上的前端該rd無法初始化。

+0

這隻給我一個空白屏幕,因爲它沒有轉發任何東西 – RandomStranger

+0

再次閱讀我的答案,第二個代碼示例不是爲了解決您的問題的提案。你得到NullPointerException的原因是因爲rd沒有在你的條件語句中初始化。 RequestDispatcher rd = null;不會初始化rd,顯然變量a永遠不會等於「register」或「login」,所以當rd.forward被調用時,rd仍然是空的! –

+0

啊我明白了。奇怪的是,它確實有用。變量A應該是'註冊'或'登錄'。註冊按鈕會將其轉發到另一頁面。讓我困惑的是,當字段被檢查爲空時,它應該初始化rd,對吧?此外,當您按下按鈕時,它會輸出從文本字段中讀取的用戶名和密碼。所以它確實進入if語句 – RandomStranger

0

你的縮進被打斷了,這使得它難以閱讀;我fized這對你..

package servlets; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.Scanner; 
import javax.servlet.FilterConfig; 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletConfig; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import classes.user; 

public class loginServlet extends HttpServlet { 
ArrayList<user> alleUsers = new ArrayList<>(); 
private String defaultUser="", defaultPassword=""; 
private user userding; 

public void init(ServletConfig config) throws ServletException{ 
    super.init(config); 
    defaultUser = config.getInitParameter("defaultUser"); 
    defaultPassword = config.getInitParameter("defaultPassword"); 
    user userding = new user(defaultUser, defaultPassword); 
} 

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
    String a = req.getParameter("back"); 
    String b = req.getParameter("username"); 
    String c = req.getParameter("password"); 
    Object o = getServletContext().getAttribute("arraylist"); 
    RequestDispatcher rd = null; 

    if (a.equals("Register")) { 
     rd = req.getRequestDispatcher("registerScherm.jsp"); 
    } 

    if (a.equals("Login")) { 
     System.out.println(b + " "+ c); 
     if(!b.equals(null) && !c.equals(null)){ 
      if (o instanceof ArrayList) { 
        alleUsers = (ArrayList) o; 
        alleUsers.add(userding); 
        if (alleUsers != null) { 
         for (user u : alleUsers) { 
          if (b.equals(u.getUsername()) && c.equals(u.getPassword())) { 
           getServletContext().setAttribute("naam", b); 
           rd = req.getRequestDispatcher("ingelogdScherm.jsp"); 
           break; 
          } else { 
           rd = req.getRequestDispatcher("loginScherm.jsp"); 
          } 
         } 
        } 
      } 
     }else{ 
      rd = req.getRequestDispatcher("loginScherm.jsp"); 
     } 
    } 
    rd.forward(req, resp); //It gives an error on this forward 
} 
現在

,因爲這樣做.. 請注意,如果一個不等於「登錄」或「註冊」,因爲初始化RD沒有改變:

String a = req.getParameter("back"); 
    RequestDispatcher rd = null;  

所以看什麼「a」是將是有趣..

其他地方,你可以在基於數據這種情況下結束了,包括這條線:

if (o instanceof ArrayList)