【问题标题】:java null pointer exception SQL prepared statementjava空指针异常SQL准备语句
【发布时间】:2011-12-02 11:03:44
【问题描述】:

我有以下 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)";

位于我的 list() 方法中:

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)

好吧,我刚刚创建了一个 Group 对象。它显示在这里:

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();

【问题讨论】:

  • nes NPE 发生在哪两条线上?我还怀疑代码示例不完整,因为 groups 未定义。
  • 哪一行给出了空指针?组不为空吗?你能显示堆栈跟踪吗?
  • 我在这两行得到空指针:preparedStatement = connection.prepareStatement(SQL_LIST_ALL); PreparedStatement.setInt(1,groups.getId_grupo()); @CodeBrickie
  • 我创建了一个新的 Group 对象,Grupos groups = new Grupos()。组会为空吗? @RogerLindsjö
  • 所以您的connection 为空? daoFactory 长什么样子?

标签: mysql sql jdbc


【解决方案1】:

检查该代码 groups.getId_grupo()

groups 是一个空对象。 您的函数必须有一个 groups 类型的对象,例如参数。

【讨论】:

  • 所以我必须在我的参数中包含以下列表(Grupos 组)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多