2011-12-02 53 views
-1

我有以下SQL查詢:的Java空指針異常SQL預處理語句

private static final String SQL_LIST_ALL = 
    "SELECT DISTINCT * " 
    + "FROM usuarios WHERE NOT EXISTS (SELECT * FROM usuarios_grupos WHERE usuarios_grupos.id_grupo = ? AND usuarios_grupos.id_usuario = usuarios.id_usuario)"; 

駐留在我的名單()方法:

public List<Usuarious> list() throws DAOExceptions { 
    Connection connection = null; 
    PreparedStatement preparedStatement = null; 
    ResultSet resultSet = null; 
    List<Usuarious> users = new ArrayList<Usuarious>(); 

    try { 
     connection = daoFactory.getConnection(); 
     preparedStatement = connection.prepareStatement(SQL_LIST_ALL); 
     preparedStatement.setInt(1,groups.getId_grupo()); 
     resultSet = preparedStatement.executeQuery(); 
     while (resultSet.next()) { 
      users.add(mapUser(resultSet)); 
     } 
    } catch (SQLException e) { 
     throw new DAOExceptions(e); 
    } finally { 
     close(connection, preparedStatement, resultSet); 
    } 

    return users; 
} 

我得到一個空指針異常,當我運行程序。它發生在這裏:

preparedStatement = connection.prepareStatement(SQL_LIST_ALL); 
    preparedStatement.setInt(1,groups.getId_grupo()); 

我的結果集的方法: 「?」

private static Usuarious mapUser(ResultSet rs) throws SQLException { 
    Usuarious user = new Usuarious(rs.getInt("id_usuario"), rs.getString("nome"), rs.getString("setor"), 
      rs.getString("senha"), rs.getString("email"), rs.getString("bloquear"), rs.getString("admin")); 
    return user; 
} 

我試圖將id_grupo通入在我的SQL語句上。但是,這些方法在我的UserDAO中。所以,我在DAO中創建了一個新的組對象來檢索id_grupo。

誰能告訴我這裏有什麼問題嗎?

我的堆棧跟蹤:

javax.el.ELException: /index.xhtml @43,78 value="#{usuariousGruposBean.listOfUsuarios}": Error reading 'listOfUsuarios' on type br.view.UsuariousGruposBean 
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114) 
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) 
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) 
at javax.faces.component.UIData.getValue(UIData.java:731) 
at javax.faces.component.UIData.getDataModel(UIData.java:1798) 
at javax.faces.component.UIData.getRowCount(UIData.java:356) 
at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:456) 
at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:201) 
at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:180) 
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:85) 
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) 
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1763) 
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168) 
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845) 
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756) 
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759) 

Caused by: java.lang.NullPointerException 
    at br.dao.UsuariousDAO.list(UsuariousDAO.java:93) 
    at br.view.UsuariousGruposBean.getListOfUsuarios(UsuariousGruposBean.java:47) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:616) 

好吧,我剛剛創建一個組對象。在此圖所示:

public class UsuariousDAO { 


private static final String SQL_LIST_ALL = 
     "SELECT DISTINCT * " 
     + "FROM usuarios WHERE NOT EXISTS (SELECT * FROM usuarios_grupos WHERE usuarios_grupos.id_grupo = ? AND usuarios_grupos.id_usuario = usuarios.id_usuario)"; 


private DAOFactory daoFactory; 

Grupos groups = new Grupos(); 
+0

在這兩條線的哪一條線上發生了NPE?我還懷疑代碼示例是不完整的,因爲'groups'是未定義的。 – tobiasbayer

+0

哪一行給出空指針?組不爲null?你可以顯示堆棧跟蹤? –

+0

我在這兩行得到Null指針:preparedStatement = connection.prepareStatement(SQL_LIST_ALL);preparedStatement.setInt(1,groups.getId_grupo()); @CodeBrickie – ShaunK

回答

0

檢查代碼 groups.getId_grupo()

是一個空對象。 您的功能必須有一個類型的對象類似參數。

+0

所以我必須在我的參數中包含以下列表(Grupos組)? – ShaunK