//Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
//at Sudoku.azar(Sudoku.java:94)
//at Sudoku$EscuchadorStart.actionPerformed(Sudoku.java:106)
这是按下按钮开始后显示的错误, 程序编译但按下按钮开始时不起作用 我在methot中使用了抛出IOException,但当我调用methot时它说我需要 捕获IOEXception
文本文件juegos是一个包含2个sudokus游戏的文本文件,因此它有9列和18个
这是代码
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Sudoku extends Frame{
private TextField[][] cuadrados=new TextField[9][9];
private Button start=new Button ("Start");
private Button check=new Button("Check");
private Button clear=new Button("Clear");
private Button exit=new Button("Exit");
private int[][] numeros=new int[9][9];
public int[][] auxiliar;
public Sudoku(){
setSize(500,500);
setTitle("Sudoku");
setLayout(new BorderLayout());
Panel pan=new Panel();
pan.setLayout(new GridLayout(9,9));
add(pan, BorderLayout.CENTER);
Font myFont = new Font("ARIALBD",Font.BOLD,30);
for(int a=0;a<9;a++){
for(int b=0;b<9;b++){
cuadrados[a][b]=new TextField();
cuadrados[a][b].setFont(myFont);
cuadrados[a][b].setBackground(Color.pink);
cuadrados[a][b].setForeground(Color.RED);
pan.add(cuadrados[a][b]);
if(a>=0&&a<3){
if((b>=0&&b<3)||(b>=6&&b<9)){
cuadrados[a][b].setBackground(Color.CYAN);
}
}
if(a>=3&&a<6){
if(b>=3&&b<6){
cuadrados[a][b].setBackground(Color.CYAN);
}
}
if(a>=6&&a<9){
if((b>=0&&b<3)||(b>=6&&b<9)){
cuadrados[a][b].setBackground(Color.CYAN);
}
}
}
}
Panel aux=new Panel();
aux.setLayout(new FlowLayout());
aux.add(start);
aux.add(check);
aux.add(clear);
aux.add(exit);
add(aux, BorderLayout.SOUTH);
start.addActionListener(new EscuchadorStart());
check.addActionListener(new EscuchadorCheck());
clear.addActionListener(new EscuchadorClear());
exit.addActionListener(new EscuchadorExit());
}
public static void main(String[] args)throws IOException {
Sudoku x= new Sudoku();
x.setVisible(true);
}
public static int[][] azar ()throws IOException{
int random= (int)(Math.random());
BufferedReader text = new BufferedReader(new FileReader("juegos.txt"));
String[] ayuda=new String[9];
int lines=18;
int[][] WhatReturn=new int[9][9];
String aux="";
for(int c=0;c<17;c++){
aux=text.readLine();
if(c==random*9){
if(c==0){
ayuda[c]=aux;
}
else ayuda[c-9]=aux;
}
}
for(int a=0;a<9;a++){
for(int b=0;b<9;b++){
WhatReturn[a][b]=Integer.parseInt(ayuda[a].substring(b,b+1));
}
}
return WhatReturn;
}
public class EscuchadorStart implements ActionListener {
public void actionPerformed (ActionEvent x) {
try{
int [][]auxiliar= azar();
}
catch(IOException e){
//code to handle an IOException here
}
for(int a=0;a<9;a++){
for(int b=0;b<9;b++){
if(auxiliar[a][b]!=0){
cuadrados[a][b].setText(""+auxiliar[a][b]);
cuadrados[a][b].setEditable(false);
}
else;
}
}
for(int a=0;a<9;a++){
for(int b=0;b<9;b++){
if(cuadrados[a][b].getText().equals("")){
cuadrados[a][b].setForeground(Color.BLUE);
}
else;
}
}
}
}
public class EscuchadorCheck implements ActionListener{
public void actionPerformed(ActionEvent x){
for(int a=0;a<9;a++){
for(int b=0;b<9;b++){
if(cuadrados[a][b].getText().equals("")){
numeros[a][b]=0;
}
else numeros[a][b]=Integer.parseInt(cuadrados[a][b].getText());
}
}
for(int a=0;a<9;a++){
for(int b=0;b<9;b++){
for(int c=b+1;c<9;c++){
if(numeros[a][b]!=0){
if(numeros[a][b]==numeros[a][c]){
cuadrados[a][b].setBackground(Color.RED);
}
}
}
}
}
for(int b=0;b<9;b++){
for(int a=0;a<9;a++){
for(int c=a+1;c<9;c++){
if(numeros[a][b]!=0){
if(numeros[a][b]==numeros[c][b]){
cuadrados[a][b].setBackground(Color.RED);
}
}
}
}
}
}
}
public class EscuchadorExit implements ActionListener{
public void actionPerformed(ActionEvent x){
System.exit(0);
}
}
public class EscuchadorClear implements ActionListener{
public void actionPerformed(ActionEvent x){
for(int a=0;a<9;a++){
for(int b=0;b<9;b++){
cuadrados[a][b].setText("");
}
}
EscuchadorStart y=new EscuchadorStart();
y.actionPerformed(x);
for(int a=0;a<9;a++){
for(int b=0;b<9;b++){
cuadrados[a][b].setBackground(Color.pink);
if(a>=0&&a<3){
if((b>=0&&b<3)||(b>=6&&b<9)){
cuadrados[a][b].setBackground(Color.CYAN);
}
}
if(a>=3&&a<6){
if(b>=3&&b<6){
cuadrados[a][b].setBackground(Color.CYAN);
}
}
if(a>=6&&a<9){
if((b>=0&&b<3)||(b>=6&&b<9)){
cuadrados[a][b].setBackground(Color.CYAN);
}
}
}
}
}
}
}
答案 0 :(得分:1)
public void actionPerformed (ActionEvent x) {
try{
int [][]auxiliar= azar();
}
catch(IOException e){
//code to handle an IOException here
}
// etc
你的问题就在这里。您的auxiliar
变量会影响您的某个实例;在之后的代码中,您尝试访问未初始化的实例变量 - &gt; NPE。
注意:在IOException的情况下,什么也不做是非常糟糕的做法。无论如何,你可能会这样做:
try {
auxiliar = azar();
} catch (IOException e) {
// please handle that
}