- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图找出我的错误是什么,但在我的代码中找不到任何称为空指针的内容。
这是 StackTrace (1):
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at daftoor.Database.connect(Database.java:92)
at daftoor.DoSignIn.checkSignin(DoSignIn.java:86)
at daftoor.DoSignIn.processRequest(DoSignIn.java:40)
at daftoor.DoSignIn.doGet(DoSignIn.java:122)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
以下是 StackTrace 日志 (2):
Jul 31, 2012 4:07:41 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [DoSignIn] in context with path [/Daftoor] threw exception
java.lang.NullPointerException
at daftoor.Database.createStatement(Database.java:105)
at daftoor.DoSignIn.checkSignin(DoSignIn.java:91)
at daftoor.DoSignIn.processRequest(DoSignIn.java:40)
at daftoor.DoSignIn.doGet(DoSignIn.java:122)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
这是错误页面:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
myproject.Database.createStatement(Database.java:101)
myproject.DoSignIn.checkSignin(DoSignIn.java:91)
myproject.DoSignIn.processRequest(DoSignIn.java:40)
myproject.DoSignIn.doGet(DoSignIn.java:127)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.22 logs.
Apache Tomcat/7.0.22
这是我的代码:
数据库类:
package somePackage;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Properties;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author mih1406
*/
public class Database {
private HashMap dbConfig;
private Connection dbConnection;
// debugging
HttpServletRequest request;
HttpServletResponse response;
public Database(HttpServletRequest request, HttpServletResponse respnse) {
this.request = request;
this.response = response;
setDatabaseConfig();
}
public void setDatabaseConfig() {
Properties config = new Properties();
InputStream configFile;
try {
configFile = this.getClass().getResourceAsStream("config.properties");
config.load(configFile);
} catch(FileNotFoundException fnfe) {
// TODO properties file not found error
} catch(IOException ioe) {
// TODO i/o Exception
}
String dbHost = config.getProperty("dbHost");
String dbPort = config.getProperty("dbPort");
String dbUsername = config.getProperty("dbUsername");
String dbPassword = config.getProperty("dbPassword");
String dbName = config.getProperty("dbName");
String dbPrefix = config.getProperty("dbPrefix");
setDatabaseConfig(dbHost, dbPort, dbUsername, dbPassword,
dbName, dbPrefix);
}
public void setDatabaseConfig(String dbHost, String dbPort, String dbUsername,
String dbPassword, String dbName, String dbPrefix) {
dbConfig = new HashMap();
dbConfig.put("dbHost", dbHost);
dbConfig.put("dbPort", dbPort);
dbConfig.put("dbUsername", dbUsername);
dbConfig.put("dbPassword", dbPassword);
dbConfig.put("dbName", dbName);
dbConfig.put("dbPrefix", dbPrefix);
}
public void connect() {
String dbHost = (String)dbConfig.get("dbHost") + "/";
String dbPort = (String)dbConfig.get("dbPort");
String dbUsername = (String)dbConfig.get("dbUsername");
String dbPassword = (String)dbConfig.get("dbPassword");
String dbName = (String)dbConfig.get("dbName");
String dbPrefix = (String)dbConfig.get("dbPrefix");
String dbDriver = "com.mysql.jdbc.Driver";
String dbURL = "jdbc:mysql://" + dbHost + ":" + dbPort + "/";
try {
Class.forName(dbDriver);
dbConnection = DriverManager.getConnection(dbURL, dbURL, dbPassword);
} catch (ClassNotFoundException cnfe) {
// TODO Class Exception
} catch (SQLException sqle) {
// TODO SQL Exception
}
}
public Statement createStatement() {
try {
return dbConnection.createStatement();
} catch(SQLException sqle) {
// TODO SQL Exception
}
return null;
}
public PreparedStatement prepareStatement(String sql) {
PreparedStatement pstmt = null;
try {
pstmt = dbConnection.prepareStatement(sql);
} catch(SQLException sqle) {
// TODO SQL Exception
}
return pstmt;
}
public void close() {
try {
if(dbConnection.isClosed()) {
return;
}
dbConnection.close();
} catch(SQLException sqle) {
// TODO SQL Exception
}
}
public String getPrefix() {
return (String)dbConfig.get("dbPrefix");
}
public String getName() {
return (String)dbConfig.get("dbName");
}
public String getHost() {
return (String)dbConfig.get("dbHost");
}
public String getPort() {
return (String)dbConfig.get("dbPort");
}
public String getUsername() {
return (String)dbConfig.get("dbUsername");
}
public String getPassword() {
return (String)dbConfig.get("dbPassword");
}
}
DoSingin 类:
package somePackage;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author mih1406
*/
@WebServlet(name = "DoSignIn", urlPatterns = {"/DoSignIn"})
public class DoSignIn extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
checkValues(request, response);
checkSignin(request, response);
}
private void checkValues(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String adminUsername = request.getParameter("adminUsername");
String adminPassword = request.getParameter("adminPassword");
boolean someEmpty = false;
if((adminUsername.equals("") || adminUsername == null)) {
someEmpty = true;
request.setAttribute("emptyEmail", "Required");
} else {
request.setAttribute("adminUsername",
request.getParameter("adminUsername"));
}
if((adminPassword.equals("") || adminPassword == null)) {
someEmpty = true;
request.setAttribute("emptyPassword", "Required");
}
if(someEmpty) {
String url = "signin.jsp";
forwardToPage(request, response, url);
}
}
private void forwardToPage(HttpServletRequest request,
HttpServletResponse response, String url)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request, response);
}
private void checkSignin(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String adminUsername = request.getParameter("adminUsername");
String adminPassword = request.getParameter("adminPassword");
out.println("<br />HereDBCreation!!");
Database db = new Database(request, response);
db.connect();
out.println("<br />HereDBConnectionDone!!");
String query = "SELECT adminUsername, adminPassword FROM " +
db.getPrefix() + db.getName() + " WHERE adminUsername = '" +
adminUsername + "' AND adminPassword = '" + adminPassword + "'";
Statement stmt = db.createStatement();
ResultSet result = null;
if(stmt == null) out.println("<br />HereDBStmtDone!!");
try {
result = stmt.executeQuery(query);
result.beforeFirst();
out.println("Here!!");
if(result.next()){
HttpSession session = request.getSession(true);
session.setMaxInactiveInterval(21*60); // 21 minutes
session.setAttribute("JSESSIONID", adminUsername);
out.println("Correct!!");
} else {
out.println("Invalid!!");
}
} catch(SQLException sqle) {
// TODO SQL Exception
System.out.println(sqle.getMessage());
}
}
}
signin.jsp JSP 文件:
<%--
Document : login
Created on : Jul 19, 2012, 3:36:48 PM
Author : mih1406
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Administration</title>
</head>
<body>
<form action="DoSignIn" method="get" name="adminSignInForm">
<table>
<tr>
<td colspan="2">Administration<td>
<br />
</tr>
<tr>
<td><label for="adminUsername">Email:</label></td>
<td><input type="text" value="${adminUsername}" name="adminUsername" />${emptyEmail}</td>
</tr>
<tr>
<td><label for="adminPassword">Password:</label></td>
<td><input type="password" value="" name="adminPassword" />${emptyPassword}</td>
</tr>
<tr>
<td colspan="2">
<input type="checkbox" value="yes" checked="checked" name="persistentCookie" />
<label for="persistentCookie">Stay signed in</label>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Sign in" /></td>
</tr>
</table>
</form>
</body>
</html>
最佳答案
就是这些检查
(adminUsername.equals("") || adminUsername == null)
您的输入可以为空,并且您已经考虑到了这一点,但如果是这样,它将尝试首先执行此语句 adminUsername.equals("")
并抛出一个 NPE。只需颠倒他们的顺序,就可以解决您的问题。
( adminUsername == null || adminUsername.equals(""))
@ftom2 也是正确的,它可能是 dbConnection.createStatement();
行,但如果没有堆栈跟踪,我们无法真正判断。您应该进行这些更改以适应 null 的可能性,这样以后就不会出现问题。
关于java - 为什么我收到 NullPointerException 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11740115/
我正在将我的模板代码移植到 XTend。在某些时候,我在测试用例中有这种类型的条件处理: @Test def xtendIfTest() { val obj = new FD if (
我是新来的 kotlin , 当我开始 Null Safety 时,我对下面的情况感到困惑. There's some data inconsistency with regard to initia
我的应用程序一直在各种Android版本中保持良好状态。我有用户在Android 4.3、5.0、5.1和6.0上正常运行。但是,具有S7 Edge的用户刚刚更新了Android 7.0,将文本粘贴到
我使用的是最新版本的 LWUIT (1.5)。我在资源编辑器中设计了我的表单,然后将代码生成到 netbeans。问题是如果我想访问除表单之外的任何对象,我会收到此错误: java.lang.Null
更新: 我在 Fedora 21 上运行它。 SonarQube - 5.0。 SonarQube Runner - 2.4 更新 2:Findbugs v3.1,Java 插件 v2.8 更新3:
RecupData 我的类仅在 web 中返回 NullPointerException。我连接到 pgsql db 8.3.7 - 该脚本在“控制台”syso 中运行良好 - 但引发了测试 Web
我在 mac 上使用 Processing 2.08。我正在尝试使用文档中给出的 createShape 函数创建 PShape。 PShape s; void setup(){ size(500
我在 mac 上使用 Processing 2.08。我正在尝试使用文档中给出的 createShape 函数创建 PShape。 PShape s; void setup(){ size(500
每次运行此 jsp 时,都会收到以下错误异常: org.apache.jasper.JasperException: java.lang.NullPointerException root cause
Kotlin 在编译时有一个出色的 null 检查,使用分离到“可空?”和“不可为空”的对象。它有一个 KAnnotator 来帮助确定来自 Java 的对象是否可以为空。但是,如果 not-null
我有一个布局将显示一个TextView,用于显示一个滴答时间。我遵循了此链接中的代码 How to Display current time that changes dynamically for
Elasticsearch 1.4.1版(“lucene_version”:“4.10.2”) 我有一个像这样的文件: $ curl 'http://localhost:9200/blog/artic
这是我从另一个类调用函数的方法Selenium 设置已定义。 public void Transfer() throws Exception { System.out.println("\nTrans
我试图在主类中使用我在此类中创建的函数,但它崩溃并显示“警告:无法在根 0 处打开/创建首选项根节点 Software\JavaSoft\Prefsx80000002。 Windows RegCrea
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 3 年前。 我有一个 Java 代码,它将
我声明了两张牌: Card card1 = new Card('3', Card.Suit.clubs); Card card2 = new Card('T', Card.Suit.diamonds)
我编写了一段代码来解码 Base64 图像并在 javafx 中表示该图像。在我的 url base64 代码中不断变化。这就是我在 javafx 代码中使用任务的原因。但我收到错误:java.lan
我正在尝试使用 arrayList 的 arrayList 在 Java 中实现图形。 每当调用 addEdge 函数时,我都会收到 NullPointerException 。我似乎无法弄清楚为什么
我是 Java/android 的新手,所以很多这些术语都是外国的,但我愿意学习。我不打算详细介绍该应用程序,因为我认为它不相关。我目前的问题是,我使用了博客中的教程和代码 fragment ,并使我
我正在开发一个 Android 应用程序来在 Android developer guide 的帮助下录制视频.我程序上的所有代码都与此页面相同。 我在 之外定义了权限标签。 当应
我是一名优秀的程序员,十分优秀!