[트러블슈팅]No enclosing instance of type Solution is accessible

HwangHyoSang·2022년 7월 10일

트러블슈팅

목록 보기
1/2
  1. 현상파악

(1) 전체코드

package org.opentutorials.java_start.eclipse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Solution {

    class Pair{
        int x;
        int y;

        Pair(int x,int y){
            this.x = x;
            this.y = y;
        }
    }
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        //BufferedReader br = new BufferedReader(new FileReader("D:\\input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int T = Integer.parseInt(br.readLine());
        for(int i = 0; i < T; ++i) {
            int N= Integer.parseInt(br.readLine());
            String str = br.readLine();
            StringTokenizer st = new StringTokenizer(str," ");
            Pair home = new Pair(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));


        }
    }
}

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15OZ4qAPICFAYD
의 문제를 구현하던 중,

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

메세지가 뜨면서

Pair home = new Pair(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));

에서 Pair클래스의 변수 선언에 에러발생.

  1. 추정원인

Pair클래스를 제외한 다른 변수선언시 문제가 발생되지 않았으므로, Pair클래스에 문제가 있다고 판단.

  1. 추정원인에 의거한 조치

Pair클래스의 선언과 내용 자체는 문제가 없었으므로, Pair클래스를 선언한 위치를 변경시도

  1. 최종 조치 결과

Pair클래스를 Soultion 클래스 바깥에 위치한 결과, 정상동작

Soultion 클래스 내에 Pair클래스를 내부 클래스로 선언한 것에 의해

발생한 문제로 확인.

  1. 수정 코드

    package org.opentutorials.java_start.eclipse;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.StringTokenizer;
    class Pair{
        int x;
        int y;

        Pair(int x,int y){
            this.x = x;
            this.y = y;
        }
    }

    public class Solution {
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            //BufferedReader br = new BufferedReader(new FileReader("D:\\input.txt"));
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            int T = Integer.parseInt(br.readLine());
            for(int i = 0; i < T; ++i) {
                int N= Integer.parseInt(br.readLine());
                String str = br.readLine();
                StringTokenizer st = new StringTokenizer(str," ");
                Pair home = new Pair(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));


            }
        }
    }
profile
하루의 기억을 정리하는 기록장

0개의 댓글