0

I have a JSP application with a plain java class of Login.java and a servlet with a call to a procedure named loginList in the doGet method. The loginList procedure needs to create a list of logins using a java class named OAVDbUtil with a method "getLoginsList". But Eclipse does not seem to recognise the "getLoginsList" procedure and when I enter it the massage of "eclipse cannot make a static reference to the non-static method" is given. But everything looks okay as I have not stated the getLoginsList as static. I think there is a way to create an instance of the OAVDbUtil to avoid having to create multiple instances of New OAVdbUtil objects but can someone tell me how to do this please and NOT get the message of "eclipse cannot make a static reference to the non-static method"?

Here is some code and a screen dump

Screen dump of servlet

enter image description here

Here is the code for the OAVDbUtil

public OAVDbUtil(DataSource theDataSource) {
  dataSource = theDataSource;
}

public List<Login> getLoginsList() throws Exception {

   List<Login> loginList = new ArrayList<Login>();
AJF
  • 1,801
  • 4
  • 27
  • 54

4 Answers4

0

getLoginsList() is not static, make it and it will work.

Tony
  • 72
  • 1
  • 8
  • Yes i tried that but it causes other problems. Also, I don't actually want getLoginsList to be static. When i type the period after OAVDbUtil the method getLoginsList does NOT appear in the intellisense – AJF May 06 '16 at 16:40
0

You are getting this error because getLoginsList() is not static.

  1. You can change that method static

    public static List<Login> getLoginsList() throws Exception {
    

(Or)

  1. Create a object for OAVDbUtil

    OAVDbUtil dbUtil = new OAVDbUtil();
    List<Login> logins = dbUtil.getLoginsList();
    
Gangaraju
  • 4,406
  • 9
  • 45
  • 77
0

OAVDbUtil.getLoginsList() is how you use a static method in a class, you can't use this to access an instance method of a class.

If you intended getLoginsList to be a static method then declare it as such:

public static List<Login> getLoginsList() throws Exception

If you want getLoginsList to be an instance method then you need to have an instance of the OAVDbUtil class and available and call the method using:

OAVDbUtil theInstance = .... get from somewhere

List<Login> logins = theInstance.getLoginsList();
greg-449
  • 109,219
  • 232
  • 102
  • 145
0

You need to provide an instance of OAVDbUtil to the servlet, and then call getLoginsList() on that object.

A servlet might construct such an object in its initialization method, or it could be injected into the servlet by a container.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • Thanks erickson. I created the following initialization in the Servlet; @Override public void init() throws ServletException { super.init(); try{ oavDbUtil = new OAVDbUtil(dataSource); } catch (Exception exc) { throw new ServletException(exc); } } and then changed OAVDbUtil.getLoginsList() to oavDbUtil.getLoginsList() and the error is gone. Am I correct in saying then that instead of having several instances of creating new OAVDbUtil objects this initialization creates one that is then available throughout the servlet?? – AJF May 09 '16 at 11:46