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) {}
}
1
  • Side note: brace-less if statements are the work of the devil. Jan 6, 2014 at 16:01

4 Answers 4

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; 
    } 
9
  • 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 6, 2014 at 16:04
  • @user3165913 Out of interest, what are the recommendations? Jan 6, 2014 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 6, 2014 at 16:10
  • @user3165913 You may want to post the contents of Polylinje into your question so that our guesswork goes away. Jan 6, 2014 at 16:13
  • 1
    ooooh! ok! :D i thught it was an spelling error you made.. but it seems to work now! thanks :D
    – morxy49
    Jan 6, 2014 at 16:39
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.

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.

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

4
  • Also Polylinje.this.horn.length > 0 won't compile. horn() is a method, but used as a field. Jan 6, 2014 at 15:54
  • thanks! but now i got a new error on the exact same rows. "horn cannot be resolved or is not a field"
    – morxy49
    Jan 6, 2014 at 15:56
  • You seem to have missed the fact that surrounding class is called PolylinjeIterator not Polylinje. Jan 6, 2014 at 15:57
  • yep I did... will fix it
    – giorashc
    Jan 6, 2014 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.