Eu já estou há bastente tempo tentando achar qual é o problema disso pois ele deve substituir um parametro por outro, eu estou tentando fazer um verificador de imagem (Parecido com o que eu fiz com o som em um outro topico) caso se o inputstream da imagem for nulo ou seja não estiver encontrado nenhum arquivo ele o substituirá por outro ou seja ele substitui um parametro, aqui está mais detalhes do erro:
Exception in thread “main” java.lang.NullPointerException
at PacoteJogo.ImageLoader.copy(ImageLoader.java:68)
at PacoteJogo.ImageLoader.readBytes(ImageLoader.java:61)
at PacoteJogo.Jogo.Tela(Jogo.java:146)
at PacoteJogo.Main.main(Main.java:22)
ou seja ele dá nulo mesmo substituindo o parametro:
Parametro errado sendo entregado para o imageVerifier que eu fiz:
InputStream inp2 = imageVerifier.verifyImage("/PacoteJogo/Sprites/Botã.png");
O parametro certo é esse:
InputStream inp2 = imageVerifier.verifyImage("/PacoteJogo/Sprites/Botão.png");
o image verifier retorna um inputstream caso a imagem esteja errada ele substitui pela imagem : notexturefound.png.
Aqui está o codigo do Imageverifier:
public class ImageVerifier {
private InputStream b;
private String d;
public InputStream verifyImage(String input) throws IOException, URISyntaxException {
String input2;
String input3;
InputStream c;
InputStream teste = this.getClass().getResourceAsStream(input);
System.out.println(input + " input agora");
//this.d = "/Pacotejogo/Sprites/notexturefound.png";
if(teste == null){
//System.out.println(this.d + " d");
input2 = "/Pacotejogo/Sprites/notexturefound.png";
c = this.getClass().getResourceAsStream(input2);
System.out.println(teste + " 'teste' resultado");
System.out.println(c + " 'c' antes");
System.out.println("Console:(ImageLoader)[ERROR(404)]: Image or icon not found");
System.out.println("Console: Image changed for notexturefound.png");
System.out.println("Console:(ImageLoader)[ERROR(404)]: Imagem ou icone não encontrado");
System.out.println("Console: Imagem substituída por notexturefound.png");
//System.out.println(input + " input");
System.out.println(c + " teste");
}else{
c = this.getClass().getResourceAsStream(input);
System.out.println(c + " 'c' agora else");
}
return c;
}
}
Então o problema é o nulo e até agora não consegui resolver o problema
pmlm
Abril 7, 2020, 8:33am
#2
O erro é na linha 68 do ImageLoader mas tu postaste o ImageVerifier. Assim não dá para adivinhar a causa do erro embora possa imaginar que estas a tentar copiar a imagem que não existe.
Desculpe a demora eu fiquei sem acesso ao me pc ontem mas e isso mesmo tem um metodo copy, aqui está o codigo fonte do imageLoader:
public class ImageLoader {
private ImageIcon Image;
private URL url;
private byte[] bar;
private InputStream inp;
private byte[] readbytes;
private byte[] readbytes2;
private boolean vouf;
private InputStream a = this.getClass().getResourceAsStream("/Pacotejogo/Sprites/notexturefound.png");
public void ImageIconSetter(String Location, ImageIcon img) throws IOException{
System.out.println(Location + " Location");
System.out.println(img + " img");
this.inp = this.getClass().getResourceAsStream(Location);
System.out.println(this.inp + " inp");
File file = stream2file(this.inp);
System.out.println(file + " File");
this.url = file.toURI().toURL();
System.out.println(this.url + " URL");
this.Image = new ImageIcon(url);
System.out.println(this.Image + " Image");
img = Image;
}
public byte[] inicilizador(InputStream input) throws IOException{
this.readbytes = readBytes(input);
return this.readbytes;
}
public File stream2file (InputStream in) throws IOException {
final File tempFile = File.createTempFile("Image", ".png");
tempFile.deleteOnExit();
//System.out.println(tempFile + " a");
try (FileOutputStream out = new FileOutputStream(tempFile.getPath())) {
IOUtils.copy(in, out);
//System.out.println(copy + " b");
}
return tempFile;
}
public byte[] readBytes(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(input, output);
//System.out.println(input + "<< Input || Output >>" + output);
return output.toByteArray();
}
public void copy(InputStream source, OutputStream target) throws IOException {
byte[] buffer = new byte[8192];
for (int read = -1; (read = source.read(buffer)) != -1; target.write(buffer, 0, read)) { /* corpo deste for é vazio mesmo */ }
target.flush();
}
public boolean verifyimage(InputStream input) throws IOException{
System.out.println(input + " input veri");
System.out.println(a + " a agora");
if(input == null){
input = null;
InputStream b = this.getClass().getResourceAsStream("/Pacotejogo/Sprites/notexturefound.png");
//input = a;
//input = b = this.getClass().getResourceAsStream("/Pacotejogo/Sprites/notexturefound.png");
//a = input;
System.out.print(input = a);
System.out.println(" input = a");
this.vouf = true;
System.out.println("Console:(ImageLoader)[ERROR(404)]: Image or icon not found");
System.out.println("Console: Image changed for notexturefound.png");
System.out.println("Console:(ImageLoader)[ERROR(404)]: Imagem ou icone não encontrado");
System.out.println("Console: Imagem substituída por notexturefound.png");
byte[] ba = readBytes2(b);
System.out.println(ba);
this.readbytes = null;
this.readbytes = ba;
}else{
this.vouf = false;
System.out.println(a + " a");
}
return this.vouf;
}
public byte[] readBytes2(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(input, output);
//System.out.println(input + "<< Input || Output >>" + output);
return output.toByteArray();
}
public ImageIcon getImage() {
return Image;
}
public void setImage(ImageIcon Image) {
this.Image = Image;
}
public URL getUrl() {
return url;
}
public void setUrl(URL url) {
this.url = url;
}
public byte[] getBar() {
return bar;
}
public void setBar(byte[] bar) {
this.bar = bar;
}
public InputStream getInp() {
return inp;
}
public void setInp(InputStream inp) {
this.inp = inp;
}
}
pmlm
Abril 8, 2020, 9:46pm
#4
Vitor_Santana:
public void copy(InputStream source, OutputStream target) throws IOException {
byte[] buffer = new byte[8192];
for (int read = -1; (read = source.read(buffer)) != -1; target.write(buffer, 0, read)) {
/* corpo deste for é vazio mesmo */
}
target.flush();
}
Algures aí está o teu NullPointer. Tens de perceber de onde vem o NullPointer
o null vem do for ta mas porque está dando null? esse é o problema tem alguma coisa que ta dando null e eu não sei o que é
ah deixa era só um erro de digitação que eu não percebi kskskssksk fiqeui 3 dias tentando procurar