반응형

 기본 환경: IDE: Eclipse, Language: Java

 

발생 Error

Java에서 다음 Source Code를 실행할 경우,

else {
	int index = input.indexOf(repOld);
	int count=0;

	while(index > -1) {
		count++;
		index = input.indexOf(repOld, index + repNew.length());
	} // while: find String
			
	if(index!=-1) {
		String inputNew = input.replace(repOld, repNew);
	} System.out.println(inputNew);
}

inputNew cannot be resolved to a variable

→ java.lang.Error: Unresolved compilation problem 발생

 

 

Error 원인

String inputNew를 if-else 구문 내 선언하여 지역변수화 됨

else 구문 밖 println문에서는 해당 변수를 사용할 수 없음

 

 

해결 방법

else 구문 밖에서 String inputNew 선언 후, else 구문 안에서 값 계산

else {
	int index = input.indexOf(repOld);
	int count=0;
        String inputNew=""; // Initialization

	while(index > -1) {
		count++;
		index = input.indexOf(repOld, index + repNew.length());
	} // while: find String
			
	if(input.indexOf(repOld)!=-1) {
		inputNew = input.replace(repOld, repNew);
	} System.out.println(inputNew);
}

 

 

 

반응형
반응형

 기본 환경: IDE: Eclipse, Language: Java

 

발생 Error

Java에서 다음 Source Code를 실행할 경우,

public static void main(String[] args) {
	int a; // 4byte(32bit) memory instance 생성
	System.out.println("Local Variable a = " + a);
}

The local variable a may not have been initialized

→ java.lang.Error: Unresolved compilation problem 발생

 

 

Error 원인

int a 선언 후, 초기값을 지정하지 않음

: method 내부의 지역 변수(Local value)의 경우, garbage value가 초기에 저장되어있으므로 초기값 설정이 필요

 

 

해결 방법

int a 선언 후, 초기값 지정

public static void main(String[] args) {
	int a=0; // 4byte(32bit) memory instance 생성
	System.out.println("Local Variable a = " + a);
}

 

 

 

➕ 전역 변수의 초기값 설정

public class Variable02 {
	int a; // Field, Global Variable: Class 내부에서 사용 가능
	// Field는 초기화가 되어있으므로 변수값 지정없이 사용 가능
	double b;
	static int c;
	// static variable의 경우, memory 할당이 필요없이 자동으로 메모리에 공간이 부여되어있음(메모리 할당 선언 필요X)

	public static void main(String[] args) {
		Variable02 v = new Variable02(); // Variable Class memory 생성
		// v: Variable02의 address 보유(Class_name@16진수_참조주소값)
		System.out.println("Variable02: " + v);
		// Variable02: basic.Variable02@515f550a (pkg.Class@ref_address)
		
		System.out.println("Field a = " + v.a); // field 출력: Default 0
		// Variable02의 주소값을 가진 v에서 해당 클래스의 필드값 a를 출력
		System.out.println("Field b = " + v.b); // field 출력: Default 0.0
		System.out.println("Field c = " + c); // field 출력: Default 0
		// Variable02.c와 동일
		// v.c 사용 필요 X
	}
}

class에서 직접 선언되는 전역변수(Global value)의 경우에는 초기값 설정이 되어있으므로 초기값 설정 불요

 

public class Default_value {
	boolean a;
	char b;
	byte c;
	short d;
	int e;
	long f;
	float g;
	double h;
	
	public static void main(String[] args) {
		Default_value dv = new Default_value();
		System.out.println("Default boolean: " + dv.a); // false
		System.out.println("Default char: " + dv.b); // '\0'
		System.out.println("Default byte: " + dv.c); // 0
		System.out.println("Default short: " + dv.d); // 0
		System.out.println("Default int: " + dv.e); // 0
		System.out.println("Default long: " + dv.f); // 0
		System.out.println("Default float: " + dv.g); // 0.0
		System.out.println("Default double: " + dv.h); // 0.0
	}
	
}

 

 

반응형
반응형

 기본 환경: IDE: Eclipse, Language: Java

 

발생 Exception

Java에서 다음 Source Code를 실행할 경우,

public class Exam {
	private char[] ox=null;

	public void compare() {
		for(int i=0; i<JUNG.length(); i++) {
			if(dap.charAt(i)==JUNG.charAt(i)) {
				ox[i] = (char)'O';
			} else {ox[i] = (char)'X';}
		} // for
	} // compare
}

Exception in thread "main" java.lang.NullPointerException: Cannot store to char array because "this.ox" is null

→ NullPointerException 발생

 

 

Exception 원인

char[]를 선언 후, 배열의 크기를 지정해주는 초기화 작업을 진행하지 않음

 

 

해결 방법

public class Exam {
	private char[] ox=null;

	public void compare() {
    ox = new char[JUNG.length()];
    
		for(int i=0; i<JUNG.length(); i++) {
			if(dap.charAt(i)==JUNG.charAt(i)) {
				ox[i] = (char)'O';
			} else {ox[i] = (char)'X';}
		} // for
	} // compare
}

/*
Result
O	X	X	O	O
*/

ox = new char[JUNG.length()];

: ox 배열에 대해 크기 지정

 

 

 

참고 자료

📑 why can't I assign null to a particular index of Char Array?

📑 Primitive Types

📑 charGPT

 

반응형