-3

I am struggling with this code I have been writing. I did some research but I could not find a solution.

Here is my full error:

No enclosing instance of type Project2 is accessible. Must qualify the allocation with an enclosing instance of type Project2 (e.g. x.new A() where x is an instance of Project2).

Here is my full code:

import java.io.*;
import java.util.Scanner;

public class Project2 {
    public static void main(String[] args) throws IOException {

        SuperheroWithPowers aquaman = new SuperheroWithPowers();

        Superhero2 catwoman = new Superhero2();

        //Aquaman calls marine life
        aquaman.setTelepathy(true);
        if (aquaman.getTelepathy()) {
            System.out.println("Aquaman calls all marine life around him to help defeat an evil villian!");
        }

        //Aquaman fights in the depths
        aquaman.setAdaptability(true);
        if (aquaman.getAdaptability()) {
            System.out.println("Aquaman is able to hand very high depths of water due to his ability to adapt to the ocean!");
        }

        //Aquaman tries to use his super strength
        aquaman.setStrength(true);
        if (aquaman.getStrength()) {
            System.out.println("Aquaman tries to use his super strength but the villian is stronger!");
        }

        //Aquaman uses the trident
        aquaman.setTrident(true);
        if (aquaman.getTrident()) {
            System.out.println("Aquaman finally defeats his evil foe with his powerful trident!");
        }


        //Catwoman uses her equipment
        catwoman.setEquipment(true);
        if (catwoman.getEquipment()) {
            System.out.println("Catwoman uses her bola to trip up evil");
        }

        //Catwoman uses her gymnastics
        catwoman.setGymnast(true);
        if (catwoman.getGymnast()) {
            System.out.println("Catwoman escapes a near death by being very agile");
        }

        //Catwoman uses judo
        catwoman.setJudo(true);
        if (catwoman.getJudo()) {
            System.out.println("Catwoman has incredible combat skills to take down any foe in her path");
        }

        //Catwoman uses thief
        catwoman.setThief(true);
        if (catwoman.getThief()) {
            System.out.println("Catwoman is easily able to sneak at night due to her master theif skills");
        }
    }


    class SuperheroWithPowers {
        String name;
        String alterEgo;
        boolean telepathy;
        boolean adaptability;
        boolean strength;
        boolean trident;

        //Constructor
        public SuperheroWithPowers() {
            name = "Aquaman";
            alterEgo = "Arthur Curry";
            System.out.println("Aquaman created!");
        }//End Constructor


        //Start setters
        public void setTelepathy(boolean x) {
            telepathy = x;
        }

        public void setAdaptability(boolean x) {
            adaptability = x;
        }

        public void setStrength(boolean x) {
            strength = x;
        }

        public void setTrident(boolean x) {
            trident = x;
        }
        //End Setters

        //Start getters
        public boolean getTelepathy() {
            return telepathy;
        }

        public boolean getAdaptability() {
            return adaptability;
        }

        public boolean getStrength() {
            return strength;
        }

        public boolean getTrident() {
            return trident;
        }
        //End Getters

    }//end SuperheroWithPowers

    class Superhero2 {
        String name;
        String alterEgo;
        boolean equipment;
        boolean gymnast;
        boolean judo;
        boolean thief;

        //Constructor
        public Superhero2() {
            name = "Catwoman";
            alterEgo = "Selina Kyle";
            System.out.println("Catwoman created!");
        }//end constructor

        //Start setters
        public void setEquipment(boolean x) {
            equipment = x;
        }

        public void setGymnast(boolean x) {
            gymnast = x;
        }

        public void setJudo(boolean x) {
            judo = x;
        }

        public void setThief(boolean x) {
            thief = x;
        }
        //End setters

        //Start getters
        public boolean getEquipment() {
            return equipment;
        }

        public boolean getGymnast() {
            return gymnast;
        }

        public boolean getJudo() {
            return judo;
        }

        public boolean getThief() {
            return thief;
        }

    }//End Getters
}//End Superhero2

The point of this assignment is to use constructors which is where I believe I went wrong, but I just can't put my finger on it.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • 3
    [Search for the error message](http://stackoverflow.com/search?q=%5Bjava%5D+No+enclosing+instance+of+type+is+accessible.+) (on stackoverflow, with the type-specific information removed). You'll find the answer. In any case, I would recommend *un-nesting* the classes, and after doing such all this will go away (or really, change into something else to fix). – user2864740 Sep 19 '15 at 04:11
  • Sorry i had to edit my post. I did research, but I could not find a solution that would work for me. – Steven Stewart Sep 19 '15 at 04:21
  • If you did research, it's incumbent on you to mention that fact, along with where you looked and what you found, so that we don't waste out time going down the same paths you've already covered. – azurefrog Sep 19 '15 at 04:23
  • Sorry about that. Like I had said, I am new to here and to java and I am only trying to seek help. I have tried the following solutions: http://stackoverflow.com/questions/18690770/no-enclosing-instance-of-type-is-accessible, http://stackoverflow.com/questions/25357115/syntax-error-on-tokens-annotationname-expected-instead-error-on-query, http://stackoverflow.com/questions/9744639/must-qualify-the-allocation-with-an-enclosing-instance-of-type-geolocation – Steven Stewart Sep 19 '15 at 04:26
  • Please don't add additional material as a series of comments on your own question. You have the ability to edit your question for a reason. Go add any additional material to the question itself. – azurefrog Sep 19 '15 at 04:28
  • I've cleaned up a bit of the grammar and phrasing. A few things to note: whether or not you're a beginner isn't relevant - you're a person asking a question to me. To that, explain what you researched, and why that didn't work out for you. – Makoto Sep 19 '15 at 04:37
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 03 '16 at 23:29

1 Answers1

0

The problem is that is missing a right curly brace at Project2 class and rest a right curly brace at Superhero2 class.

Add a right curly brace at Project2 class:

    //Catwoman uses thief
    catwoman.setThief(true);
    if(catwoman.getThief()){
      System.out.println("Catwoman is easily able to sneak at night due to her master theif skills");
    }
  }
} //Add this right curly brace to close the Project2 class

Remove the last right curly brace from Superhero2 class:

}//End Superhero2 -> Remove this right curly brace

I recommend you always indent your code correctly, thus you can avoid this kind of issue.

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
Rodrigo Gomes
  • 346
  • 1
  • 5