【100分求】jsp 空指针异常问题解决
我新学,请求高手帮忙,问题解决必有重谢tomcat报错java.lang.NullPointerExceptionelectric.dbs.Newsdb.selectsq...
我新学,请求高手帮忙,问题解决必有重谢
tomcat报错
java.lang.NullPointerException
electric.dbs.Newsdb.selectsql(Newsdb.java:34)
代码如下
package electric.dbs;
import electric.dbs.Dbnews;
import electric.dbs.news;
import java.util.Collection;
import java.util.ArrayList;
import java.sql.SQLException;
import java.sql.ResultSetMetaData;
import java.sql.ResultSet;
import java.util.Iterator;
import java.sql.*;
import javax.servlet.http.*;
import electric.electricUtils.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Newsdb {
public Newsdb() {
}
public int selectsql(int st)
{
String countSql="SELECT COUNT(*) FROM NEWS WHERE STATUS<>1";
Connection co=null;
ResultSet r = null;
PreparedStatement pstm = null;
int size=0;
int sizepa=0;
try {
co = DbConnectionManager.getConnection();
pstm = co.prepareStatement(countSql);
r = pstm.executeQuery();
while (r.next()) {
size = r.getInt(1);
sizepa=(int)size/st;
if(size%st>0)
{
sizepa=sizepa+1;
}
}
r.close();
r=null;
pstm.close();
pstm=null;
co.close();
co=null;
}
catch (SQLException sqle) {
System.err.println(sqle.getMessage()+"333");
}
return sizepa;
}
public Collection select(int step, int page) {
Collection coll = new ArrayList();
Connection con=null;
ResultSet rs=null;
PreparedStatement pstmt=null;
int tip = step * (page - 1);
try {
String sql="SELECT * FROM [NEWS] WHERE STATUS<>1";
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = pstmt.executeQuery();
System.out.print(tip);
if (tip <= 0) {
rs.beforeFirst();
} else {
if (!rs.absolute(tip)) {
rs.beforeFirst();
}
}
for (int i = 1; rs.next() && i <= step; i++) {
news news = new news();
news.setId(rs.getInt(1));
news.setMotif(rs.getString(2));
news.setKithepeople(rs.getString(3));
news.setContent(rs.getString(4));
news.setCreateDate(rs.getString(5));
news.setCreatepeople(rs.getInt(6));
news.setStatus(rs.getInt(6));
coll.add(news);
}
rs.close();
rs=null;
pstmt.close();
pstmt=null;
con.close();
con=null;
} catch (SQLException ex) {
System.out.println(ex.getMessage()+"666");
} finally {
return coll;
}
}
} 展开
tomcat报错
java.lang.NullPointerException
electric.dbs.Newsdb.selectsql(Newsdb.java:34)
代码如下
package electric.dbs;
import electric.dbs.Dbnews;
import electric.dbs.news;
import java.util.Collection;
import java.util.ArrayList;
import java.sql.SQLException;
import java.sql.ResultSetMetaData;
import java.sql.ResultSet;
import java.util.Iterator;
import java.sql.*;
import javax.servlet.http.*;
import electric.electricUtils.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Newsdb {
public Newsdb() {
}
public int selectsql(int st)
{
String countSql="SELECT COUNT(*) FROM NEWS WHERE STATUS<>1";
Connection co=null;
ResultSet r = null;
PreparedStatement pstm = null;
int size=0;
int sizepa=0;
try {
co = DbConnectionManager.getConnection();
pstm = co.prepareStatement(countSql);
r = pstm.executeQuery();
while (r.next()) {
size = r.getInt(1);
sizepa=(int)size/st;
if(size%st>0)
{
sizepa=sizepa+1;
}
}
r.close();
r=null;
pstm.close();
pstm=null;
co.close();
co=null;
}
catch (SQLException sqle) {
System.err.println(sqle.getMessage()+"333");
}
return sizepa;
}
public Collection select(int step, int page) {
Collection coll = new ArrayList();
Connection con=null;
ResultSet rs=null;
PreparedStatement pstmt=null;
int tip = step * (page - 1);
try {
String sql="SELECT * FROM [NEWS] WHERE STATUS<>1";
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = pstmt.executeQuery();
System.out.print(tip);
if (tip <= 0) {
rs.beforeFirst();
} else {
if (!rs.absolute(tip)) {
rs.beforeFirst();
}
}
for (int i = 1; rs.next() && i <= step; i++) {
news news = new news();
news.setId(rs.getInt(1));
news.setMotif(rs.getString(2));
news.setKithepeople(rs.getString(3));
news.setContent(rs.getString(4));
news.setCreateDate(rs.getString(5));
news.setCreatepeople(rs.getInt(6));
news.setStatus(rs.getInt(6));
coll.add(news);
}
rs.close();
rs=null;
pstmt.close();
pstmt=null;
con.close();
con=null;
} catch (SQLException ex) {
System.out.println(ex.getMessage()+"666");
} finally {
return coll;
}
}
} 展开
4个回答
展开全部
pstm = co.prepareStatement(countSql);
这行报错了,connection对象为空
检查一下配置
这行报错了,connection对象为空
检查一下配置
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
100分呢?拿出来点行动吧
参考资料: ◆ノSimply
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错误信息贴出来看看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询