Non-static trying to access static
I'm trying to get the hang of Java by writing a gui xml parser. I'm coming from PHP so programming isn't new, but I didn't do a lot of OO in PHP. Anyway, I have a public method(go) from a public class that returns a variable of type Document.
Error says that the non-static method go cannot be accessed from a static context. This code is in a listener method set up like this: private void startAction(java.awt.event.ActionEvent evt). Should I make this method public? I don't really understand where the static part comes into the picture when no method is marked static except main
Message was edited by:
subnetrx
[647 byte] By [
subnetrxa] at [2007-10-3 2:26:41]

A static method belongs to the class and not to an instance of that class. This means you don't need to create an instance of the class, you can call that method directly. For example the Math class has static methods so you do this
double d = Math.pow(2.0, 3.0);
instead of
Math m = new Math();
double d = m.pow(2.0, 3.0);
So if you are in a static method and you need to call a non static method then you need to create an instance of that class first.
Hard to say without seeing code, but my guess is you're doing something like this: MyClass.someMethod();
when you need to be doing something like this: MyClass mine = new MyClass();
mine.someMethod();
ClassName.methodName() is how you invoke static methods. You need to create an instance of the class--an object--in order to invoke a non-static method.
jverda at 2007-7-14 19:25:45 >

ok, here's what I have:
public class TidyProc
public Document go(String userURL)
that is called from this method that is an eventlistener for a button in my GUI class
private void startAction(java.awt.event.ActionEvent evt)
{
userURL=jTextField1.getText();
try
{
baseURL= new java.net.URL(userURL);
HttpURLConnection con = (HttpURLConnection) baseURL.openConnection();
int respcode=con.getResponseCode();
if (respcode != 200)
{
jOptionPane1.showMessageDialog(jEditorPane1,
"Cannot connect to the URL, returned error code: " + respcode,
"URL Warning",
jOptionPane1.WARNING_MESSAGE);
jTextField1.requestFocusInWindow();
}
else
{
TidyProc tidyProc1 = new TidyProc();
Document pagedoc=TidyProc.go(userURL);
}
}
catch(java.net.MalformedURLException me)
{
//custom title, warning icon
jOptionPane1.showMessageDialog(jEditorPane1,
"Please try entering the URL again\nError: " + me.toString(),
"URL Warning",
jOptionPane1.WARNING_MESSAGE);
jTextField1.requestFocusInWindow();
}
catch(java.io.IOException ie)
{
jOptionPane1.showMessageDialog(jEditorPane1,
"Please check that URL exists and try again",
"URL Warning",
jOptionPane1.WARNING_MESSAGE);
jTextField1.requestFocusInWindow();
}
}
In accord with what the others said above, the error is:
Document pagedoc=TidyProc.go(userURL);
should be
Document pagedoc=tidyProc1.go(userURL);
I don't know what "go" does, but as your code stands, you aren't doing anything with the returned Document (the "pagedoc"). It will go out of scope immediately, as it is at the end of the block.
MLRona at 2007-7-14 19:25:45 >
