I'm developing a system to perform maintenance of machines, I need to send some photos. When the user submits the photos without close or exit the activity, sending is done successfully. When the user leaves the activity, returning the photos are not sent, the following log:
java.lang.NullPointerException
Rejecting registerization due to +iget-object-quick v7, v9, (#8)
at java.io.File.fixSlashes(File.java:185)
at java.io.File.<init>(File.java:134)
at java.io.FileInputStream.<init>(FileInputStream.java:105)
at br.com.tetsistemas.rcbmanutencoes01.ManutencaoConclusaoActivity.envioFTP(ManutencaoConclusaoActivity.java:600)
at br.com.tetsistemas.rcbmanutencoes01.ManutencaoConclusaoActivity.run(ManutencaoConclusaoActivity.java:549)
at java.lang.Thread.run(Thread.java:856)
Follows the code of the activity:
// Script to send one of the photos
helper.getFoto_plataforma2().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nomeArquivoPlataforma2 = "Foto_Plataforma2_" + hora_final + ".jpg";
localPlataforma2 = STORAGE_SERVICE + "/extSdCard/"
+ "/RCBManutencoes/" + nomeArquivoPlataforma2 + ".jpg";
File arquivo = new File(localPlataforma2);
//URI que informa onde o arquivo resultado deve ser salvo
Uri localFoto = Uri.fromFile(arquivo);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, localFoto);
startActivityForResult(intent, TAKE_PLATAFORMA2);
}
});
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Verificacao do resultado da nossa requisicao
if (requestCode == TAKE_PLATAFORMA) {
if (resultCode == Activity.RESULT_OK) {
helper.carregarFotoPlataforma(this.localPlataforma);
} else {
localPlataforma = null;
}
}
@Override
public void run() {
String diretorioPlataforma = localPlataforma;
String diretorioPlataforma2 = localPlataforma2;
String diretorioHorimetro = localHorimetro;
String diretorioProblema = localProblema;
String diretorioProblema2 = localProblema2;
String diretorioCausa = localPlataforma;
String diretorioCausa2 = localPlataforma2;
String nomeArqPlataforma = nomeArquivoPlataforma;
String nomeArqPlataforma2 = nomeArquivoPlataforma2;
String nomeArqHorimetro = nomeArquivoHorimetro;
String nomeArqProblema = nomeArquivoProblema;
String nomeArqProblema2 = nomeArquivoProblema2;
String nomeArqCausa = nomeArquivoCausa;
String nomeArqCausa2 = nomeArquivoCausa2;
final TextView notificacao = (TextView) findViewById(R.id.notificacao);
try {
envioFTP("login", "senha", diretorioPlataforma, diretorioPlataforma2, diretorioHorimetro,
diretorioProblema, diretorioProblema2, diretorioCausa, diretorioCausa2,
nomeArqPlataforma, nomeArqPlataforma2, nomeArqHorimetro, nomeArqProblema, nomeArqProblema2, nomeArqCausa, nomeArqCausa2);
handler.post(new Runnable() {
public void run() {
notificacao.setText("Arquivo enviado com sucesso");
notificacao.setVisibility(View.VISIBLE);
}
});
} finally {
dialog.dismiss();
}
}
private void envioFTP(String login, String senha, String diretorioPlataforma, String diretorioPlataforma2,
String diretorioHorimetro, String diretorioProblema, String diretorioProblema2, String diretorioCausa, String diretorioCausa2,
String nomeArqPlataforma, String nomeArqPlataforma2, String nomeArqHorimetro, String nomeArqProblema, String nomeArqProblema2,
String nomeArqCausa, String nomeArqCausa2) {
FTPClient ftp = new FTPClient();
try {
ftp.connect("url do ftp", 21);
ftp.login(login, senha);
ftp.changeWorkingDirectory("MANUTENCOES");
FileInputStream arqEnviarPlataforma = new FileInputStream(diretorioPlataforma);
FileInputStream arqEnviarPlataforma2 = new FileInputStream(diretorioPlataforma2);
FileInputStream arqEnviarHorimetro = new FileInputStream(diretorioHorimetro);
FileInputStream arqEnviarProblema = new FileInputStream(diretorioProblema);
FileInputStream arqEnviarProblema2 = new FileInputStream(diretorioProblema2);
FileInputStream arqEnviarCausa = new FileInputStream(diretorioCausa);
FileInputStream arqEnviarCausa2 = new FileInputStream(diretorioCausa2);
ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.storeFile(nomeArqPlataforma, arqEnviarPlataforma);
ftp.storeFile(nomeArqPlataforma2, arqEnviarPlataforma2);
ftp.storeFile(nomeArqHorimetro, arqEnviarHorimetro);
ftp.storeFile(nomeArqProblema, arqEnviarProblema);
ftp.storeFile(nomeArqProblema2, arqEnviarProblema2);
ftp.storeFile(nomeArqCausa, arqEnviarCausa);
ftp.storeFile(nomeArqCausa2, arqEnviarCausa2);
ftp.logout();
ftp.disconnect();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
You should specify a condition if the user sends directly to the server then run this code:
FileInputStream arqEnviarPlataforma = new FileInputStream(diretorioPlataforma);
FileInputStream arqEnviarPlataforma2 = new FileInputStream(diretorioPlataforma2);
FileInputStream arqEnviarHorimetro = new FileInputStream(diretorioHorimetro);
FileInputStream arqEnviarProblema = new FileInputStream(diretorioProblema);
FileInputStream arqEnviarProblema2 = new FileInputStream(diretorioProblema2);
FileInputStream arqEnviarCausa = new FileInputStream(diretorioCausa);
FileInputStream arqEnviarCausa2 = new FileInputStream(diretorioCausa2);
Else if the user saves locally and decides to send later you should specify some condition!! In your code the above lines get called for both conditions, thats why you get null pointer exception, you should specify a separate logic for sending the information later which you saved locally.
PS: I've noticed you haven't closed the InputStreams !!
you should close all the InputStream objects in a finally block
finally{
arqEnviarHorimetro.close();
// close all others
}
Without seeing your whole code this is just a wild assumption, since you're getting NullPointerException in your second case.