2013-05-20 2 views
1

나는 스도쿠 퍼즐을 생성하고 해결할 자바 프로그램을위한 GUI를 만들고있다. 내가하고있는 방식은 그 안에 3x3 크기의 jpanels가있는 1 개의 주요 jpanel을 가지고있다. 이런 일이 왜자바 스윙에서 스도쿠 보드를 만들려고합니다. NullPointerException 오류가 발생 했습니까?

board.add(subBoard[i][j]); 

누군가가 설명 할 수 :이 라인에 NullPointerException이 오류를 얻고있다

package sudoku; 

import java.awt.GridLayout; 
import javax.swing.*; 
import javax.swing.border.Border; 

public class Gui { 

    Gui gui; 
    JPanel board; 
    JPanel subBoard[][]; 
    GridLayout layout; 
    JLabel square[][]; 
    Border big; 
    Border small; 

    public void load() { 

     gui = new Gui(); 
     gui.setUp(); 
     gui.buildBoard(); 

    } 

    private void setUp() { 

     layout = new GridLayout(3, 3); 
     board = new JPanel(layout); 
     subBoard = new JPanel[3][3]; 
     square = new JLabel[9][9]; 

    } 

    private void buildBoard() { 

     // set up board 
     board.setVisible(true); 

     int mod = 0; 
     for (int i = 0; i < 3; i++) { 
      for (int j = 0; j < 3; j++) { 
       // add subBoards to board 
       board.add(subBoard[i][j]); 
       subBoard[i][j].setLayout(layout); 

       // add textfields to each subBoard 
       subBoard[i][j].add(square[i + mod][j + mod]); 

      } 
      mod += 3; 

     } 
    } 

} 

: 여기

는 지금까지이 무엇인가? 또한 다른 문제가 있거나 더 좋은 방법이 있다면이 문제를 알려주세요. 감사합니다. . 이 라인

subBoard = new JPanel[3][3]; 

에서 당신의 setUp() 방법에서

답변

4

당신은 단지 배열을 만든 아직 JPanel의의로를 작성하지 않았습니다.

그래서, 기본적으로 배열의 모든 인덱스 위치에 대한 null를 포함하고, 따라서 당신은이 NPE가 발생합니다 (subBoard 달리 제대로 new JPanel(layout)으로 초기화) 당신의 board에 추가하려고 할 때.

0
import java.util.Random; 
import java.util.Scanner; 

public class sudoku { 
    public static void main(String[] args) { 
     int p = 1; 
     Random r = new Random(); 
     int i1=r.nextInt(8); 
     int firstval = i1; 
     while (p == 1) { 
      int x = firstval, v = 1; 
      int a[][] = new int[9][9]; 
      int b[][] = new int[9][9]; 
      for (int i = 0; i < 9; i++) { 
       for (int j = 0; j < 9; j++) { 
        if ((x + j + v) <= 9) 
         a[i][j] = j + x + v; 
        else 
         a[i][j] = j + x + v - 9; 
        if (a[i][j] == 10) 
         a[i][j] = 1; 
        // System.out.print(a[i][j]+" "); 
       } 
       x += 3; 
       if (x >= 9) 
        x = x - 9; 
       // System.out.println(); 
       if (i == 2) { 
        v = 2; 
        x = firstval; 
       } 
       if (i == 5) { 
        v = 3; 
        x = firstval; 
       } 

      } 
      int eorh; 
      Scanner in = new Scanner(System.in); 
      System.out 
        .println("hey lets play a game of sudoku:take down the question and replace the 0's with your digits and complete the game by re entering your answer"); 
      System.out.println("enter your option 1.hard 2.easy"); 
      eorh = in.nextInt(); 
      switch (eorh) { 
      case 1: 
       b[0][0] = a[0][0]; 
       b[8][8] = a[8][8]; 
       b[0][3] = a[0][3]; 
       b[0][4] = a[0][4]; 
       b[1][2] = a[1][2]; 
       b[1][3] = a[1][3]; 
       b[1][6] = a[1][6]; 
       b[1][7] = a[1][7]; 
       b[2][0] = a[2][0]; 
       b[2][4] = a[2][4]; 
       b[2][8] = a[2][8]; 
       b[3][2] = a[3][2]; 
       b[3][8] = a[3][8]; 
       b[4][2] = a[4][2]; 
       b[4][3] = a[4][3]; 
       b[4][5] = a[4][5]; 
       b[4][6] = a[4][6]; 
       b[5][0] = a[5][0]; 
       b[5][6] = a[5][6]; 
       b[6][0] = a[6][0]; 
       b[6][4] = a[6][4]; 
       b[6][8] = a[6][8]; 
       b[7][1] = a[7][1]; 
       b[7][2] = a[7][2]; 
       b[7][5] = a[7][5]; 
       b[7][6] = a[7][6]; 
       b[8][4] = a[8][4]; 
       b[8][5] = a[8][5]; 
       b[0][0] = a[0][0]; 
       b[8][8] = a[8][8]; 

       break; 
      case 2: 
       b[0][3] = a[0][3]; 
       b[0][4] = a[0][4]; 
       b[1][2] = a[1][2]; 
       b[1][3] = a[1][3]; 
       b[1][6] = a[1][6]; 
       b[1][7] = a[1][7]; 
       b[1][8] = a[1][8]; 
       b[2][0] = a[2][0]; 
       b[2][4] = a[2][4]; 
       b[2][8] = a[2][8]; 
       b[3][2] = a[3][2]; 
       b[3][5] = a[3][5]; 
       b[3][8] = a[3][8]; 
       b[4][0] = a[4][0]; 
       b[4][2] = a[4][2]; 
       b[4][3] = a[4][3]; 
       b[4][4] = a[4][4]; 
       b[4][5] = a[4][5]; 
       b[4][6] = a[4][6]; 
       b[5][0] = a[5][0]; 
       b[5][1] = a[5][1]; 
       b[5][4] = a[5][4]; 
       b[5][6] = a[5][6]; 
       b[6][0] = a[6][0]; 
       b[6][4] = a[6][4]; 
       b[6][6] = a[6][6]; 
       b[6][8] = a[6][8]; 
       b[7][0] = a[7][0]; 
       b[7][1] = a[7][1]; 
       b[7][2] = a[7][2]; 
       b[7][5] = a[7][5]; 
       b[7][6] = a[7][6]; 
       b[8][2] = a[8][2]; 
       b[8][4] = a[8][4]; 
       b[8][5] = a[8][5]; 
       break; 
      default: 
       System.out.println("entered option is incorrect"); 
       break; 
      } 

      for (int y = 0; y < 9; y++) { 
       for (int z = 0; z < 9; z++) { 
        System.out.print(b[y][z] + " "); 
       } 
       System.out.println(""); 
      } 
      System.out.println("enter your answer"); 
      int c[][] = new int[9][9]; 
      for (int y = 0; y < 9; y++) { 
       for (int z = 0; z < 9; z++) { 
        c[y][z] = in.nextInt(); 
       } 
      } 
      for (int y = 0; y < 9; y++) { 
       for (int z = 0; z < 9; z++) 
        System.out.print(c[y][z] + " "); 
       System.out.println(); 
      } 
      int q = 0; 
      for (int y = 0; y < 9; y++) { 
       for (int z = 0; z < 9; z++) 
        if (a[y][z] == c[y][z]) 
         continue; 
        else { 
         q++; 
         break; 
        } 
      } 
      if (q == 0) 
       System.out 
         .println("the answer you have entered is correct well done"); 
      else 
       System.out.println("oh wrong answer better luck next time"); 
      System.out 
        .println("do you want to play a different game of sudoku(1/0)"); 
      p = in.nextInt(); 
      firstval=r.nextInt(8); 
      /*if (firstval > 8) 
       firstval -= 9;*/ 
     } 

    } 
} 
+0

이 코드는 스도쿠 질문을 생성하고 사용자가 입력 한 해결 스도쿠를 비교하여 입력 한 스도쿠가 맞는지 여부를 표시합니다. –

+0

멋진 코드 (아마 :-) - 그냥 : 질문에 대한 관계를 많이 볼 수 없습니다 (Swing에서 그렇게하는 것입니다) – kleopatra