Pegar o caminho do arquivo usando FILE (Erro)

2 respostas
juceliohv

Olá Pessoal :D

Tenho o seguinte problema:

Criei um objeto com nome ArqDir. Ele recebe um inteiro e um java.io.File no construtor. Quando eu crio este objeto através do JFileChooser.getSelectedFile() ele retorna um File e manda para o construtor do ArqDir. Até ai beleza! Pego o ArqDir e faço ArqDir.getArqPath.getAbsolutePath. Mas quando eu crio o objeto a partir de uma String, new ArqDir(numero, new File(minhaStringDoPath)) >>> ArqDir.getArqPath.getAbsolutePath() , ocorre nullPointException. Alguém saberia me dizer por que isso ocorre :?:

Segue código.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui.util;

import java.io.File;

/**
 *
 * @author juc
 */
public class ArqDir {
    private int tipoArq;
    private File arqPath;

    public ArqDir(Integer tpArq, File path){
        super();
        this.tipoArq = tpArq;
        this.arqPath = arqPath;
        System.out.print(path.getAbsolutePath());
    }

    public int getTipoArq()     {   return this.tipoArq;    }
    public File getArqPath()  {   return this.arqPath;    }

    public void setTipoArq(int tpArq)   { this.tipoArq = tpArq;    }
    public void setArqPath(File path) { this.arqPath = path;     }

        

}
if (jTF_arqPisCofins.getText() == ""){
            listaErro.add(new ErrorMessage( 1,
                                            1,
                                            "O arquivo EFD Pis/Cofins não foi informado!",
                                            "Favor informar um arquivo válido para a EFD Pis/Cofins."));
        } else {
            arqPisCofins = new ArqDir(EFD_PISCOFINS_FILE, new File(jTF_arqPisCofins.getText()));
        }
        
        System.out.println(arqPisCofins.getArqPath().getAbsolutePath());
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at gui.AvalArqSPEDFiscal.validaParametros(AvalArqSPEDFiscal.java:1185)
        at gui.AvalArqSPEDFiscal.jB_execAvalActionPerformed(AvalArqSPEDFiscal.java:910)
        at gui.AvalArqSPEDFiscal.access$000(AvalArqSPEDFiscal.java:41)
        at gui.AvalArqSPEDFiscal$1.actionPerformed(AvalArqSPEDFiscal.java:271)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
        at java.awt.Component.processMouseEvent(Component.java:6263)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6028)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

2 Respostas

M

juceliohv, bom dia

vc chegou a debugar? tem certeza que o valor de jTF_arqPisCofins.getText() não está sendo retornado null?
Pois na validação do if vc verifica se é string vazia “”, mas não verifica se o valor está nulo.

juceliohv

Bom dia,

Olá msantaguida, já sim.

Resolvi da seguinte maneira. Estendi meu objeto de File e uso diretamente o métoos do pai. Assim não deu erro.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui.util;

import java.io.File;

/**
 *
 * @author juc
 */
public class ArqDir extends File{
    private int tipoArq;
    
    public ArqDir(Integer tpArq, File path){
        super(path.getAbsolutePath());
        this.tipoArq = tpArq;
         
    }

    public int getTipoArq()     {   return this.tipoArq;    }
    
    public void setTipoArq(int tpArq)   { this.tipoArq = tpArq;    }        

}
Criado 4 de maio de 2011
Ultima resposta 4 de mai. de 2011
Respostas 2
Participantes 2