• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

non-static methos cannot be referenced from static context?

 
samantha katam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyOne,

I am getting:
java:830: non-static method createDocument(java.io.InputS
tream,boolean,boolean) cannot be referenced from a static context
error when i try to compile the code. Can any one tell me why it sbeing caused and how do i go about correcting it. The error is occuring near EOXMLUtils.createDocument


public static Document getDoc(InputStream in, boolean validate)
throws SAXException, IOException, EOXMLParseException
{
Document document = null;

try
{
// Build a DOM Document from the InputStream
logDebug( "in try for getDoc" );
document = EOXMLUtils.createDocument(in, validate, USE_NAMESPACE);
}
catch (EOXMLParseException e)
{
logDebug("Parse error occurred: EOXMLParseException");
throw e;
}
catch (FileNotFoundException e)
{
if (validate)
{ throw e; }
else
{
e.printStackTrace();
logDebug("\n---> FileNotFoundException caught in "+
"CSSSDKTestSupport.getDoc()\n");
}
}
finally
{
try
{
if (in != null)
{ in.close(); }
}
catch (IOException e)
{ e.printStackTrace(); }
}

return document;
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears that you need to either create an instance of EOXMLUtils and call the method using that instance, or declare the method static within EOXMLUtils. (The second option seems more likely for a "utility" class.)
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a general problem. Whenever you declare a method static, it applies without change to the class, the whole class and nothing but the class.
So, whichever object you are coming from, it is exactly the same. So you can call a static method from an object ("non-static"), but not vice versa.

If you call an instance method or instance variable ("non-static") from a static method, the static method cannot tell how many objects there might or might not be. So, the compiler will complain. Otherwise, if there are several objects of the class, the static method might call the non-static method on the wrong object.

Usually whenever I have had that compiler error, I have been trying to access an instance member of the same class from its static methods. So is your method a member of EOXMUtils?
I agree with Marc Weber that you can create an instance of the class from within a static method (after all, that is what you do if you have a main method within the same class which does something), or set the method you are calling static.
 
samantha katam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys !

It really helped me out. Well i created a instance for EOXMLUtils.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic