JAVA NullPointerException 도와주세요 ㅠㅠ
글쓴이: onyva93 / 작성시간: 목, 2016/04/14 - 8:51오후
안녕하세요, 자바를 이제 배우기 시작한 초보입니다.
학교 과제로 간단한 프로그램을 만들고 있는데,
NullPointerException에 대한 개념이 안잡혀서 여쭤보고싶습니다.
Null을 가리키면 오류가 나는 오류타입인 걸로 생각해서 모든 변수에 값을 지정해줬는데요. (null이 되지 않도록)
그래도 모든 부분에서 NPE가 뜨더라구요 ㅠㅠ 이거 어떻게해결하나요?? 정말 하나하나 다 오류처리를 해줘야하나요?
아니면 함수를 다시 짜면 될까요?
아래는 예시 함수입니다 seatmap.view(seats); if(seats[i][j].reserved == true) isOccupied() 등 오류가 안나는부분이 없습니다 ㅠㅠ
극장 좌석예약하는 함수이고
위 Seat클래스는 Seat에 대한 클래스 생성,
SeatMap클래스는 좌석예약,취소 등 기능을 수행하는 클래스입니다..
//1번소스
package NewTheater; public class Seat{ public String name = "name"; public int num = 0; public boolean reserved = false; public String getName(){ return name; } public void setName(String s){ name = s; } public void cancel(){ reserved = false; } public boolean isOccupied(){ return reserved; } } //2번소스 package NewTheater; import java.util.Scanner; public class SeatMap{ public static void main(String[] args){ Seat[][] seats = new Seat[5][]; for(int i=0; i<5; i++){ seats[i]= new Seat[9]; } for(int i=0; i<5; i++) for(int j=0; j<9; j++){ seats[i][j].name="lee"; seats[i][j].num = 0; seats[i][j].reserved = false; } } public void reserve(Seat[][] seats){ String tempStr; int tempNum; int tempRow; int tempCol; Scanner scanf = new Scanner(System.in); System.out.print("name: "); tempStr = scanf.nextLine(); System.out.println(); System.out.print("How many seats: "); tempNum = scanf.nextInt(); System.out.println(); // Char를 입력받아야 하는데 int를 캐릭터로 변경시켜줘야함 System.out.print("Row: "); tempRow = (scanf.nextInt()); System.out.println(); System.out.print("Column: "); tempCol = scanf.nextInt(); System.out.println(); while(seats[tempRow-65][tempCol].isOccupied() == true){ System.out.println("Seat's already taken. Try another one."); System.out.print("Row: "); tempRow = (scanf.nextInt()); System.out.println(); System.out.print("Column: "); tempCol = scanf.nextInt(); System.out.println(); } seats[tempRow-65][tempCol].setName(tempStr); seats[tempRow-65][tempCol].num = tempNum; seats[tempRow-65][tempCol].reserved = true; this.showMap(seats); NewTheater.func(seats); } public void view(Seat[][] seats){ int count= 1; for(int i=0; i<5;i++) for(int j=0; j<9; j++) if(seats[i][j].reserved == true) { System.out.print(count+": "+seats[i][j].getName()+" "+ (char)(i+65)+j); count++;} this.showMap(seats); NewTheater.func(seats); } public void cancel(Seat[][] seats){ Scanner scanf = new Scanner(System.in); System.out.print("cancel whom : "); String delName = scanf.nextLine(); for(int i=0; i<5;i++) for(int j=0; j<9; j++) if(seats[i][j].name == delName) seats[i][j].cancel(); this.showMap(seats); NewTheater.func(seats); } public void showMap(Seat[][] seats){ String[][] copyOne = new String[5][9]; System.out.println(" 1 2 3 4 5 6 7 8 9"); for(int i=0; i<5; i++){ System.out.print((char)(i+65)); for(int j=0; j<9; j++){ if(seats[i][j].isOccupied()) System.out.print("[O] "); else System.out.print("[ ] "); } } } }
Forums:
댓글 달기