-1

I'm using eclipse as IDE for java.
I wrote the following code, but I have one error on loadStrade(). Eclipse suggested me to change loadStrade from public void to public static, and I don't understand why?
I've looked for similar problem and I've found some problems like mine, but I still not understand why I have to change method to static. Uffa!

In the code, routesNet is a graph (jgraphT), and loadStrade() is used to populate vertex and edge.
Can I have help. Thanks, Fabrizio

public class GestioneStrade {

private Stradario routesNet;

public static void main(String[] args) {

    /*
     * Instantiate Stradario and fill it with routes and cross
     * 
     */
    GestioneStrade m = new GestioneStrade(); //istance of gestionestrade ok?
    // now I set new routesNet
    m.setRoutesNet(new Stradario());

    loadStrade(m.getRoutesNet());  // why loadStrade must be static :-(

}

public Stradario getRoutesNet() {
    return routesNet;
}

public void setRoutesNet(Stradario routesNet) {
    this.routesNet = routesNet;
}

public void loadStrade(Stradario str) {
    // some code to fill routesNet

}
NoobEditor
  • 15,563
  • 19
  • 81
  • 112
Fabrizio R.
  • 117
  • 1
  • 14
  • 1
    You are calling the method from a static context (the static method `main`). Either make the method `static` , or call the method on an instance of `GestioneStrade`, e.g `m.loadStrade(m.getRoutesNet());` – Arnaud Jan 05 '17 at 09:57
  • Is this class a `nested` class? – ImAtWar Jan 05 '17 at 09:58
  • Note: it's not telling you to change `public void` to `public static`, it will be to `public static void`: you still need a return type. – Andy Turner Jan 05 '17 at 09:58
  • See [Non-static variable cannot be referenced from a static context](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context). – MC Emperor Jan 05 '17 at 10:00
  • is your method enclosed inside class? – Sudip Bhandari Jan 05 '17 at 10:02

1 Answers1

1

In "main" you should replace

loadStrade(m.getRoutesNet());

to

m.loadStrade(m.getRoutesNet());

And leave loadStrade as non-static.

Dmitry Gorkovets
  • 2,208
  • 1
  • 10
  • 19
  • Or(/and) change `loadStrade` so that it doesn't take the parameter at all, and have it call its own `getRoutesNet()` method. – Andy Turner Jan 05 '17 at 10:01
  • Thaks you very much, now is clear to me. As a java beginner, things like this, take me lot of time to be solved. – Fabrizio R. Jan 05 '17 at 19:54