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) {}
}
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
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.
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. Eg:
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;
}
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.