java.lang.NullPointerException的可能原因及处理

java.lang.NullPointerException的可能原因及处理

java.lang.NullPointerException具体意思是空指针异常,最常见的问题就是没有初始化。

  1. 字符串等数据类型没有初始化
  2. 类实例(对象)有用具体的类初始化
  3. 没有判断是否为空

 

Eg:

源码:

 1 public static BookInformation[] ImFromClassification(String a){
 2         Connection conn = null;
 3         PreparedStatement ps = null;
 4         ResultSet rs = null;
 5         try{
 6                 int x = 0;
 7                 conn = LinkMysql.getDBconnection();
 8                 if(conn == null){System.out.println("conn");}
 9                 String sql="select * from bookinformation where classification=?";
10                 ps = conn.prepareStatement(sql);
11                 ps.setString(1, a);
12                 rs = ps.executeQuery();
13                 rs.beforeFirst();
14                 while(rs.next()){
15                     ++x;
16                 }
17                 System.out.println(x);
18                 BookInformation[] a1 = new BookInformation[x];
19                 rs.first();
20                 for(int i = 0; i < x; i++){
21                     //a1[i] = new BookInformation();
22                     a1[i].setName(rs.getString("name"));
23                     a1[i].setAuthor(rs.getString("author"));
24                     a1[i].setClassification(rs.getString("classification"));
25                     a1[i].setAmount(rs.getInt("amount"));
26                     a1[i].setPrice(rs.getInt("price"));
27                     a1[i].setSalvesVolum(rs.getInt("sales_volum"));
28                     rs.next();
29                 }
30                 return a1;
31                 
32         } catch (SQLException e) {
33             System.out.println("xxx");
34             return null;
35         }
36         finally{LinkMysql.closeDB(conn, ps, rs);}
37         
38     }

报错:

root cause

java.lang.NullPointerException
	Dao.BookInfor.ImFromClassification(BookInfor.java:31)
	org.apache.jsp.front.home_jsp._jspService(home_jsp.java:120)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

分析:

数组定义(BookInformation[] a1 = new BookInformation[5])之后,没有对每个数组元素初始化。

更改:

 1 public static BookInformation[] ImFromClassification(String a){
 2         Connection conn = null;
 3         PreparedStatement ps = null;
 4         ResultSet rs = null;
 5         try{
 6                 int x = 0;
 7                 conn = LinkMysql.getDBconnection();
 8                 if(conn == null){System.out.println("conn");}
 9                 String sql="select * from bookinformation where classification=?";
10                 ps = conn.prepareStatement(sql);
11                 ps.setString(1, a);
12                 rs = ps.executeQuery();
13                 rs.beforeFirst();
14                 while(rs.next()){
15                     ++x;
16                 }
17                 System.out.println(x);
18                 BookInformation[] a1 = new BookInformation[x];
19                 rs.first();
20                 for(int i = 0; i < x; i++){
21                     a1[i] = new BookInformation();
22                     a1[i].setName(rs.getString("name"));
23                     a1[i].setAuthor(rs.getString("author"));
24                     a1[i].setClassification(rs.getString("classification"));
25                     a1[i].setAmount(rs.getInt("amount"));
26                     a1[i].setPrice(rs.getInt("price"));
27                     a1[i].setSalvesVolum(rs.getInt("sales_volum"));
28                     rs.next();
29                 }
30                 return a1;
31                 
32         } catch (SQLException e) {
33             System.out.println("xxx");
34             return null;
35         }
36         finally{LinkMysql.closeDB(conn, ps, rs);}
37         
38     }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值