Possible causes and treatment of java.lang.NullPointerException

Source: Internet
Author: User

Possible causes and treatment of java.lang.NullPointerException

Java.lang.NullPointerException specific meaning is null pointer exception, the most common problem is not initialized.

    1. Data types such as strings are not initialized
    2. class instances (objects) useful for specific class initialization
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.