java sql预处理,java空指针异常SQL预处理语句

I have the following SQL query:

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

which resides in my list() method:

public List list() throws DAOExceptions {

Connection connection = null;

PreparedStatement preparedStatement = null;

ResultSet resultSet = null;

List users = new ArrayList();

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;

}

I got a null pointer exception when I run my program. It happens here:

preparedStatement = connection.prepareStatement(SQL_LIST_ALL);

preparedStatement.setInt(1,groups.getId_grupo());

My result set method:

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;

}

I am trying to pass the id_grupo into the "?" on my SQL statement. However, these methods are in my UserDAO. So, I created a new group object in the DAO to retrieve the id_grupo.

Can anyone tell me what is wrong here?

My stack trace:

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)

Well, I just created a Group object. It is shown here:

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

解决方案

Check that code

groups.getId_grupo()

groups is an null object.

You function must have one object of type groups like parameter.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值