Erro java.lang.ExceptionInInitializerError[RESOLVIDO]

18 respostas
J

Alguem poderia me ajudar a eliminar os erros java.lang.ExceptionInInitializerError

Porque será que esta caindo nestes erro?
Já tentei usar try …catch(){mensagem…, mas ai so cai nesta mensagem e nao executa o que esta dentro

Este é um codigo de sintese de voz disponivel do FurbSpeech

Exception in thread "main" java.lang.ExceptionInInitializerError
	at br.furb.api.furbspeech.synth.SynthesizerFactory.getSynthesizer(SynthesizerFactory.java:26)
	at br.furb.api.furbspeech.FurbSpeech.speech(FurbSpeech.java:120)
	at br.furb.api.furbspeech.FurbSpeech.main(FurbSpeech.java:48)
Caused by: java.lang.NullPointerException
	at br.furb.api.furbspeech.synth.MBrolaSynthesizer.<clinit>(MBrolaSynthesizer.java:35)
	... 3 more

18 Respostas

P

Coloca o codigo ai da classe MBrolaSynthesizer.java

J

Seria esse aqui olha, eu pensei que poderia ser esse caminho do aquivo exe, sera que é isso?

package br.furb.api.furbspeech.synth;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import br.furb.api.furbspeech.FurbSpeech;
import br.furb.api.furbspeech.util.ComponentUtils;
import br.furb.api.furbspeech.util.OSNotSupportedException;

public class MBrolaSynthesizer implements Synthesizer {

	/**
	 * BR1 voice. Downloaded from Mbrola project web site.
	 */
	public static final File VOICE_BR1 = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource("synthesizer/mbrola/br1").getFile()));
	
	/**
	 * BR2 voice. Downloaded from Mbrola project web site.
	 */
	public static final File VOICE_BR2 = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource("synthesizer/mbrola/br2").getFile()));
	
	/**
	 * BR3 voice. Downloaded from Mbrola project web site.
	 */
	public static final File VOICE_BR3 = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource("synthesizer/mbrola/br3").getFile()));
	
	private File voiceName;
	
	public void setVoice(File voiceName) {
		this.voiceName = voiceName;
	}

	public void synthesize(File inputFile, File outputFile) {
		// should set the default voice?
		if (voiceName == null) {
			voiceName = VOICE_BR1;
		}
		
		String cmdLine = "<mbrola-bin> -e <voice> <input> <output>";
		cmdLine = cmdLine.replace("<mbrola-bin>", getPreparedFilePath(getMBrolaAppAbsolutePath()))
						 .replace("<voice>", getPreparedFilePath(voiceName.getAbsolutePath()))
						 .replace("<input>", getPreparedFilePath(inputFile.getAbsolutePath()))
						 .replace("<output>", getPreparedFilePath(outputFile.getAbsolutePath()));
		try {
			//FurbSpeech.getLogger().debug("Executing command: " + cmdLine);
			Runtime.getRuntime().exec(cmdLine).waitFor();
		} catch (IOException e) {
			//FurbSpeech.getLogger().fatal("I/O excetion in the external MBrola application.", e);
			throw new RuntimeException(e);
		} catch (InterruptedException e) {
			//FurbSpeech.getLogger().fatal("Error while the current thread waits for the end of the mbrola applation process.", e);
			throw new RuntimeException(e);
		}
	}

	private String getPreparedFilePath(String filePath) {
		String osName = (String) System.getProperties().get("os.name");
		if (osName.toLowerCase().indexOf("windows") > -1) {
			return "\"".concat(filePath).concat("\"");
		}
		else {
			return filePath.replaceAll(" ", "\\ ");
		}
	}
	
	private String getMBrolaAppAbsolutePath() {
		String bin = null;
		String osName = (String) System.getProperties().get("os.name");
		if (osName.toLowerCase().indexOf("windows") > -1) {
			bin = "mbrola.exe";
		}
		else if (osName.toLowerCase().indexOf("linux") > -1) {
			bin = "mbrola-linux-i386";
		}
		else if (osName.toLowerCase().indexOf("mac") > -1) {
			bin = "mbrola-darwin-ppc";
		}
		
		if (bin != null) {
			URL mbrolaBin = MBrolaSynthesizer.class.getClassLoader().getResource("synthesizer/mbrola/" + bin);
			return new File(ComponentUtils.getClearDirAbsolutePath(mbrolaBin.getFile())).getAbsolutePath();
		}
		else {
			throw new OSNotSupportedException();
		}
	}
	
	public File getVoice() {
		return voiceName;
	}
}
P

desculpe, falei errado, poste o codigo desta SynthesizerFactory.java

J

Tudo bem Pedro_GTI, ja valeu só por estar tentando me ajudar

package br.furb.api.furbspeech.synth;

public class SynthesizerFactory {

	public static Synthesizer getSynthesizer() {
		// TODO: in the future, when I have more implementations of synthesizers, 
		// I'll need to check some config file to know which constructor to invoke. 
		return new MBrolaSynthesizer();
	}
	
}
P

Esse erro ocorre quando? Vc fez alterações no código, depois que postou a exceção?

J

Acrescentei esta linha em negrito, inclusive a primeira mensagem aparece,

Esse é o main do FurbSpeech.java
public static void main(String []args){

		System.out.println("Aplicativo iniciado...");
		[b]File audio = new FurbSpeech().text("Rua antônio da veiga").to().speech();[/b]
		System.out.println("Finalizando.....");

}
aqui é onde recebo este texto, incluisve coloquei esta mensagem para ver se executa e executou
public FurbSpeech text(String text) {
		this.text = text;
		System.out.println("texto pego");///LINHA ADICIONADA PRA TESTES
		return this;
}
aqui é onde comeca a parte da leitura, esta tambem executa
public FurbSpeech withVoice(File voiceFile) {
		this.voiceFile = voiceFile;
		System.out.println("voz tratada");
		return this;
}
ficando assim a mensagem de erro completa
Aplicativo iniciado...
texto pego
criado arquivo
Exception in thread "main" java.lang.ExceptionInInitializerError
	at br.furb.api.furbspeech.synth.SynthesizerFactory.getSynthesizer(SynthesizerFactory.java:26)
	at br.furb.api.furbspeech.FurbSpeech.speech(FurbSpeech.java:120)
	at br.furb.api.furbspeech.FurbSpeech.main(FurbSpeech.java:48)
Caused by: java.lang.NullPointerException
	at br.furb.api.furbspeech.synth.MBrolaSynthesizer.<clinit>(MBrolaSynthesizer.java:35)
	... 3 more
pmlm

O que está nesta linha?

at br.furb.api.furbspeech.synth.MBrolaSynthesizer.(MBrolaSynthesizer.java:35)

P

Mostre a implementacao do metodo to() dentro de FurbSpeech

J

Quando clico no sublinhado da classe

Ai me leva a esta linha do MBrolaSynthesizer.java

public static final File VOICE_BR1 = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource("synthesizer/mbrola/br1").getFile()));
J

o método to() esta assim

public FurbSpeech to() { fileName = DEFAULT_OUTPUT_FILENAME; System.out.println("criado arquivo");///LINHA ADICIONADA PRA TESTES return this; }

pmlm

Em algum lugar desta linha algo está a null

public static final File VOICE_BR1 = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource("synthesizer/mbrola/br1").getFile()));
P

Adiciona o código da classe FurbSpeech.java (Mas ja estou sentindo o cheiro do erro)

J
Nossa que bom, espero que vc me ajude a achar pq nem sei mais o que fazer nisso
package br.furb.api.furbspeech;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import br.furb.api.furbspeech.comp.Text;
import br.furb.api.furbspeech.synth.Synthesizer;
import br.furb.api.furbspeech.synth.SynthesizerFactory;
import br.furb.api.furbspeech.util.ComponentUtils;

public class FurbSpeech {
	
public static void main(String []args){

		System.out.println("Aplicativo iniciado...");
		File audio = new FurbSpeech().text("Rua antônio da veiga").to().speech();
		System.out.println("Finalizando.....");

}	
	private static final String DEFAULT_OUTPUT_FILENAME = "speech.wav";
	
	//private static Logger logger;
	private String text;
	private File voiceFile;
	private String fileName;
	
	public FurbSpeech() {
		super();
	}
	
	/**
	 * Defines the text of the operation.
	 * @param text
	 * @return
	 */
	public FurbSpeech text(String text) {
		this.text = text;
		System.out.println("texto pego");///LINHA ADICIONADA PRA TESTES
		return this;
	}
	
	/**
	 * Defines the voice to be used by sinthesizer application.
	 * @param voiceFile
	 * @return
	 */
	public FurbSpeech withVoice(File voiceFile) {
		this.voiceFile = voiceFile;
		System.out.println("voz tratada");///LINHA ADICIONADA PRA TESTES
		return this;
	}
	
	/**
	 * Defines the default output WAV filename.
	 * @return
	 */
	public FurbSpeech to() {
		fileName = DEFAULT_OUTPUT_FILENAME;
		System.out.println("criado arquivo");///LINHA ADICIONADA PRA TESTES
		return this;
	}
	
	/**
	 * Defines the output WAV filename.
	 * @param outputType
	 * @param fileName Relative to the user project binary directory.
	 * @return
	 */
	public FurbSpeech to(String fileName) {
		this.fileName = fileName.startsWith("/") ? fileName.substring(1) : fileName;
		return this;
	}
	
	/**
	 * Speech the text based on previous defined text, output file and voice.
	 */
	public File speech() {
		checkAttributes();
		
		Text text = new Text(this.text);
		text.parsePhrases(ComponentUtils.BASE_FREQUENCY, ComponentUtils.BASE_TIME);
		String output = text.showPhrases();
		
		File synthInput = writeOutputInTheFile(output);
		 
		File outputSpeechFile = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource("output").getFile() + "/" + this.fileName));
		
		Synthesizer synthesizer = SynthesizerFactory.getSynthesizer();
		if (voiceFile != null) {
			synthesizer.setVoice(voiceFile);
		}
		synthesizer.synthesize(synthInput, outputSpeechFile);
		System.out.println("Lendo?");
		return outputSpeechFile;
	}

	private File writeOutputInTheFile(String output) {
		//FurbSpeech.getLogger().debug("Creating output directory");
		File outputDir = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource(".").getFile()).concat("/output/"));
		outputDir.mkdir();
		File fileOutput = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource("output").getFile()) + "/output.pho");
		
		try {
			if (!fileOutput.exists()) {
				fileOutput.createNewFile();
				//FurbSpeech.getLogger().debug("Empty .pho file created");
			}
			FileWriter fw = new FileWriter(fileOutput);
			fw.write(output);
			fw.close();
			//FurbSpeech.getLogger().debug(".pho contents added with success");
			return fileOutput;
		}
		catch (IOException e) {
			//FurbSpeech.getLogger().error("I/O error writting .pho file", e);
			return null;
		}
	}
	
	private void checkAttributes() {
		if (this.text == null) {
			throw new IllegalStateException("The text must be previously defined. Before this invocation, call the method text(String)");
		}
	
		if (this.fileName == null) {
			throw new IllegalStateException("The output WAV file name must be previously defined. Before this invocation, call the method to() or to(String)");
		}
	}

}
J

Nao pmlm, aparentemente nada nesta linha esta recebendo null

P

Bota um break point dentro do método speech(), e acompanha o fluxo, dentro dele deve ter alguma coisa dano trela, não da pra saber olhando, debuga ai pra ver…

pmlm

Aparentemente? Ou de certeza?

J

voiceFile esta recebendo null pois nao continua apartir da linha em negrito

public File speech() {
		checkAttributes();
		
		Text text = new Text(this.text);
		text.parsePhrases(ComponentUtils.BASE_FREQUENCY, ComponentUtils.BASE_TIME);
		String output = text.showPhrases();
		
		File synthInput = writeOutputInTheFile(output);
		 
		File outputSpeechFile = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource("output").getFile() + "/" + this.fileName));
		
		[b]Synthesizer synthesizer = SynthesizerFactory.getSynthesizer();[/b]
		if (voiceFile != null) {
			synthesizer.setVoice(voiceFile);
		}
		
		synthesizer.synthesize(synthInput, outputSpeechFile);
		System.out.println("Lendo?");//adicionada para testes
		return outputSpeechFile;
}
J

Obrigada pmlm e Pedro_GTI

desculpem a gafe
o erro era por cauda disso
public static final File VOICE_BR1 = new File(ComponentUtils.getClearDirAbsolutePath(FurbSpeech.class.getClassLoader().getResource(“resources/synthesizer/mbrola/br1”).getFile()));

o caminho estava mesmo errado,
Bom o audio ainda nao funciona, o arquivo nao esta sendo criado de maneira nenhuma,
mas estou tentando aqui, quem sabe é algo que esta passando despercebido, vou ter q ver

Criado 10 de abril de 2012
Ultima resposta 10 de abr. de 2012
Respostas 18
Participantes 3