java.lang.NumberFormatException: null what does it mean?

2

I'm getting an error and I do not understand where I have it, it says it is when importing the file however after reviewing everything I still do not understand.

  

java.lang.NumberFormatException: null

This is the method to load interface info:

 public void cargarInfo ()
 {

        JFileChooser archivo = new JFileChooser("./data");
        archivo.setDialogTitle("Escoger Dificultad");


        int resultado = archivo.showOpenDialog(this);

        if(resultado == JFileChooser.APPROVE_OPTION);
        {
            cueva=null;
            panelTablero.removeAll();
            panelTablero.revalidate();
            panelTablero.repaint();
            archivoNivel = archivo.getSelectedFile( );
        }
    try
    {
        cueva=new Cueva(archivoNivel);
        //panelControles.refrescar();
        panelTablero.setSize(ancho,alto);
        panelTablero.inicializar(cueva.darFilas(),cueva.darColumnas(),cueva.darBitmap());
        //panelTablero.iluminar();
        int ancho = (darCueva().darColumnas()*51);  
        int alto=(darCueva().darFilas()*52);
        int anchoP=ancho+228;
        int altoP=alto+235;

        setSize(anchoP,altoP);

        validate();
        repaint();
    }

    catch(Exception e)
    {
        e.printStackTrace();
        JOptionPane.showMessageDialog(this, e.getMessage());

    }
    }

And this is the world:

public Cueva(File archivo) throws Exception
{
    Properties datos = cargarInfo(archivo);
    String numeroFilas1 = datos.getProperty( "filas" );
    filas = Integer.parseInt( numeroFilas1 );
    String columnas1 = datos.getProperty( "columnas" );
    columnas = Integer.parseInt( columnas1 );
    inicializar(datos);
 }
    
asked by user69155 07.12.2017 в 13:09
source

1 answer

3

The exception NumberFormatException is thrown when:

  

Thrown to indicate that the application has attempted to convert to a string of the numeric types, but that the string does not have the appropriate format.

In Spanish:

  

It is launched to indicate that the application has tried to convert a string to one of the numeric types, but the string does not have the appropriate format.

In the Integer documentation .Integer () you can verify that this exception is used to indicate that the string that is passed to it as a parameter is not a number.

I recommend you reinforce the code with blocks try/catch :

public Cueva(File archivo) throws Exception
{
    Properties datos = cargarInfo(archivo);
    String numeroFilas1 = datos.getProperty( "filas" );
    try {
        filas = Integer.parseInt( numeroFilas1 );
    } catch (NumberFormatException e) {
        /* Qué hacer en caso de que no sea un número correcto */
    }
    String columnas1 = datos.getProperty( "columnas" );
    try {
        columnas = Integer.parseInt( columnas1 );
    } catch (NumberFormatException e) {
        /* Qué hacer en caso de que no sea un número correcto */
    }
    inicializar(datos);
}

Also, it seems that everything comes from that there is no such property. As you can see in the documentation of Properties .getProperty () will return null if it does not exist, and that's why you get that in the exception.

One way to detect that the property does not exist and generate an exception to indicate it would be:

public Cueva(File archivo) throws Exception
{
    Properties datos = cargarInfo(archivo);
    String numeroFilas1 = datos.getProperty( "filas" );
    if (numeroFilas1 == null) {
        throw new Exception("La propiedad 'filas' no está definida");
    }
    try {
        filas = Integer.parseInt( numeroFilas1 );
    } catch (NumberFormatException e) {
        /* Qué hacer en caso de que no sea un número correcto */
    }
    String columnas1 = datos.getProperty( "columnas" );
    if (columnas1 == null) {
        throw new Exception("La propiedad 'columnas' no está definida");
    }
    try {
        columnas = Integer.parseInt( columnas1 );
    } catch (NumberFormatException e) {
        /* Qué hacer en caso de que no sea un número correcto */
    }
    inicializar(datos);
}
    
answered by 07.12.2017 в 13:23