Possible causes and treatment of java.lang.NullPointerException
Java.lang.NullPointerException specific meaning is null pointer exception, the most common problem is not initialized.
- Data types such as strings are not initialized
- class instances (objects) useful for specific class initialization
- There is no judging whether it is empty
Eg:
Source:
1 Public Staticbookinformation[] Imfromclassification (String a) {2Connection conn =NULL;3PreparedStatement PS =NULL;4ResultSet rs =NULL;5 Try{6 intx = 0;7conn =linkmysql.getdbconnection ();8 if(conn = =NULL) {SYSTEM.OUT.PRINTLN ("conn");}9String sql= "SELECT * from Bookinformation where classification=?";TenPS =conn.preparestatement (SQL); OnePs.setstring (1, a); Ars =ps.executequery (); - Rs.beforefirst (); - while(Rs.next ()) { the++x; - } - System.out.println (x); -bookinformation[] A1 =NewBookinformation[x]; + Rs.first (); - for(inti = 0; i < x; i++){ + //A1[i] = new Bookinformation (); AA1[i].setname (rs.getstring ("name")); atA1[i].setauthor (rs.getstring ("Author")); -A1[i].setclassification (rs.getstring ("Classification")); -A1[i].setamount (Rs.getint ("Amount")); -A1[i].setprice (Rs.getint ("Price")); -A1[i].setsalvesvolum (Rs.getint ("Sales_volum")); - Rs.next (); in } - returnA1; to +}Catch(SQLException e) { -SYSTEM.OUT.PRINTLN ("xxx"); the return NULL; * } $ finally{LINKMYSQL.CLOSEDB (conn, PS, RS);}Panax Notoginseng -}
Error:
Root causejava.lang.NullPointerExceptionDao.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)
Analysis:
Array definition (bookinformation[] a1 = new bookinformation[5]), there is no initialization for each array element.
Change:
1 Public Staticbookinformation[] Imfromclassification (String a) {2Connection conn =NULL;3PreparedStatement PS =NULL;4ResultSet rs =NULL;5 Try{6 intx = 0;7conn =linkmysql.getdbconnection ();8 if(conn = =NULL) {SYSTEM.OUT.PRINTLN ("conn");}9String sql= "SELECT * from Bookinformation where classification=?";TenPS =conn.preparestatement (SQL); OnePs.setstring (1, a); Ars =ps.executequery (); - Rs.beforefirst (); - while(Rs.next ()) { the++x; - } - System.out.println (x); -bookinformation[] A1 =NewBookinformation[x]; + Rs.first (); - for(inti = 0; i < x; i++){ +A1[i] =Newbookinformation (); AA1[i].setname (rs.getstring ("name")); atA1[i].setauthor (rs.getstring ("Author")); -A1[i].setclassification (rs.getstring ("Classification")); -A1[i].setamount (Rs.getint ("Amount")); -A1[i].setprice (Rs.getint ("Price")); -A1[i].setsalvesvolum (Rs.getint ("Sales_volum")); - Rs.next (); in } - returnA1; to +}Catch(SQLException e) { -SYSTEM.OUT.PRINTLN ("xxx"); the return NULL; * } $ finally{LINKMYSQL.CLOSEDB (conn, PS, RS);}Panax Notoginseng -}
Possible causes and treatment of java.lang.NullPointerException