| Author |
Static context for AbstractAction question
|
David Pantale
Ranch Hand
Joined: Mar 16, 2010
Posts: 31
|
|
I'm sure there's a simple explanation for this but I can't figure it out. I extended AbstractAction and implemented actionPerformed. I want to reference a method in another, clearly non-static, class. However I'm getting a non-static method cannot be referenced from a static context message in the actionPerformed method. I checked AbstractAction and the ActionListener interface and nothing there was static. Can someone please explain why I'm getting this message?
Thanks Dave
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
The compiler is not lying, but it's impossible to see where you're going wrong without some code. Trim it down to the bare minimum to demonstrate the problem, please.
Also:
I want to reference a method in another, clearly non-static, class.
It's not whether the class is static that matters. It's whether you're in a static method or initializer. And in any case, only non-top-level classes can be static.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4901
|
|
David Pantale wrote:I'm sure there's a simple explanation for this but I can't figure it out.
It's a bit tough for us too without seeing your code (and also the exact message you're getting). Plainly a piece of code in a static context is trying to access a method that isn't, but why and where is presumably what you want to know.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
David Pantale
Ranch Hand
Joined: Mar 16, 2010
Posts: 31
|
|
Thanks for the swift responses. The code is below (note: I'm using NetBeans):
//Class that I wish to get information from.
public final class PortfoliosTopComponent extends TopComponent implements ExplorerManager.Provider, LookupListener {
private static transient ExplorerManager em = new ExplorerManager();
private Lookup.Result<PortfolioNode> result = null;
private static PortfolioNode portfolionode = null;
private String portfolioname;
.
.
.
public String getPortfolioName() { //I want to retrieve this value
return portfolioname;
}
}
//Action class that is attempting to get the info
public class AddStockAction extends AbstractAction {
public AddStockAction() {
super("Add Stock");
}
@Override
public void actionPerformed(ActionEvent e) {
String result = JOptionPane.showInputDialog("Enter name:");
if (result == null) {
return;
}
xmlManager xm = new xmlManager();
if (xm.addStock(PortfoliosTopComponent.getPortfolioName(), result)) //Right here is where I get the message:
PortfoliosTopComponent.refreshNode(); //non-static method getPortfolioName cannot be referenced from a static context
}
}
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4901
|
|
David Pantale wrote:
Well PortfoliosTopComponent is a class name, so PortfoliosTopComponent.getPortfolioName() is how you would call a static method.
What you need is an instance (ie, probably a variable) name there instead.
Winston
|
 |
David Pantale
Ranch Hand
Joined: Mar 16, 2010
Posts: 31
|
|
|
Thanks, very good point. Unfortunately I'm not quite sure how I'm going to be able to do this. Any suggestions?
|
 |
David Pantale
Ranch Hand
Joined: Mar 16, 2010
Posts: 31
|
|
Okay, I figured out a way to do it - Netbeans only I'm afraid. To get a reference to a node in another class (TopComponent) use:
Node[] n = TopComponent.getRegistry().getCurrentNodes();
and then, for example, its:
String x = n[0].getDisplayName();
if you want the nodes display name.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
|
That looks wrong just from looking at it.
|
 |
 |
|
|
subject: Static context for AbstractAction question
|
|
|