2016-05-21 4 views
0

私は、MySQLからいくつかの行を選択して、Javaクラスデータは、MySQLから選択されるが、手順は、NullPointerExceptionが

この中でArrayListの に入れしようとしているスロークエリです:

"SELECT user_id,book_id,preference FROM rating LIMIT %d OFFSET %d" 

I (私は、サーブレットを使用して10行に行を制限する):私はGlassFishのサーバーログで見つけたもの。この

ratesDATA bk = new ratesDATA(); 
       bk.setUser_id(rs.getLong("user_id")); 
       bk.setBook_id(rs.getLong("book_id")); 
       bk.setPref(rs.getDouble("preference")); 
       System.out.println("TTTTNNNN"+rs.getLong("user_id")+"/"+rs.getLong("book_id")+"/"+rs.getDouble("preference")); 

       list.add(bk); 

のArrayListに格納する前にデータを検証してみてください

------- 10正しい行および例外------

Infos: TTTTNNNN1/0/4.0 
Infos: TTTTNNNN1/3/3.0 
Infos: TTTTNNNN1/4/3.0 
Infos: TTTTNNNN1/5/1.0 
Infos: TTTTNNNN1/6/4.0 
Infos: TTTTNNNN1/8/2.0 
Infos: TTTTNNNN2/0/3.0 
Infos: TTTTNNNN2/1/3.0 
Infos: TTTTNNNN2/4/2.0 
Infos: TTTTNNNN2/7/1.0 
java.lang.NullPointerException 
    at com.vaannila.ratesDATA.loadList(ratesDATA.java:134) 
    at com.servlet.nextRatesPage.doGet(nextRatesPage.java:57) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) 

この手順の完全なコードである:

public void loadList(int firstrow,int rowcount) 
    { 

      com.mysql.jdbc.PreparedStatement ps = null; 



    list= new ArrayList<ratesDATA>(); 
      ResultSet rs = null; 

      String url="jdbc:mysql://127.0.0.1:3306/pfadb"; 
      String name="root"; 
      String pw="root"; 
      String driver="com.mysql.jdbc.Driver";  


      Connection connexion=null; 


try 
{  
    Class.forName(driver).newInstance(); 
    connexion=DriverManager.getConnection(url,name,pw);   
    String SQL_SUBLIST = "SELECT user_id,book_id,preference FROM rating LIMIT %d OFFSET %d"; 

//String q = String.format(SQL_SUBLIST, firstrow, rowcount); 
String q = String.format(SQL_SUBLIST, rowcount, firstrow); 

      Statement commande=connexion.createStatement(); 
       rs=commande.executeQuery(q);     
       while (rs.next()) 
       { 
       ratesDATA bk = new ratesDATA(); 
       bk.setUser_id(rs.getLong("user_id")); 
       bk.setBook_id(rs.getLong("book_id")); 
       bk.setPref(rs.getDouble("preference")); 
       System.out.println("TTTTNNNN"+rs.getLong("user_id")+"/"+rs.getLong("book_id")+"/"+rs.getDouble("preference")); 

       list.add(bk); 

       } 

       } 

       catch(Exception e) 
       { 
       e.printStackTrace(); 

       } 
       finally 
       { 
       try 
       { 
       connexion.close(); 
       ps.close(); 
       } 
       catch(Exception e) 
       { 
       e.printStackTrace(); 
       } 
       }  








    } 

、これがいっぱいになりますサーブレットのコード:

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     int page = 1; 
     try{ 
        int recordsPerPage = 10; 
     if(request.getParameter("page") != null) 
      page = Integer.parseInt(request.getParameter("page")); 
     ratesDATA dao = new ratesDATA(); 
       dao.loadList((page-1)*recordsPerPage,recordsPerPage); 

     List<ratesDATA> list = dao.getList(); 



     int noOfRecords = dao.getNoOfRecords(); 
     int noOfPages = (int) Math.ceil(noOfRecords * 1.0/recordsPerPage); 

     request.setAttribute("rateslist", list); 
     request.setAttribute("noOfPages", noOfPages); 
     request.setAttribute("currentPage", page); 
     RequestDispatcher view = request.getRequestDispatcher("adminRates.jsp"); 
     view.forward(request, response); 


       } catch (Exception ex) { 
        System.out.println("TTTTTTTTTTTTTTTT"+ex.getMessage()); 

     } 
    } 

答えて

0

Closeステートメントあなたが接続を閉じる前


psとは何ですか?

ps.close(); 
+0

感謝この小さなコードは私の問題 – Taha

0

最後にコードを変更してください。もう一度ブロックしてpsをrsに変更する必要はありません。返信 ため

finally { 
    rs.close(); 
    connexion.close(); 
} 
+0

の源だった助けてくれてありがとう NullPointerExceptionが消えます – Taha

関連する問題