java.lang.nullException error

Asked

Viewed 156 times

-2

I am developing software for a video rental company with Pattern MVC (Model-view-controller) design with the Java Persistent Api Framework and the XML (Generic Markup Extensible Language).

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

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

Classgrade: String class Path that is coming 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  
  }  

}  

Main 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();

Gravaposition class:

/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)
  • @Guilhermenascimento Your help from Bin folder solved the problem here and that method worked very well! Thanks and thanks!

  • @Math Thanks you too for helping me!

  • I replied @Igorcontini could mark as correct?

  • @Guilhermenascimento Of course I did! Look I didn’t answer you before because I didn’t have internet and because of personal problems...

2 answers

3


As was resolved in the comments then I am turning in response, the String can receive null, and at the time it sends to File the problem occurred, so just add a check, so for example:

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

The code stays this way:

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
        }
    }

}

1

I know it’s solved, but in this part:

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 believe it would solve the problem too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.