Estou tantando carregar uma imagem com o componente inputFileUpload mas esta ocorrendo um java.lang.NullPointerException quando submeto a página.
Alguém sabe porque…
vejam o código —>
<h:graphicImage value="MyBean.filePath" />
<t:inputFileUpload value="MyBean.file" storage="file" accept="/image/*" />
<h:commandButton value="Carregar imagem" action="MyBean.getImage" />
BackBean
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.apache.myfaces.custom.fileupload.UploadedFile;
public class MyBean{
private UploadedFile file;
private String filePath="";
public MyBean(){
System.out.println("Starting...");
}
public String getImage(){
try {
String myParam = null;
MessageDigest md = MessageDigest.getInstance(myParam);
InputStream in = new BufferedInputStream(
file.getInputStream());
try {
byte[] buffer = new byte[64 * 1024];
int count;
while ((count = in.read(buffer)) > 0)
md.update(buffer, 0, count);
} finally {
in.close();
}
byte hash[] = md.digest();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
int b = hash[i] & 0xFF;
int c = (b >> 4) & 0xF;
c = c < 10 ? '0' + c : 'A' + c - 10;
buf.append((char) c);
c = b & 0xF;
c = c < 10 ? '0' + c : 'A' + c - 10;
buf.append((char) c);
}
filePath = buf.toString();
System.out.println("filePath --->" + filePath);
return null;
} catch (Exception x) {
FacesMessage message = new FacesMessage( FacesMessage.SEVERITY_FATAL,x.getClass().getName(), x.getMessage());
FacesContext.getCurrentInstance().addMessage(null, message);
return null;
}
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
O código pode parece estranho mais eu peguei de um exemplo, desse código estou usandi apenas essa parte -> filePath =buf.toString();
utilizo essa parte para pegar o nome do arquivo e passar para imagem, mas parece que o arquivo não está sendo setado dai está ocorendo um java.lang.NullPointerException.
Grato a todos que puderem me ajudar…
:?: :arrow: :idea: