Question

I tried to run easy web project on Intellij 13.1 and got next error:

SEVERE: Servlet.service() for servlet [com.java.task11.webapp.LoginServlet] in context with path [] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoSuchMethodError: com.java.task11.controller.dao.factory.DAOFactory.getUserDAO()Lcom/java/task11/controller/dao/factory/UserDAO;
    at com.java.task11.controller.service.UserService.getByEmail(UserService.java:39)
    at com.java.task11.webapp.LoginServlet.doPost(LoginServlet.java:41)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

From java API:

Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method.

Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.

Here is 41 line at LoginServlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");

    HttpSession session = request.getSession();
    String email = request.getParameter("email");
    String password = MD5Utils.getMD5String(request.getParameter("password"));
    User user = null;
    try {
        user = new UserService().getByEmail(email);        // 41 line
    } catch (DAOException e) {
        log.error(e);
    }

39 line at UserService:

public User getByEmail(String email) throws DAOException {
    if (DAOFactory.getInstance().getUserDAO().getByEmail(email).isEmpty()) {   // 39 line
        return null;
    } else {
        return DAOFactory.getInstance().getUserDAO().getByEmail(email).get(0);
    }
}

From stack trace L means about trouble with reference - an instance of class.

But all should be work.

Here is snippet of DAOFactory:

public abstract class DAOFactory {
    private static DAOFactory instance;

    public static DAOFactory getInstance() throws DAOException {
        try {
            if (null == instance) {
                instance = (DAOFactory) Class
                        .forName("com.java.task11.controller.dao.implement.JDBCDAOFactory")
                        .newInstance();
            }
        } catch (Exception e) {
            throw new DAOException("Could not create the DAOFactory instance", e);
        }

        return instance;
    }

    public abstract UserDAO getUserDAO();

UPDATE:

I have implementation of this interface:

public class UserDAOImpl implements UserDAO {
   // other methods
    public List<User> getByEmail(String email) throws DAOException {
    PreparedStatement ps = null;
    ResultSet rs = null;
    List<User> ret = new ArrayList<User>();

    try {
        if (null == email) {
            ps = getConn().prepareStatement(
                    DBUtil.selectNull(tableName, allColumns,
                            Arrays.asList("email")));
        } else {
            ps = getConn().prepareStatement(
                    DBUtil.select(tableName, allColumns,
                            Arrays.asList("email")));
            DBUtil.bind(ps, 1, email);
        }

        rs = ps.executeQuery();

        while (rs.next())
            ret.add(fromResultSet(rs));
    } catch (SQLException e) {
        throw new DAOException(e);
    } finally {
        DBUtil.close(ps, rs);
    }

    return ret;
}

And last puzzle of this logic is JDDCFactory class:

public class JDBCDAOFactory extends DAOFactory {

    public UserDAO getUserDAO() {
        return new UserDAOImpl();
    }

All necessary jars are added to class path. Even more this code was working, this happen after I pulled from remote repo. But at other guys all works fine.

How to solve this trouble?

Was it helpful?

Solution

You must implement the getUserDAO() method.

Try removing getInstance here:

public User getByEmail(String email) throws DAOException {
    if (DAOFactory.getUserDAO().getByEmail(email).isEmpty()) {   // 39 line
        return null;
    } else {
        return DAOFactory.getUserDAO().getByEmail(email).get(0);
    }
}

And in DAOFactory implementing getUserDAO() as follows:

public UserDAO getUserDAO() {
    return getInstance().getUserDAO();
}

OTHER TIPS

Edit: Add the getEmail() to your interface.

public User getByEmail(String email) throws DAOException {
    if (DAOFactory.getUserDAO().getByEmail(email).isEmpty()) {   // 39 line
        return null;
    }
}

Since your getUserDAO() is returning the UserDAO (your interface), it throws MethodNotFoundException as your interface does not implement the method. You should now add it to your method or re-think what getUserDAO() returns.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top