-2

I have got a problem with my program. Eclipse writes:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Cannot make a static reference to the non-static method umfang() from the type GeoPolygon
Cannot make a static reference to the non-static method laenge() from the type GeoPolyline

What have I done wrong? I've got 2 errors in my App.class. Could you improve my code and explain? Thank you in advance.

    import java.util.ArrayList; //import Array List (java.util)
import java.util.List;  //import List (java.util)

public class App {      //new public class App


    public static void main(String[] args) { //main methode

        GeoPoint p1 = new GeoPoint(10.681,-48.857);   // new Points (x,y)
        GeoPoint p2 = new GeoPoint(96.980,20.724);
        GeoPoint p3 = new GeoPoint(66.647,-66.558);
        GeoPoint p4 = new GeoPoint(-2.674,-58.571);
        GeoPoint p5 = new GeoPoint(40.11,-12.342);
        GeoPoint p6 = new GeoPoint(27.782,46.809);
        GeoPoint p7 = new GeoPoint(54.759,-46.709);
        GeoPoint p8 = new GeoPoint(-33.89,-90.787);
        GeoPoint p9 = new GeoPoint(15.84,-67.553);
        GeoPoint p10 = new GeoPoint(19.481,51.331);


        List<GeoPoint> list1 = new ArrayList<GeoPoint>(); // Create ArrayList and add Points
        list1.add(p1);
        list1.add(p2);
        list1.add(p3);
        list1.add(p4);
        list1.add(p5);
        list1.add(p6);
        list1.add(p7);
        list1.add(p8);
        list1.add(p9);
        list1.add(p10);



        BoundingBox bbox = new BoundingBox(list1);          // get the list from BoundingBox
        double c = GeoPolygon.umfang();     //new variable c = height
        double d = GeoPolyline.laenge();        //new variable d = width

        System.out.println("Das Array beinhaltet die Punkte: " + list1 ); // This prints: "Das Array beinhaltet die Punkte" and the list
        System.out.println("Der Gesamtlaenge des Polygons betraegt (" + d +")." );  // This prints the polygons circumference
        System.out.println("Der Umang des Polygons betraegt: (" + c +")." );    // This prints the polygons area


    }
} 

Polygon

import java.util.List;

public class GeoPolygon implements Geometry {

    private List<GeoPoint> Polylinelist;
    private String name;
    private double umfang;

    public double umfang() {

        umfang = 0;

        for (int i = 0; i < Polylinelist.size() -1 ; i++) {
            GeoPoint p1 = Polylinelist.get(i);
            GeoPoint p2 = Polylinelist.get(i + 1);

            umfang += GeoUtil.distance(p1, p2);

        }
        umfang += GeoUtil.distance(Polylinelist.get(0), Polylinelist.get(Polylinelist.size() -1 ));

        return umfang;
    }

    public GeoPolygon(List<GeoPoint> punktliste, String name) {
        this.Polylinelist = punktliste;
        this.name = name;
    }

    public BoundingBox getBoundingBox() {
        BoundingBox bbox = new BoundingBox(Polylinelist);
        return bbox;
    }

    public String getName() {
        {
            return name;
        }
    }

    public void addGeoPoint(GeoPoint geoPoint) {
        Polylinelist.add(geoPoint);
    }

}

GeoPolyline

import java.util.List;

public class GeoPolyline implements Geometry {

    private List<GeoPoint> Polylinelist;
    private String name;
    private double laenge;

    public double laenge() {

        laenge = 0;

        for (int i = 0; i < Polylinelist.size() - 1; i++) {
            GeoPoint p1 = Polylinelist.get(i);
            GeoPoint p2 = Polylinelist.get(i + 1);

            laenge += GeoUtil.distance(p1, p2);

        }

        return laenge;
    }

    public BoundingBox getBoundingBox() {
        BoundingBox bbox = new BoundingBox(Polylinelist);
        return bbox;
    }

    public GeoPolyline(List<GeoPoint> punktliste, String name) {    //Konstruktor
        this.Polylinelist = punktliste;
        this.name = name;
    }

    public String getName() {
        {
            return name;
        }
    }

    public List<GeoPoint> getPolylinelist() {
        {
            return Polylinelist;
        }

    }

    public void addGeoPoint(GeoPoint geoPoint) {
        Polylinelist.add(geoPoint);
    }

}
Forseth11
  • 1,418
  • 1
  • 12
  • 21
inerini9
  • 55
  • 1
  • 8
  • Those aren't static methods, meaning you need to utilize an instance of that class to access the methods. new GeoPolygon().umfang(); or whatever. Commenting from my phone so it's difficult but that's the idea. – zgc7009 May 28 '15 at 23:19
  • http://stackoverflow.com/questions/11906913/java-static-vs-instance – Anatoly May 28 '15 at 23:23
  • Please, check for already existing answers about the subject. http://stackoverflow.com/questions/4969171/cannot-make-static-reference-to-non-static-method – Federico Cristina May 28 '15 at 23:29

3 Answers3

3
double c = GeoPolygon.umfang();     
double d = GeoPolyline.laenge();

Those are not static methods. If you want them to be static, declare them as such.

Or, you can make an instance of the class before calling them:

eg:

GeoPolygon x = new GeoPolygon();
double c = x.umfang();
alainlompo
  • 4,414
  • 4
  • 32
  • 41
Aify
  • 3,543
  • 3
  • 24
  • 43
1

Inside your App class you have the following line inside public static void main:

double c = GeoPolygon.umfang();

Notice how the method it is inside of is static. Because of this you can not call umfang() because it is not static. The same goes for laenge().

Read about static here

You must create a new instance of GeoPolygon and GeoPolyline if you want to call a non-static method from it.

You can access static items directly in a class, but you can not access non-static items directly in a class. To access non-static items, such as methods or fields, you must create a new instance of that class.

Community
  • 1
  • 1
Forseth11
  • 1,418
  • 1
  • 12
  • 21
0

you need to add the keyword static to the methods on GeoPolygon

    public static double umfang() {
    ...
    }
JSelser
  • 3,510
  • 1
  • 19
  • 40