2 Replies - 244 Views - Last Post: 17 September 2017 - 10:38 AM

#1 evo4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 14

non-static method getLog() cannot be referenced from a static contex

Posted 16 September 2017 - 08:06 AM



Hello all my first post here so here goes....

I've made an array of APIs/Wrappers to make life easier when developing webbapps.

I've added a new method in a my Analytics.class bean called getLog() and it compiles with no errors. The code is as follows:



public Map<String, String> getLog() {
		
		Map<String, String> log = new HashMap<String, String>();
		
		try {
			
			MySQL mysql = new MySQL(this.MYSQL_HOST, this.MYSQL_USER, this.MYSQL_PASS, this.MYSQL_DTBA);

			ResultSet dbLog = mysql.getResultSet("SELECT * FROM analytics");

			ResultSetMetaData rsmd = dbLog.getMetaData();
			int columnCount = rsmd.getColumnCount()+1;
			ArrayList<String> dbColumns = new ArrayList<String>();

			for(int i=1;i<columnCount;i++) {

				String columnName = rsmd.getColumnName(i);
				dbColumns.add(columnName);
			}

			while(dbLog.next()) {

				for(String columnName : dbColumns) {

					String value = dbLog.getString(columnName);
					log.put(columnName, value);
				}
			}
		}
		catch(SQLException ex) {
			try {
				FileIO io = new FileIO();
				io.quickAppendToFile(this.exceptionPath, Validation.printExceptionTrace(ex));
			}
			catch(Exception e){}
		}
		
		return log;
	}




I've create a APIs.class servlet is test this new method. APIs.class servlet code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		out.print("Analytics:");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
		Map<String, String> log;
		
		String JSP_SELF = "http://"+request.getServerName()+request.getRequestURI()+"?"+request.getQueryString();
		Analytics log_user = new Analytics("dd/MM/yyyy '@' hh:mm:ss", request.getRemoteAddr(), JSP_SELF);
		log = Analytics.getLog();
		

	}



But when I come to compile APIs.java I get the following compilation error:

D:\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\classes\ewe\test>javac APIs.java
APIs.java:36: error: non-static method getLog() cannot be referenced from a static context
                log = Analytics.getLog();
                               ^
1 error




Any ideas on a solution to this? Man thanks!!

Is This A Good Question/Topic? 0
  • +

Replies To: non-static method getLog() cannot be referenced from a static contex

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 5060
  • View blog
  • Posts: 13,654
  • Joined: 18-April 07

Re: non-static method getLog() cannot be referenced from a static contex

Posted 16 September 2017 - 06:13 PM

Well you defined getLog to be a method of the class instance. In other words you have to create an instance of the class to access the method...

Analytics log_user = new Analytics("dd/MM/yyyy '@' hh:mm:ss", request.getRemoteAddr(), JSP_SELF);
log = log_user.getLog(); //<-- Notice we use the instance of the class.



Now, if you want to access it statically, then you just have to define the method to be static using the static keyword...

public static Map<String, String> getLog() {



After doing this, you can use the class name to access the method like you are. But keep in mind that if you define the method to be static, it can't rely on instance values in the function. Think of static methods like functions that are attached to a class, but not using the class internals. The famous static methods of the 'Math' class are a prime example. cos(), tan() etc are all used by calling Math.cos() or Math.tan() but don't actually use any values in the Math class itself.
Was This Post Helpful? 0
  • +
  • -

#3 evo4ever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 14

Re: non-static method getLog() cannot be referenced from a static contex

Posted 17 September 2017 - 10:38 AM

View PostMartyr2, on 16 September 2017 - 06:13 PM, said:

Well you defined getLog to be a method of the class instance. In other words you have to create an instance of the class to access the method...

Analytics log_user = new Analytics("dd/MM/yyyy '@' hh:mm:ss", request.getRemoteAddr(), JSP_SELF);
log = log_user.getLog(); //<-- Notice we use the instance of the class.



Now, if you want to access it statically, then you just have to define the method to be static using the static keyword...

public static Map<String, String> getLog() {



After doing this, you can use the class name to access the method like you are. But keep in mind that if you define the method to be static, it can't rely on instance values in the function. Think of static methods like functions that are attached to a class, but not using the class internals. The famous static methods of the 'Math' class are a prime example. cos(), tan() etc are all used by calling Math.cos() or Math.tan() but don't actually use any values in the Math class itself.


Here is my Analytics.class:

package ewe.api;

import ewe.api.FileIO;
import ewe.api.Validation;

import java.io.*;
import java.util.*;
import java.text.*;
import java.sql.*;
import java.util.Date;

public class Analytics {
	
		private static final String exceptionPath = "D:\\Apache Software Foundation\\Tomcat 7.0\\webapps\\ROOT\\Logs\\Errors\\analyticsExceptions.txt";
	
		private mysqlDetails MySQL = new mysqlDetails("mysql.conf.xml");
	
		private String MYSQL_HOST = MySQL.get("host");
		private String MYSQL_USER = MySQL.get("user");
		private String MYSQL_PASS = MySQL.get("pass");
		private String MYSQL_DTBA = MySQL.get("dtba");
	
		private String date;
		private String ip;
		private String page;
		
		public Analytics(String dFormat, String ip, String page) {
			
			this.setDate(dFormat);
			this.ip = ip;
			this.page = page;
			
			if(date!=null && ip!=null && page!=null)
				this.logVisitor();
		}
		
		private void setDate(String dFormat) {
			
			Date rawDate = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat(dFormat);
			this.date = sdf.format(rawDate).toString();
		}
		
		private void logVisitor() {
			
			MySQL mysql = new MySQL(this.MYSQL_HOST, this.MYSQL_USER, this.MYSQL_PASS, this.MYSQL_DTBA);
			ResultSet result = mysql.getResultSet("SELECT * FROM analytics WHERE ip='"+this.ip+"'");
			
			try {
				if(result.absolute(1)) {
					
					int visits = result.getInt("total_visits");
					visits += 1;
					mysql.update("UPDATE analytics SET date='"+this.date+"', last_page_visited='"+this.page+"', total_visits='"+visits+"' WHERE ip='"+this.ip+"'");
					mysql.close();
				}
				else {
					
					String new_visitor = "INSERT INTO analytics (date, ip, last_page_visited, total_visits) VALUES ('"+this.date+"', '"+this.ip+"', '"+this.page+"', '1')";
					mysql.update(new_visitor);
					mysql.close();
				}
			}
			catch(SQLException ex){
				try {
					FileIO io = new FileIO();
					io.quickAppendToFile(this.exceptionPath, Validation.printExceptionTrace(ex));
				}
				catch(Exception e){}
			}
		}
	
	public Map<String, String> getLog() {
		
		Map<String, String> log = new HashMap<String, String>();
		
		try {
			
			MySQL mysql = new MySQL(this.MYSQL_HOST, this.MYSQL_USER, this.MYSQL_PASS, this.MYSQL_DTBA);

			ResultSet dbLog = mysql.getResultSet("SELECT * FROM analytics");

			ResultSetMetaData rsmd = dbLog.getMetaData();
			int columnCount = rsmd.getColumnCount()+1;
			ArrayList<String> dbColumns = new ArrayList<String>();

			for(int i=1;i<columnCount;i++) {

				String columnName = rsmd.getColumnName(i);
				dbColumns.add(columnName);
			}

			while(dbLog.next()) {

				for(String columnName : dbColumns) {

					String value = dbLog.getString(columnName);
					log.put(columnName, value);
				}
			}
		}
		catch(SQLException ex) {
			try {
				FileIO io = new FileIO();
				io.quickAppendToFile(this.exceptionPath, Validation.printExceptionTrace(ex));
			}
			catch(Exception e){}
		}
		
		return log;
	}
}




I changed public Map<String, String> getLog() to public static Map<String, String> getLog() and im still getting the same error!

Has this got anything to do with the constructor not being static?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1