[size=14px]我在做数据查询时,由于Model里有两个Double型属性,在从页面取得数据(String)再转化成立Double型,转到逻辑层查询,总是NollPoint,查询时可以单个字段查询,多个字段查询,以前做过的查询碰到Int型,同样这样做的,都没问题,double型有区别吗
后台报的错
java.lang.NullPointerException
at jxc.bookmge.vo.BookModel.getInPrice(BookModel.java:25)
at jxc.bookmge.dao.impl.JdbcImpl.parparedSql(JdbcImpl.java:115)
at jxc.bookmge.dao.impl.JdbcImpl.getByCondition(JdbcImpl.java:170
at jxc.bookmge.business.ebo.BookEbo.getByCondition(BookEbo.java:6
at jxc.bookmge.web.BookServlet.query(BookServlet.java:211)
at jxc.bookmge.web.BookServlet.doPost(BookServlet.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)下面是我的主要代码Modle 中的属性
public class BookModel implements java.io.Serializable{
private String id,name;
private Double inPrice,salePrice;
servelt类中的代码
private void query(HttpServletRequest request, HttpServletResponse response) {
//1.收集参数
String id = request.getParameter("id");
String name = request.getParameter("name");
String inPriceStr= request.getParameter("inPrice");
String inPriceStr2= request.getParameter("inPrice2");
String salePriceStr = request.getParameter("salePrice");
String salePriceStr2 = request.getParameter("salePrice2");
//2.组织参数
BookQueryModel bqm = new BookQueryModel();
bqm.setId(id);
bqm.setName(name);
if(inPriceStr!=null && inPriceStr.trim().length()>0){
bqm.setInPrice(Double.parseDouble(inPriceStr));
}
if(inPriceStr2!=null && inPriceStr2.trim().length()>0){
bqm.setInPrice2(Double.parseDouble(inPriceStr2));
}
if(salePriceStr!=null && salePriceStr.trim().length()>0 ){
bqm.setSalePrice(Double.parseDouble(salePriceStr));
}
if(salePriceStr2!=null && salePriceStr2.trim().length()>0 ){
bqm.setSalePrice2(Double.parseDouble(salePriceStr2));
}
//3.调逻辑层API
Collection col = Factory.getBookEbi().getByCondition(bqm);
//4.根据要求跳转页面
request.setAttribute("queryCol", col);
request.setAttribute("isQuery", "true");
toList(request,response);
}这是toList方法
private void toList(HttpServletRequest request, HttpServletResponse response) {
Collection col = new ArrayList();
String isQuery = (String) request.getAttribute("isQuery");
if(isQuery!=null && isQuery.equals("true")){
col = (Collection) request.getAttribute("queryCol");
}else{
col = Factory.getBookEbi().getAll();
}
request.setAttribute("listCol", col);
try {
request.getRequestDispatcher("/mvcbookmge/list.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}下面是逻辑层jdbcImpl类
//拼Sql
private String parparedSql(String sql,BookQueryModel bqm){
if(bqm.getId()!=null && bqm.getId().trim().length()>0){
sql += " and id = ? " ;
}
if(bqm.getName()!=null && bqm.getName().trim().length()>0){
sql += " and name like ?";
}
if(bqm.getInPrice()>0){
sql += " and inPrice >= ?";
}
if(bqm.getInPrice2()>0){
sql += " and inPrice <= ?";
}
if(bqm.getSalePrice()>0){
sql += " and salePrice >= ?";
}
if(bqm.getSalePrice2()>0){
sql += " and salePrice <= ?";
}
return sql;
}
//给问号赋值
private void setValue(PreparedStatement pstmt,BookQueryModel bqm) throws SQLException{
int count = 1;
if(bqm.getId()!=null && bqm.getId().trim().length()>0){
pstmt.setString(count, bqm.getId());
count++;
}
if(bqm.getName()!=null && bqm.getName().trim().length()>0){
pstmt.setString(count, "%"+bqm.getName()+"%");
count++;
}
if(bqm.getInPrice()>0){
pstmt.setDouble(count, bqm.getInPrice());
count++;
}
if(bqm.getInPrice2()>0){
pstmt.setDouble(count, bqm.getInPrice2());
count++;
}
if(bqm.getSalePrice()>0){
pstmt.setDouble(count, bqm.getSalePrice());
count++;
}
if(bqm.getSalePrice2()>0){
pstmt.setDouble(count, bqm.getSalePrice2());
count++;
}
}
@Override
public Collection<BookModel> getByCondition(BookQueryModel bqm) {
Connection conn = null;
Collection<BookModel> col = new ArrayList();
try {
conn = DBconn.getConn();
String sql = "select * from scott.book where 1=1 ";
//拼Sql
System.out.println(bqm);
this.parparedSql(sql, bqm);
sql += " order by id ASC";
System.out.println("sql========="+sql);
PreparedStatement pstmt = conn.prepareStatement(sql);
this.setValue(pstmt, bqm);
ResultSet rt = pstmt.executeQuery();
while(rt.next()){
String id = rt.getString("id");
String name = rt.getString("name");
Double inPrice = rt.getDouble("inPrice");
Double salePrice = rt.getDouble("salePrice");
BookModel bm = new BookModel();
bm.setId(id);
bm.setName(name);
bm.setInPrice(inPrice);
bm.setSalePrice(salePrice);
col.add(bm);
}
} catch (Exception e) {
e.printStackTrace();
}
return col;
}下面
后台报的错
java.lang.NullPointerException
at jxc.bookmge.vo.BookModel.getInPrice(BookModel.java:25)
at jxc.bookmge.dao.impl.JdbcImpl.parparedSql(JdbcImpl.java:115)
at jxc.bookmge.dao.impl.JdbcImpl.getByCondition(JdbcImpl.java:170
at jxc.bookmge.business.ebo.BookEbo.getByCondition(BookEbo.java:6
at jxc.bookmge.web.BookServlet.query(BookServlet.java:211)
at jxc.bookmge.web.BookServlet.doPost(BookServlet.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)下面是我的主要代码Modle 中的属性
public class BookModel implements java.io.Serializable{
private String id,name;
private Double inPrice,salePrice;
servelt类中的代码
private void query(HttpServletRequest request, HttpServletResponse response) {
//1.收集参数
String id = request.getParameter("id");
String name = request.getParameter("name");
String inPriceStr= request.getParameter("inPrice");
String inPriceStr2= request.getParameter("inPrice2");
String salePriceStr = request.getParameter("salePrice");
String salePriceStr2 = request.getParameter("salePrice2");
//2.组织参数
BookQueryModel bqm = new BookQueryModel();
bqm.setId(id);
bqm.setName(name);
if(inPriceStr!=null && inPriceStr.trim().length()>0){
bqm.setInPrice(Double.parseDouble(inPriceStr));
}
if(inPriceStr2!=null && inPriceStr2.trim().length()>0){
bqm.setInPrice2(Double.parseDouble(inPriceStr2));
}
if(salePriceStr!=null && salePriceStr.trim().length()>0 ){
bqm.setSalePrice(Double.parseDouble(salePriceStr));
}
if(salePriceStr2!=null && salePriceStr2.trim().length()>0 ){
bqm.setSalePrice2(Double.parseDouble(salePriceStr2));
}
//3.调逻辑层API
Collection col = Factory.getBookEbi().getByCondition(bqm);
//4.根据要求跳转页面
request.setAttribute("queryCol", col);
request.setAttribute("isQuery", "true");
toList(request,response);
}这是toList方法
private void toList(HttpServletRequest request, HttpServletResponse response) {
Collection col = new ArrayList();
String isQuery = (String) request.getAttribute("isQuery");
if(isQuery!=null && isQuery.equals("true")){
col = (Collection) request.getAttribute("queryCol");
}else{
col = Factory.getBookEbi().getAll();
}
request.setAttribute("listCol", col);
try {
request.getRequestDispatcher("/mvcbookmge/list.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}下面是逻辑层jdbcImpl类
//拼Sql
private String parparedSql(String sql,BookQueryModel bqm){
if(bqm.getId()!=null && bqm.getId().trim().length()>0){
sql += " and id = ? " ;
}
if(bqm.getName()!=null && bqm.getName().trim().length()>0){
sql += " and name like ?";
}
if(bqm.getInPrice()>0){
sql += " and inPrice >= ?";
}
if(bqm.getInPrice2()>0){
sql += " and inPrice <= ?";
}
if(bqm.getSalePrice()>0){
sql += " and salePrice >= ?";
}
if(bqm.getSalePrice2()>0){
sql += " and salePrice <= ?";
}
return sql;
}
//给问号赋值
private void setValue(PreparedStatement pstmt,BookQueryModel bqm) throws SQLException{
int count = 1;
if(bqm.getId()!=null && bqm.getId().trim().length()>0){
pstmt.setString(count, bqm.getId());
count++;
}
if(bqm.getName()!=null && bqm.getName().trim().length()>0){
pstmt.setString(count, "%"+bqm.getName()+"%");
count++;
}
if(bqm.getInPrice()>0){
pstmt.setDouble(count, bqm.getInPrice());
count++;
}
if(bqm.getInPrice2()>0){
pstmt.setDouble(count, bqm.getInPrice2());
count++;
}
if(bqm.getSalePrice()>0){
pstmt.setDouble(count, bqm.getSalePrice());
count++;
}
if(bqm.getSalePrice2()>0){
pstmt.setDouble(count, bqm.getSalePrice2());
count++;
}
}
@Override
public Collection<BookModel> getByCondition(BookQueryModel bqm) {
Connection conn = null;
Collection<BookModel> col = new ArrayList();
try {
conn = DBconn.getConn();
String sql = "select * from scott.book where 1=1 ";
//拼Sql
System.out.println(bqm);
this.parparedSql(sql, bqm);
sql += " order by id ASC";
System.out.println("sql========="+sql);
PreparedStatement pstmt = conn.prepareStatement(sql);
this.setValue(pstmt, bqm);
ResultSet rt = pstmt.executeQuery();
while(rt.next()){
String id = rt.getString("id");
String name = rt.getString("name");
Double inPrice = rt.getDouble("inPrice");
Double salePrice = rt.getDouble("salePrice");
BookModel bm = new BookModel();
bm.setId(id);
bm.setName(name);
bm.setInPrice(inPrice);
bm.setSalePrice(salePrice);
col.add(bm);
}
} catch (Exception e) {
e.printStackTrace();
}
return col;
}下面
解决方案 »
- 请推荐SSH2的书籍
- dwr[org.directwebremoting.dwrp.BaseCallMarshaller]Erroring: batchId[3]
- JdbcTemplate的疑问!
- 头疼,ActionMessage(),java.lang.NullPointerException
- lomboz没有装上
- ?在together中要逆向工程怎么弄,就是从源码生成图
- Hibernate HQL问题
- 100分求在java中用代码实现将一个xml文件以Html格式显示出来
- 奇怪了,为什么这个问题以前都没有遇到过呢?有关中文问题,大家看看,较怪!
- 请问我在soap服务端的函数中可以返回一个JDOM的Document对象吗?
- hibernate 关系映射问题.
- 配置eBuilder和intra-mart的过程中出现严重问题
//拼Sql
private String parparedSql(String sql,BookQueryModel bqm){
if(bqm.getId()!=null && bqm.getId().trim().length()>0){
sql += " and id = ? " ;
}
if(bqm.getName()!=null && bqm.getName().trim().length()>0){
sql += " and name like ?";
}
if(bqm.getInPrice()>0){
sql += " and inPrice >= ?";
}
if(bqm.getInPrice2()>0){
sql += " and inPrice <= ?";
}
if(bqm.getSalePrice()>0){
sql += " and salePrice >= ?";
}
if(bqm.getSalePrice2()>0){
sql += " and salePrice <= ?";
}那应该不会NullPoint啊!我想是不是Double型,bqm.getInPrice()>0这里判断的有没有问题我测试了一下,测试查询id=1这条记录,数据库中存在inPrice不输入值,后台报NullPoint,拼Sql失败,没有执行System.out.println("sql========="+sql);
java.lang.NullPointerException
at jxc.bookmge.vo.BookModel.getInPrice(BookModel.java:25)
at jxc.bookmge.dao.impl.JdbcImpl.parparedSql(JdbcImpl.java:115)
at jxc.bookmge.dao.impl.JdbcImpl.getByCondition(JdbcImpl.java:170)
at jxc.bookmge.business.ebo.BookEbo.getByCondition(BookEbo.java:63)
at jxc.bookmge.web.BookServlet.query(BookServlet.java:211)
at jxc.bookmge.web.BookServlet.doPost(BookServlet.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
sql += " and inPrice >= ?";
}