0

First of all, sorry for the Swedish in my code. It's a school assignment and they are written in Swedish... I hope the code is understandable.

I get this error on three lines in my code and have no idea why.

no enclosing instance of the type Polylinje is accessible in scope

My code is:

public class PolylinjeIterator { 
    private int aktuell = -1; 

    public PolylinjeIterator (){ 
        if (Polylinje.this.horn.length > 0) // ERROR HERE!
            aktuell = 0; 
    } 

    public boolean finnsHorn (){ 
        return aktuell != -1; 
    } 

    public Punkt horn () 
            throws java.util.NoSuchElementException{ 
        if (!this.finnsHorn ()) 
            throw new java.util.NoSuchElementException ( 
                    "slut av iterationen"); 

        Punkt horn = Polylinje.this.horn[aktuell]; // ERROR HERE!

        return horn; 
            } 

    public void gaFram (){ 
        if (aktuell >= 0 && 
                aktuell < Polylinje.this.horn.length - 1) // ERROR HERE!
            aktuell++; 
        else 
            aktuell = -1; 
    }
} 

the code inside Polylinje.java looks like this:

import java.util.Arrays;
public class Polylinje {

// Instansvariabler

// En tom Polylinje
private Punkt[] horn;

// Polylinjens färg
private String farg = "svart";

// Polylinjens bredd
private int bredd = 1;



// Konstruktorer

// Polylinje skapar en Polylinje utan hörn
public Polylinje () {
    this.horn = new Punkt[0];
}

// Polylinje skapar en Polylinje med argument
public Polylinje (Punkt[] horn, String farg, int bredd)
{
    this.horn = new Punkt[horn.length];
    for (int i = 0; i < horn.length; i++)
        this.horn[i] = new Punkt (horn[i]);

    this.farg = farg;
    this.bredd = bredd;
}
public Polylinje (Punkt[] horn)
{
    this.horn = new Punkt[horn.length];
    for (int i = 0; i < horn.length; i++)
        this.horn[i] = new Punkt (horn[i]);
}



// Konvertorer

//
public String toString () {
    String s = "";
    s = "{"+Arrays.toString(horn)+", "+farg+", "+bredd+"}";
    return s;
}



// Inspektorer

// getHorn returnerar hörnen i form av Punkt-array.
public Punkt[] getHorn () {return horn;}

// getFarg returnerar färgen i form av en String.
public String getFarg () {return farg;}

// getBredd returnerar bredden i form av en integer.
public int getBredd () {return bredd;}



// Mutatorer

// setFarg låter dig ange färgen på en Polylinje.
public void setFarg (String farg) {this.farg = farg;}

// setBredd låter dig ange bredden på en Polylinje.
public void setBredd (int bredd) {this.bredd = bredd;}

// langd beräknar längden på en Polylinje.
public double langd () {

    double langd = 0;
    double d = 0;

    for (int i = 0; i < (horn.length-1); i++){
        d = horn[i].avstand (horn[i+1]);
        langd += d;
    }

    return langd;
}

// laggTill lägger till en linje i slutet av Polylinjen
public void laggTill (Punkt horn) {
    Punkt[] h = new Punkt[this.horn.length + 1];
    int i = 0;
    for (i = 0; i < this.horn.length; i++)
        h[i] = this.horn[i];
    h[i] = new Punkt (horn);
    this.horn = h;
}

// laggTillFramfor lägger till en linje framför en vald linje
public void laggTillFramfor (Punkt horn, String hornNamn)
{
    int pos = -1;

    for(int i = 0; i < this.horn.length; i++){
      if(this.horn[i].namn == hornNamn){
         pos = i;
         break;
      }
    }

    Punkt[] h = new Punkt[this.horn.length + 1];

    for (int j = 0; j < pos; j++)
        h[j] = this.horn[j];


    for (int k = pos+1; k < h.length; k++)
        h[k] = this.horn[k-1];

    h[pos] = new Punkt (horn);

    this.horn = h;
}

//
public void taBort (String hornNamn) {}
}
morxy49
  • 3
  • 4

4 Answers4

0

If you want to refer to an instance of Polylinje inside PolylinjeIterator you will need to pass an instance of PolylinjeIterator to the constructor :

  public PolylinjeIterator (Polylinje polylinjeInstance){ 
        if (polylinjeInstance.horn().length > 0) // Assuming Punkt has a length member and horn is a method in Polylinje 
            aktuell = 0; 
    } 

If you want to use the Polylinje in different places in your PolylinjeIterator class create a class member and assign the given instance to this member in the constructor. Then use the member in your PolylinjeIterator class.

Using Polylinje.this is meaningless since Classes do not have their own member as an instance. The instance is what you create as a concrete entity of your class so whenever you refer to this the class name is not needed

giorashc
  • 13,502
  • 3
  • 32
  • 69
0

The espression Polylinje.this.horn in your code is not valid. If you need to access a horn attribute in an instance of class Polylinje you need to make this instance accessible to class PolylinjeIterator, possibly by giving it an attribute of class Polylinje and initializing it in PolylinjeIterator's constructor.

You also appear to use the horn identifier in three different ways: as a method of class PolylinjeIterator, as a local variable in this method and possibly as an attribute of class Polylinje; this is likely one source of confusion you should try to remove.

Nicola Musatti
  • 16,551
  • 1
  • 40
  • 50
0

In Java, you use the phrase Foo.this to refer to the enclosing type from within an anonymous class. See this question for more details.

You are not in this situation.

Based on your latest question edit, you need to just call the getters. E.g.:

if (Polylinje.this.horn.length > 0) // ERROR HERE!

should become:

if (polylinje.getHorn().length > 0)

Which will work if you have a field in your class called polylinje that you insert during your constructor, for instance:

public class PolylinjeIterator { 
    private int aktuell = -1; 
    private final Polylinje polylinje;

    public PolylinjeIterator (Polylinje polylinje){ 
        this.polylinje = polylinje;
        if (polylinje.getHorn().length > 0) 
            aktuell = 0; 
    } 
Community
  • 1
  • 1
Duncan Jones
  • 59,308
  • 24
  • 169
  • 227
  • this seemed to work, but gave some "recommendations" (the yellow lightbulbs in eclipse) in Polylinje.java instead. altough everything seems to work, so i hope this is correct :) – morxy49 Jan 06 '14 at 16:04
  • @user3165913 Out of interest, what are the recommendations? – Duncan Jones Jan 06 '14 at 16:04
  • nope, when i did this i ruined the code in some other places :/ the recommendetions said "the static field Polylinje.horn should be accesed in a static way" – morxy49 Jan 06 '14 at 16:10
  • @user3165913 You may want to post the contents of `Polylinje` into your question so that our guesswork goes away. – Duncan Jones Jan 06 '14 at 16:13
  • what do you mean with "Which will work if you have a field in your class called `polylinje` that you insert during your constructor."? – morxy49 Jan 06 '14 at 16:27
  • @user3165913 Well, that is what my code example at the end shows you. – Duncan Jones Jan 06 '14 at 16:28
  • i've added the code example at the end to my code, and then i get a new error on the exact same rows. "cannot make a static reference to the non-static method getHorn() from the type Polylinje" but i cannot make getHorn static, the rest of the code will fail then. – morxy49 Jan 06 '14 at 16:35
  • @user3165913 You must have made a mistake. You must have typed `Polylinje.getHorn()` (note the capital 'P') at some point when I've suggested you should use `polylinje.getHorn()`. It sounds like you need to do some research to understand the difference between instance and static methods in Java. (Try [this question](http://stackoverflow.com/questions/3903537/i-want-to-know-the-difference-between-static-method-and-non-static-method) for example). – Duncan Jones Jan 06 '14 at 16:37
  • 1
    ooooh! ok! :D i thught it was an spelling error you made.. but it seems to work now! thanks :D – morxy49 Jan 06 '14 at 16:39
0

Polylinje.this means that you are accessing 'this' instance in Polylinje class and/or inside an inner class of Polylinje, this is useful when you use inside a non-static inner class (member class)/anonymous inner class of Polylinje. A solution is to create an instance of Polylinje in PolylinjeIterator and access horn through an accessor or do the required operation in Polylinje, or maybe declare horn in PolylinjeIterator.

Laksitha Ranasingha
  • 3,654
  • 1
  • 26
  • 33