You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by hugo <hu...@geoinformex.com> on 2002/10/03 05:51:37 UTC

non-static method cannot be referenced from static context error

Hi

I would like to implement an interface called FileRenamePolicy from the 
oreilly package for uploading files, and I am creating a class which 
does the implementation.

Here is the relevant code:

<%@ page session="true"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page import="com.oreilly.servlet.multipart.*" %>

<%
abstract class reName implements FileRenamePolicy {
  public File changename (File oldname) {
  DefaultFileRenamePolicy dfrp = new DefaultFileRenamePolicy();
   FileRenamePolicy frp = (FileRenamePolicy) dfrp;
   File savename = frp.rename(oldname);
  return savename;
  }
}
%>

Apparently this class needed to be abstract for the jsp to compile.

Later on in the code, I would like to rename the file. First I create a 
MultipartRequest object:

MultipartRequest mr = new 
MultipartRequest(request,"/var/www/projects_data/");

Then I upload the file object:

File oldn = mr.getFile(mr.getParameter("audit_doc"));

And then I rename the file:

File newname = reName.changename(oldn);

But here is when I get the error:

"non-static method changename(java.io.File) cannot be referenced from a 
static context"

What does this mean and what am I doing wrong?

After I rename the file, I would still like do download it (using the 
new filename) to /var/www/projects_data/.

I think I have to use a method from the oreilly package named 
FilePart.writeTo().

Would anyone be able to give me a hint on how to use this?

Any help, especially code examples, will be greatly appreciated.

Thanks

Hugo

-- 
Dr Hugo Bouckaert
Systems and Programming Engineer

GeoInformatics Exploration Australia P/L
57 Havelock St
West Perth, WA 6005
PO Box 1675, West Perth 6872

Ph:       61 08 9420 7400
Fax:      61 08 9226 1299

www.geoinformex.com

------------------------------------------------------------------------
This email and any attachments may be confidential or legally 
privileged. If you received this message in error or are not the 
intended recipient, you should destroy the e-mail message and any 
attachments or copies, and you are prohibited from retaining, 
distributing, disclosing or using any information contained herein. 
Please inform us of the erroneous delivery by return e-mail. Thank you 
for your cooperation.



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: non-static method cannot be referenced from static context error

Posted by "J. D. Addison" <pu...@theaddisons.demon.co.uk>.
hugo wrote:
> Hi
> 
> I would like to implement an interface called FileRenamePolicy from the 
> oreilly package for uploading files, and I am creating a class which 
> does the implementation.
> 
> Here is the relevant code:
> 
> <%@ page session="true"%>
> <%@ page import="java.sql.*" %>
> <%@ page import="java.io.*" %>
> <%@ page import="java.net.*" %>
> <%@ page import="java.text.*" %>
> <%@ page import="java.util.*" %>
> <%@ page import="com.oreilly.servlet.*" %>
> <%@ page import="com.oreilly.servlet.multipart.*" %>
> 
> <%
> abstract class reName implements FileRenamePolicy {
>  public File changename (File oldname) {
>  DefaultFileRenamePolicy dfrp = new DefaultFileRenamePolicy();
>   FileRenamePolicy frp = (FileRenamePolicy) dfrp;
>   File savename = frp.rename(oldname);
>  return savename;
>  }
> }
> %>

changeName is an instance method so must be used with an object.
> 
> Apparently this class needed to be abstract for the jsp to compile.
> 
> Later on in the code, I would like to rename the file. First I create a 
> MultipartRequest object:
> 
> MultipartRequest mr = new 
> MultipartRequest(request,"/var/www/projects_data/");
> 
> Then I upload the file object:
> 
> File oldn = mr.getFile(mr.getParameter("audit_doc"));
> 
> And then I rename the file:
> 
> File newname = reName.changename(oldn);

reName is the name  of the (abstact) class. Only static 
methods can be called by using ClassName.method()


> 
> But here is when I get the error:
> 
> "non-static method changename(java.io.File) cannot be referenced from a 
> static context"
> 
> What does this mean and what am I doing wrong?
> 
> After I rename the file, I would still like do download it (using the 
> new filename) to /var/www/projects_data/.
> 
> I think I have to use a method from the oreilly package named 
> FilePart.writeTo().
> 
> Would anyone be able to give me a hint on how to use this?
> 
> Any help, especially code examples, will be greatly appreciated.
> 
> Thanks
> 
> Hugo
> 

As indicated in  the notes above you are trying to use 
changename without having an associated object. Check the 
docs for FileRenamePolicy  to see how it should be used.


-- 
J. D. Addison
email puff@theaddisons.demon.co.uk


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: non-static method cannot be referenced from static context error

Posted by jon wingfield <jo...@mkodo.com>.
The oreilly MultipartRequest uses DefaultFileRenamePolicy as, funnily
enough, a default when constructing as you have it in your code.

Later versions of the API (since 9thMay02) have overloaded constructors
where you can specify the FileRenamePolicy. The policy is asked to rename
the file before FilePart.writeTo(...) is called.

Here's a link to the constructor we use:
http://www.servlets.com/cos/javadoc/com/oreilly/servlet/MultipartRequest.htm
l#MultipartRequest(javax.servlet.http.HttpServletRequest,%20java.lang.String
,%20int,%20com.oreilly.servlet.multipart.FileRenamePolicy)

The latest version of the API is:
http://www.servlets.com/cos/cos-27May2002.zip

Onto the code. The reason the compiler says your rename class should be
abstract is because it doesn't implement FileRenamePolicy. Your class has a
method changename whereas the FileRenamePolicy interface stipulates a method
rename.
Because your class (actually defined within the _jspService(...) method of
the compiled jsp) is abstract you can't instantiate it. However, in your
code you call reName.changename(...). reName is the class name not a
reference to an object of type reName so the compiler thinks you are calling
a static method. The changename method is not static.

Personally, i dislike anything but the simplest jsp scriptlets. Any complex
functionality should not be in the view layer. I would put the upload logic
(manipulation of the multipart request and decisions on where to save files)
in either a servlet or a filter.

So what to do.
1) Move the upload logic elsewhere (servlet/filter your choice). This makes
the code cleaner, easier to test (using tools such as junit
(http://www.junit.org) and mockobjects (http://www.mockobjects.com/)) and
more maintainable.
2) Really implement FileRenamePolicy and not within the jsp. Package it and
make it a standalone class. This follows as consequence of 1) but also
removes the horrible class declaration within a method you had before.

Good luck,

Jon

-----Original Message-----
From: hugo [mailto:hugo@geoinformex.com]
Sent: 03 October 2002 04:52
To: JSP-INTEREST@JAVA.SUN.COM; tomcat-user@jakarta.apache.org
Subject: non-static method cannot be referenced from static context
error


Hi

I would like to implement an interface called FileRenamePolicy from the
oreilly package for uploading files, and I am creating a class which
does the implementation.

Here is the relevant code:

<%@ page session="true"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page import="com.oreilly.servlet.multipart.*" %>

<%
abstract class reName implements FileRenamePolicy {
  public File changename (File oldname) {
  DefaultFileRenamePolicy dfrp = new DefaultFileRenamePolicy();
   FileRenamePolicy frp = (FileRenamePolicy) dfrp;
   File savename = frp.rename(oldname);
  return savename;
  }
}
%>

Apparently this class needed to be abstract for the jsp to compile.

Later on in the code, I would like to rename the file. First I create a
MultipartRequest object:

MultipartRequest mr = new
MultipartRequest(request,"/var/www/projects_data/");

Then I upload the file object:

File oldn = mr.getFile(mr.getParameter("audit_doc"));

And then I rename the file:

File newname = reName.changename(oldn);

But here is when I get the error:

"non-static method changename(java.io.File) cannot be referenced from a
static context"

What does this mean and what am I doing wrong?

After I rename the file, I would still like do download it (using the
new filename) to /var/www/projects_data/.

I think I have to use a method from the oreilly package named
FilePart.writeTo().

Would anyone be able to give me a hint on how to use this?

Any help, especially code examples, will be greatly appreciated.

Thanks

Hugo

--
Dr Hugo Bouckaert
Systems and Programming Engineer

GeoInformatics Exploration Australia P/L
57 Havelock St
West Perth, WA 6005
PO Box 1675, West Perth 6872

Ph:       61 08 9420 7400
Fax:      61 08 9226 1299

www.geoinformex.com

------------------------------------------------------------------------
This email and any attachments may be confidential or legally
privileged. If you received this message in error or are not the
intended recipient, you should destroy the e-mail message and any
attachments or copies, and you are prohibited from retaining,
distributing, disclosing or using any information contained herein.
Please inform us of the erroneous delivery by return e-mail. Thank you
for your cooperation.



--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>