java.lang.nullException error

-2

I'm developing software for an MVC (Model-view-controller) design video store with the Api Persistent Java Framework and the Extensible Markup Language (XML) language.

My problem is this: When I log into the login screen of my system, a java.lan.nullException happens because a String is coming as null.

How can I solve this java.lang.nullException problem?

ClassGravaPosition: String class Path that is null.

package classes_utilitarias;

import java.awt.Graphics;  
import java.awt.Image;  
import java.awt.Toolkit;  
import java.io.File;
import javax.swing.JDesktopPane;  


 public class AlteraFundo extends JDesktopPane {  

  Image img;

  public AlteraFundo(String caminho){


          File imagem = new File(caminho);  
    try  
    {  
      img = javax.imageio.ImageIO.read(imagem);  
    }  
    catch(Exception e){}//do nothing  
  }  

}  

Home Screen:

package formularios;
import classes_persistencia.AcessoDAO;
import classes_registros.Acesso;
import java.awt.BorderLayout;
import java.beans.PropertyVetoException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import classes_utilitarias.AlteraFundo;
import classes_utilitarias.GravaPosicao;
import java.awt.Color;
import java.awt.Component;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

private static String Caminho_fundo = "";

 private void initComponents() {

        try{
            mdi_principal = new classes_utilitarias.AlteraFundo(GravaPosicao.le_conf_geral("Caminho_fundo"));
            tool_principal = new javax.swing.JToolBar();

Class GravaPosition:

/Método SalvaPosicao, recebe o nome do Frm que está chando e a posição dele, grava no arquivo
public class GravaPosicao {    

    public static void salvaPosicao(String nomeFrm, Point posicao){
        String newLine = System.getProperty("line.separator");
        String gravaPosicao= ""+ nomeFrm +":"+ String.valueOf(posicao.x) +"x"+ String.valueOf(posicao.y)+newLine;

        //System.out.print(gravaPosicao);

        try{            
            UtilsArquivo.salvar("c:/Singelo/position.inf", gravaPosicao, true);
        }
        catch(Exception e){
            e.printStackTrace();
        }        
    }

public static String le_conf_geral(String nome_ctxt) throws FileNotFoundException, IOException{

        String ler;
        String []info;
        String retorno= null;
        try{
            File arquivo= new File("c:/Singelo/config.inf");
            if(!(arquivo.exists())){
                return null;
            }
            FileReader leitura2= new FileReader("c:/Singelo/config.inf");

            BufferedReader leitor2= new BufferedReader(leitura2);


            while((ler = leitor2.readLine()) != null){
                info=ler.trim().split(">");
                if(info[0].equals(nome_ctxt)){
                    retorno= info[1];
                    break;
                }
            }
            leitura2.close();
        }
        catch(Exception p){
            p.printStackTrace();
        }
        return retorno;               
    }

}

Error log:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.io.File.<init>(File.java:277)
    at classes_utilitarias.AlteraFundo.<init>(AlteraFundo.java:21)
    at formularios.FrmPrincipal.initComponents(FrmPrincipal.java:91)
    at formularios.FrmPrincipal.<init>(FrmPrincipal.java:64)
    at formularios.FrmLogin.ir(FrmLogin.java:239)
    at formularios.FrmLogin.bt_entrarActionPerformed(FrmLogin.java:137)
    at formularios.FrmLogin.access$000(FrmLogin.java:20)
    at formularios.FrmLogin$1.actionPerformed(FrmLogin.java:69)
    
asked by anonymous 09.06.2016 / 15:18

2 answers

4

Since it was resolved in the comments then I am transforming in response, the String can receive null , and the moment it sends to File the problem happened, so just add a check, like this:

minhaString != null && !minhaString.isEmpty()

The code looks like this:

package classes_utilitarias;

import java.awt.Graphics;  
import java.awt.Image;  
import java.awt.Toolkit;  
import java.io.File;
import javax.swing.JDesktopPane;  

public class AlteraFundo extends JDesktopPane
{
    Image img;

    public AlteraFundo(String caminho)
    {
        if (caminho != null && !caminho.isEmpty()) {
            File imagem = new File(caminho);
            try {
                img = javax.imageio.ImageIO.read(imagem);
            }
            catch(Exception e){}//do nothing
        }
    }

}
    
15.06.2016 / 20:36
1

I know that it is solved but in this section:

String retorno= null;
        try{
            File arquivo= new File("c:/Singelo/config.inf");
            if(!(arquivo.exists())){
                return null;
            }

You could put a default return instead of returning null, I think it would solve the problem as well.

    
16.06.2016 / 01:38