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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM