Nullpointerexception at java.io.File. <init>(Unknown Source) in Java with Lucene

Asked

Viewed 516 times

-1

I’m getting the following error in my Java code

Exception in thread "Thread-2" java.lang.NullPointerException
    at java.io.File.<init>(Unknown Source)
    at Indexador.indexaArquivosDoDiretorio(Indexador.java:42)
    at Princ$1$1.run(Princ.java:235)
    at java.lang.Thread.run(Unknown Source)

I don’t know why you started giving this error, I believe it is something related to objects of type File do not accept String. Follow the code:

public void indexaArquivosDoDiretorio() {
    try {
        Properties prop = getProp();
        // Diretório que irá guardar o índice;
        String diretorioDosIndices = prop.getProperty("diretorio.indice");
        // Diretório que contém os documentos que serão indexados;
        String diretorioParaIndexar = prop.getProperty("diretorio.fonte");
        File diretorio = new File(diretorioDosIndices);
        apagaIndices(diretorio);
        // Directory: representa o diretório do índice;
        Directory d = new SimpleFSDirectory(diretorio);
        // Analyser/StandardAnalyser: fazem o pré-processamento do texto.
        // Existem analisadores inclusive em português;
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
        // IndexWriterConfig: configurações para criação do índice. No
        // projeto serão utilizados os valores padrão;
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_47,
                analyzer);
        // Inicializa o IndexWriter para gravação;
        writer = new IndexWriter(d, config);
        long inicio = System.currentTimeMillis();
        indexaArquivosDoDiretorio(new File(diretorioParaIndexar));
        // Fecha o IndexWriter e comita as mudanças
        writer.commit();
        writer.close();
        long fim = System.currentTimeMillis();
        JOptionPane.showMessageDialog(
                null,
                "Quantidade de arquivos indexados: " + i + "\n"
                        + "Tempo para indexar: "
                        + String.valueOf((fim - inicio) / 1000) + "s");
    } catch (IOException e) {
        logger.error(e);
    }
}

Line 42:

File diretorio = new File(diretorioDosIndices);
  • 1

    You came to debug and see what the variable content diretorioDosIndices?

  • I tried, but the Eclipse doesn’t stop at the breaks or it doesn’t get at them.

  • 1

    Tried to make a sysout to view the contents of the variable diretorioDosIndices?

1 answer

3


File has constructor that takes as argument an instance of String, see the builder javadoc.

File(String pathname)
Creates a new File instance by Converting the Given pathname string into an Abstract pathname.

This exception occurs because the argument passed in the constructor is null, in that case the variable diretorioDosIndices is void.

In the builder of File who receives String as argument there is the following documentation for released exceptions:

Throws:
Nullpointerexception - If the pathname argument is null

Java doc taken from this address.

Finally, check the value of the variable diretorioDosIndices, because there is no doubt that the call prop.getProperty("diretorio.indice") does not find the property and therefore returns null.

  • Actually, the value was null. But the reason is very strange, because I had to change the name of the prop because it was giving conflict with another that I use in the same class.

Browser other questions tagged

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