[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;
}下面

解决方案 »

  1.   

    有点长啊  我看了下你这个就是从前台jsp取得id ,,price 什么的给后台,,然后通过sql查找出id=id,price=price的记录对吧,,你说你的逻辑层查询,总是NollPoint,,,你可以一步一步的测试,,首先测试第一:salePrice 到底有没值,,第二:把你拼装的sql打印在后台看看,第三:看数据库有这条记录没。。   自己 debug 下  也可以 。。
      

  2.   

    查询时,在前台inprice输入不输入值都应该可以查询比如,我要查id=1的这条记录,我只要输入id=1就应该把这条记录查出来inPrice是价格,查询时可以区间查询但在查询页面价格这一行不输入值,我在后台判断,如果inPriec为空,那Sql就不拼inprice这个字段
    //拼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);
      

  3.   

    id=1,name=,inPrice=null,salePrice=null
    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)
      

  4.   

    后台报JdbcImpl.java:115,指的是if(bqm.getInPrice()>0){    这是第115行
    sql += " and inPrice >= ?";
    }