Java 从.txt填充数组时出现NullPointerException

Java 从.txt填充数组时出现NullPointerException,java,nullpointerexception,Java,Nullpointerexception,这里的交易,我需要填写一个数组从一个.txt文件。I'v使用Scanner类读取每一行,并从INT中获取要存储在数组中的令牌的位置: import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class Aplicacion { static Elemento _tablero[][] = new Ele

这里的交易,我需要填写一个数组从一个.txt文件。I'v使用Scanner类读取每一行,并从INT中获取要存储在数组中的令牌的位置:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class Aplicacion {

    static Elemento _tablero[][] = new Elemento[8][8];


    public static Elemento[][] Leertxt() throws FileNotFoundException,IOException
    { 
        Scanner sc = new Scanner(new File("C:/Users/Owner/Documents/UNIMET/Trimestre  5/Estructura de Datos/Proyecto 1a/src/inicio.txt"));

        while(sc.hasNext())
        {
            String ln = sc.next();          

           if (ln.equals("Pared"))
        {
            int i = sc.nextInt();
            int j = sc.nextInt();

             _tablero[i][j] = new Pared(i,j);//crea una pared nueva
        } 
         else if (ln.equals("Fantasma"))    
          {
             int i = sc.nextInt();
             int j = sc.nextInt(); 

           _tablero[i][j] = new Fantasma(i,j);//crea un fantasma nuevo
      }
        else if (ln.equals("Vacio"))
        {
            int i = sc.nextInt();
            int j = sc.nextInt();

            _tablero[i][j] = new Vacio(i,j); //crea un vacio
        }


     }


       for(int i=0; i<_tablero.length;i++)
        {
          for(int j=0;j<_tablero.length;j++)
            {
               if (_tablero[i][j] instanceof Vacio)
               {
                 _tablero[i][j] = null; 

                 _tablero[i][j] = new Punto(i,j);
            }
        }
    }     return _tablero;
}  

public  void mostrar() throws FileNotFoundException, IOException
{   Elemento[][] tab = Leertxt(); 
    for (int i = 0; i < tab.length; i++)
  { for(int j = 0;j < tab.length; j++)
      {
           System.out.print("  "+ tab[i][j].mostrar();
          }
       System.out.println();//salto de linea
      }
}
我不明白第73行的NullPointerException是从哪里来的。
mostrar方法是Elemento类中的一个抽象方法,它只打印一个符号…任何帮助都会被愉快地接受

,因为当您尝试调用
选项卡[i][j]时<代码>选项卡[i][j]
null
。您从未在数组中的该位置放置对象

Leertxt()
方法中没有任何东西可以确保所有64个位置都接收到一个对象

如果要找出该位置,请将循环更改为:

Elemento[][] tab = Leertxt(); 
for (int i = 0; i < tab.length; i++)
{ 
    for(int j = 0;j < tab[i].length; j++)
    {
        if (tab[i][j] == null)
            System.out.println("null at location: [" + i + "," + j + "]");
        else
            System.out.print("  "+ tab[i][j].mostrar();
    }
    System.out.println();//salto de linea
}
Elemento[]tab=Leertxt();
对于(int i=0;i
继Brian Roach的评论之后,我建议您添加以下内容:

if (_tablero[i][j] instanceof Vacio)
要将其扩展为包含null(我假设在空白处,您也会包含null):

因此,如果您的文本文件中有未定义的内容,它将在此处定义。如果您愿意,您甚至可以在它检测到空方块时抛出一个标志,以确保跟踪空方块

另一种选择是围绕:

System.out.print("  "+ tab[i][j].mostrar());
ie问题行73带有尝试/捕捉块或三元操作:

try{
    System.out.print("  "+ tab[i][j].mostrar());
} catch (NullPointerException ex){
    //haz algo
}


mostrar()
中只有一个解引用……您几乎可以从中推断行号;)
System.out.print("  "+ tab[i][j].mostrar());
try{
    System.out.print("  "+ tab[i][j].mostrar());
} catch (NullPointerException ex){
    //haz algo
}
System.out.print("  "+ ((tab[i][j] == null) ? "null" : tab[i][j].mostrar()));