JAVA NullPointerException 도와주세요 ㅠㅠ

onyva93의 이미지

안녕하세요, 자바를 이제 배우기 시작한 초보입니다.

학교 과제로 간단한 프로그램을 만들고 있는데,
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("[ ] ");
			}
		}
	}
 
 
 
 
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.