请求帮助,急!!万分感谢!!!

java_shi 2008-07-24 11:46:30
我做一个用户管理页面;出现:java.lang.NullPointerException

public void deleteById(Long id) throws UesNameException {
deleteByIds(new Long[] { id });

}

public void deleteByIds(Long[] ids) throws UesNameException {
Connection connection = null;
PreparedStatement stmt = null;

try {
// -->A.
connection = getConnection();
// -->B.
String sql = "delete from useadmin where id=?";
stmt = connection.prepareStatement(sql);

for (int i = 0; i < ids.length; i++) {
Long id = ids[i];
stmt.setLong(1, id.longValue());
stmt.addBatch();
}

int[] rowCounts = stmt.executeBatch();

System.out.println(rowCounts.length + " row(s) deleted");

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {

}

}
//-------------------------JDBC 连接数据库页面:------------------------------------------------//

public void deleteById(Long id) throws UesNameException {
deleteByIds(new Long[] { id });

}

public void deleteByIds(Long[] ids) throws UesNameException {
Connection connection = null;
PreparedStatement stmt = null;

try {
// -->A.
connection = getConnection();
// -->B.
String sql = "delete from useadmin where id=?";
stmt = connection.prepareStatement(sql);

for (int i = 0; i < ids.length; i++) {
Long id = ids[i];
stmt.setLong(1, id.longValue());
stmt.addBatch();
}

int[] rowCounts = stmt.executeBatch();

System.out.println(rowCounts.length + " row(s) deleted");

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {

}

}

//-------------------------DeleteNameAction页面:------------------------------------------------//

//--> C.
DeleteNameForm fm = (DeleteNameForm) form;
Long id = fm.getId();


//--> M.
getServices().deleteById(id);

//--> V.
return mapping.findForward("uesccess");


//-------------------------显示页面:------------------------------------------------//

id Name Pws Pwss Gender Age Email Tal QQ Msg
1 admin 123456 123456 f 1235445 shidfsfsfdf@163.com 2112345678 84859195 shangha <a href="DeleteName.do">删除</a> 修改 新建
2 123 123 123 M 0 0 0 <a href="DeleteName.do">删除</a> 修改 新建
...全文
74 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
reality 2008-07-24
  • 打赏
  • 举报
回复
deleteByIds(JDBCUesNameImp.java:37)
这个地方的代码造成的错误
看看是不是哪个类为null 出的错
java_shi 2008-07-24
  • 打赏
  • 举报
回复
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.NullPointerException
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.NullPointerException
com.witbridge.service.imp.JDBCUesNameImp.deleteByIds(JDBCUesNameImp.java:37)
com.witbridge.service.imp.JDBCUesNameImp.deleteById(JDBCUesNameImp.java:20)
com.witbridge.struts.action.DeleteNameAction.execute(DeleteNameAction.java:28)
org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.

reality 2008-07-24
  • 打赏
  • 举报
回复
需要完整的错误代码
java_shi 2008-07-24
  • 打赏
  • 举报
回复
奇怪的是,我在JDBC main 下调试是可以的删除的
java_shi 2008-07-24
  • 打赏
  • 举报
回复
//======================DeleteNameAction====================================//

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.witbridge.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.witbridge.service.Exception.UesNameException;
import com.witbridge.struts.form.DeleteNameForm;


public class DeleteNameAction extends BaseAction {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UesNameException {
//--> C.
DeleteNameForm fm = (DeleteNameForm) form;
Long id = fm.getId();


//--> M.
getServices().deleteById(id);

//--> V.
return mapping.findForward("uesccess");
}


}

//======================JDBCUesNameImp getServices()=====================//

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.witbridge.struts.action;

import org.apache.struts.action.Action;

import com.witbridge.service.imp.JDBCProductImp;
import com.witbridge.service.imp.JDBCUesNameImp;


public class BaseAction extends Action {

protected JDBCProductImp getService() {
return new JDBCProductImp();
}

protected JDBCUesNameImp getServices() {
return new JDBCUesNameImp();
}
}

//======================视图页面=====================//
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><bean:message key="my.netstore.title" /></title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<script type="text/javascript" src="js/"></script>
</head>
<body>
<table>
<tr>
<th>id</th>
<th><bean:message key="logon.username" /></th>
<th><bean:message key="logon.password" /></th>
<th><bean:message key="logon.passwords" /></th>
<th><bean:message key="logon.gender" /></th>
<th><bean:message key="logon.age" /></th>
<th><bean:message key="logon.email" /></th>
<th><bean:message key="logon.tal" /></th>
<th><bean:message key="logon.qq" /></th>
<th><bean:message key="logon.msg" /></th>
</tr>
<logic:iterate id="uesname" name="uesname">
<tr>
<td><bean:write name="uesname" property="id" /></td>
<td><bean:write name="uesname" property="name" /></td>
<td><bean:write name="uesname" property="pws" /></td>
<td><bean:write name="uesname" property="pwss" /></td>
<td><bean:write name="uesname" property="gender" /></td>
<td><bean:write name="uesname" property="age" /></td>
<td><bean:write name="uesname" property="email" /></td>
<td><bean:write name="uesname" property="tal" /></td>
<td><bean:write name="uesname" property="qq" /></td>
<td><bean:write name="uesname" property="msg" /></td>
<td><a href="DeleteName.do">删除</a></td>
<td><a href="ModifyName.do">修改</a></td>
<td><a href="NewName.do">新建</a></td>
</tr>
</logic:iterate>
</table>
</body>
</html>

请高手帮忙看一下,谢谢;

==============================HTTP Status 500 - ===================================
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.NullPointerException
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.NullPointerException
com.witbridge.service.imp.JDBCUesNameImp.deleteByIds(JDBCUesNameImp.java:37)
com.witbridge.service.imp.JDBCUesNameImp.deleteById(JDBCUesNameImp.java:20)
com.witbridge.struts.action.DeleteNameAction.execute(DeleteNameAction.java:28)
org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.
java_shi 2008-07-24
  • 打赏
  • 举报
回复
//======================JDBC 连接数据库页面====================================//

package com.witbridge.service.imp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.witbridge.domain.UesName;
import com.witbridge.service.UesNameService;
import com.witbridge.service.Exception.UesNameException;

public class JDBCUesNameImp implements UesNameService {

public void deleteById(Long id) throws UesNameException {
deleteByIds(new Long[] { id });

}

public void deleteByIds(Long[] ids) throws UesNameException {
Connection connection = null;
PreparedStatement stmt = null;

try {
// -->A.
connection = getConnection();
// -->B.
String sql = "delete from useadmin where id=?";
stmt = connection.prepareStatement(sql);

for (int i = 0; i < ids.length; i++) {
Long id = ids[i];
stmt.setLong(1, id.longValue());
stmt.addBatch();
}

int[] rowCounts = stmt.executeBatch();

System.out.println(rowCounts.length + " row(s) deleted");

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {

}

}

public List list() throws UesNameException {

List uesnames = new ArrayList();

Connection connection = null;
Statement stmt = null;
ResultSet rs = null;

try {
// --> A.
connection = getConnection();
// --> B.
String sql = "select * from useadmin";
stmt = connection.createStatement();
// Execute the statement
rs = stmt.executeQuery(sql);

while (rs.next()) {
// get column value
Long id = new Long(rs.getLong("id"));
String name = rs.getString("name");
String pws = rs.getString("pws");
String pwss = rs.getString("pwss");
String gender = rs.getString("gender");
int age = rs.getInt("age");
String email = rs.getString("email");
int tal = rs.getInt("tal");
int qq = rs.getInt("qq");
String msg = rs.getString("msg");

UesName uesname = new UesName();
uesname.setId(id);
uesname.setName(name);
uesname.setPws(pws);
uesname.setPwss(pwss);
uesname.setGender(gender);
uesname.setAge(age);
uesname.setEmail(email);
uesname.setTal(tal);
uesname.setQq(qq);
uesname.setMsg(msg);

uesnames.add(uesname);
}

return uesnames;
// --> C.
} catch (ClassNotFoundException e) {
throw new UesNameException(e);
} catch (SQLException e) {
throw new UesNameException(e);
} finally {
close(connection, stmt, rs);
}

}

public void load(Long id) throws UesNameException {

}

public void save(UesName uesname) throws UesNameException {
Connection connection = null;
PreparedStatement stmt = null;
try {
// --> A.
connection = getConnection();
// --> B.
String sql = "insert into useadmin values (?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
stmt = connection.prepareStatement(sql);
stmt.setLong(1, uesname.getId().longValue());
stmt.setString(2, uesname.getName());
stmt.setString(3, uesname.getPws());
stmt.setString(4, uesname.getPwss());
stmt.setString(5, uesname.getGender());
stmt.setInt(6, uesname.getAge());
stmt.setString(7, uesname.getEmail());
stmt.setInt(8, uesname.getTal());
stmt.setInt(9, uesname.getQq());
stmt.setString(10, uesname.getMsg());

int rowCount = stmt.executeUpdate();

System.out.println(rowCount + " row(s) inserted");
// --> C.
} catch (ClassNotFoundException e) {
throw new UesNameException(e);
} catch (SQLException e) {
throw new UesNameException(e);
} finally {

}
}

// ---- Get Connection-----------------------//

public Connection getConnection() throws ClassNotFoundException,
SQLException {

Connection connection;
// 1.Driver Class
String driver = "com.mysql.jdbc.Driver";
// 2.URL of database
String url = "jdbc:mysql://localhost/product?user=root&password=admin";

Class.forName(driver);
connection = DriverManager.getConnection(url);
return connection;

}

private void close(Connection connection, PreparedStatement stmt,
ResultSet rs) {
close(connection, stmt, null);
}

// ----------------------------------------------------//

private void close(Connection connection, Statement stmt, ResultSet rs) {
// --C'
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
// -- B'
if (stmt != null)
try {
stmt.close();
} catch (SQLException e) {
}

// -- A'
if (connection != null)
try {
connection.close();
} catch (SQLException e) {
}

}

public static void main(String[] args) throws UesNameException {

UesNameService service = new JDBCUesNameImp();

List uesnames = service.list();
for (Iterator it = uesnames.iterator(); it.hasNext();) {
UesName uesname = (UesName) it.next();
System.out.println(uesname.getId() + "\t" + uesname.getName()
+ "\t" + uesname.getPws() + "\t" + uesname.getPwss() + "\t"
+ uesname.getGender() + "\t" + uesname.getAge() + "\t"
+ uesname.getEmail() + "\t" + uesname.getTal() + "\t"
+ uesname.getQq() + "\t" + uesname.getMsg());
}

//service.deleteByIds(new Long[] { new Long(6), new Long(7) });

}

}


81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧