ImageIO.read returns NULL, with no errors












16














The following code seems not to work, even though the file appears to be found just fine.



    images = new BufferedImage[32];
FileInputStream fis = null;
for (int i = 0; i < 32; i++) {
File file = new File("tiles\"+i+".bmp");
if (!file.exists()){
System.out.println("File "+i+" failed");
}
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.err.println(e + "" + i);
}
try {
images[i] = ImageIO.read(fis);
} catch (IOException e) {
System.err.println(e + "" + i);
}
if (images[i] == null) {
System.out.println("Image "+i+" failed");
}
}


Thanks in advance for any help.



Edit: The result is me attempting to Graphics.drawImage(images[0]);, and it giving me a null pointer exception. This code here completes fine.



Edit: Changed moved the if(!file.exists()) as suggested, and wrapped the file in an input stream.










share|improve this question




















  • 3




    RTFM
    – mre
    Mar 30 '12 at 13:34










  • Could you describe the result? A wild guess: the path in incorrect...
    – Alexis Dufrenoy
    Mar 30 '12 at 13:36










  • Why don'y you put your test if (!file.exists()) System.out.println("File "+i+" failed"); before the read?
    – Alexis Dufrenoy
    Mar 30 '12 at 13:38
















16














The following code seems not to work, even though the file appears to be found just fine.



    images = new BufferedImage[32];
FileInputStream fis = null;
for (int i = 0; i < 32; i++) {
File file = new File("tiles\"+i+".bmp");
if (!file.exists()){
System.out.println("File "+i+" failed");
}
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.err.println(e + "" + i);
}
try {
images[i] = ImageIO.read(fis);
} catch (IOException e) {
System.err.println(e + "" + i);
}
if (images[i] == null) {
System.out.println("Image "+i+" failed");
}
}


Thanks in advance for any help.



Edit: The result is me attempting to Graphics.drawImage(images[0]);, and it giving me a null pointer exception. This code here completes fine.



Edit: Changed moved the if(!file.exists()) as suggested, and wrapped the file in an input stream.










share|improve this question




















  • 3




    RTFM
    – mre
    Mar 30 '12 at 13:34










  • Could you describe the result? A wild guess: the path in incorrect...
    – Alexis Dufrenoy
    Mar 30 '12 at 13:36










  • Why don'y you put your test if (!file.exists()) System.out.println("File "+i+" failed"); before the read?
    – Alexis Dufrenoy
    Mar 30 '12 at 13:38














16












16








16


4





The following code seems not to work, even though the file appears to be found just fine.



    images = new BufferedImage[32];
FileInputStream fis = null;
for (int i = 0; i < 32; i++) {
File file = new File("tiles\"+i+".bmp");
if (!file.exists()){
System.out.println("File "+i+" failed");
}
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.err.println(e + "" + i);
}
try {
images[i] = ImageIO.read(fis);
} catch (IOException e) {
System.err.println(e + "" + i);
}
if (images[i] == null) {
System.out.println("Image "+i+" failed");
}
}


Thanks in advance for any help.



Edit: The result is me attempting to Graphics.drawImage(images[0]);, and it giving me a null pointer exception. This code here completes fine.



Edit: Changed moved the if(!file.exists()) as suggested, and wrapped the file in an input stream.










share|improve this question















The following code seems not to work, even though the file appears to be found just fine.



    images = new BufferedImage[32];
FileInputStream fis = null;
for (int i = 0; i < 32; i++) {
File file = new File("tiles\"+i+".bmp");
if (!file.exists()){
System.out.println("File "+i+" failed");
}
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.err.println(e + "" + i);
}
try {
images[i] = ImageIO.read(fis);
} catch (IOException e) {
System.err.println(e + "" + i);
}
if (images[i] == null) {
System.out.println("Image "+i+" failed");
}
}


Thanks in advance for any help.



Edit: The result is me attempting to Graphics.drawImage(images[0]);, and it giving me a null pointer exception. This code here completes fine.



Edit: Changed moved the if(!file.exists()) as suggested, and wrapped the file in an input stream.







java image






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 23 '13 at 10:41









d.raev

5,60163866




5,60163866










asked Mar 30 '12 at 13:29









Naberius

100117




100117








  • 3




    RTFM
    – mre
    Mar 30 '12 at 13:34










  • Could you describe the result? A wild guess: the path in incorrect...
    – Alexis Dufrenoy
    Mar 30 '12 at 13:36










  • Why don'y you put your test if (!file.exists()) System.out.println("File "+i+" failed"); before the read?
    – Alexis Dufrenoy
    Mar 30 '12 at 13:38














  • 3




    RTFM
    – mre
    Mar 30 '12 at 13:34










  • Could you describe the result? A wild guess: the path in incorrect...
    – Alexis Dufrenoy
    Mar 30 '12 at 13:36










  • Why don'y you put your test if (!file.exists()) System.out.println("File "+i+" failed"); before the read?
    – Alexis Dufrenoy
    Mar 30 '12 at 13:38








3




3




RTFM
– mre
Mar 30 '12 at 13:34




RTFM
– mre
Mar 30 '12 at 13:34












Could you describe the result? A wild guess: the path in incorrect...
– Alexis Dufrenoy
Mar 30 '12 at 13:36




Could you describe the result? A wild guess: the path in incorrect...
– Alexis Dufrenoy
Mar 30 '12 at 13:36












Why don'y you put your test if (!file.exists()) System.out.println("File "+i+" failed"); before the read?
– Alexis Dufrenoy
Mar 30 '12 at 13:38




Why don'y you put your test if (!file.exists()) System.out.println("File "+i+" failed"); before the read?
– Alexis Dufrenoy
Mar 30 '12 at 13:38












3 Answers
3






active

oldest

votes


















8














ImageIO.read(file); will return null if no registered ImageReader is not found. Please check whether you have registered any ImageReader or not.



I think this code snippet could help you



File file = new File("bear.jpg"); // I have bear.jpg in my working directory  
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis); //reading the image file


You just need to wrap the file into an FileInputStream and then pass it to read()






share|improve this answer





















  • I'm probably still not doing this properly, but what I did do was unsuccessful. I changed the code above to show what was changed.
    – Naberius
    Mar 30 '12 at 13:59










  • Can you say what are your image file names in the disk?
    – Chandra Sekhar
    Mar 30 '12 at 14:07










  • @Naberius Can you say what are your image file names in the disk? It is perfectly working in my case.
    – Chandra Sekhar
    Mar 30 '12 at 14:16










  • Sorry, no weekend internet access. The files are named 0.bmp, 1.bmp, up to 31. They're also stored in a folder called "tiles", but nothing changes by taking them out of said folder.
    – Naberius
    Apr 2 '12 at 13:12












  • @Naberius Then try using File file = new File("tiles"+i+".bmp"); this.
    – Chandra Sekhar
    Apr 2 '12 at 13:20





















31














ImageIO.read(*...) will only load these image types GIF, PNG, JPEG, BMP, and WBMP.



Any other image type will return null without error.



reference:
http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html



I do realize this is not a solution to the specific original problem but it is a solution to the question asked.






share|improve this answer





























    0














    Try wrap you InputStream into BufferedInputStream:



    fis = new FileInputStream(file); ==>
    new BufferedInputStream(new FileInputStream(file));






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9943962%2fimageio-read-returns-null-with-no-errors%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      8














      ImageIO.read(file); will return null if no registered ImageReader is not found. Please check whether you have registered any ImageReader or not.



      I think this code snippet could help you



      File file = new File("bear.jpg"); // I have bear.jpg in my working directory  
      FileInputStream fis = new FileInputStream(file);
      BufferedImage image = ImageIO.read(fis); //reading the image file


      You just need to wrap the file into an FileInputStream and then pass it to read()






      share|improve this answer





















      • I'm probably still not doing this properly, but what I did do was unsuccessful. I changed the code above to show what was changed.
        – Naberius
        Mar 30 '12 at 13:59










      • Can you say what are your image file names in the disk?
        – Chandra Sekhar
        Mar 30 '12 at 14:07










      • @Naberius Can you say what are your image file names in the disk? It is perfectly working in my case.
        – Chandra Sekhar
        Mar 30 '12 at 14:16










      • Sorry, no weekend internet access. The files are named 0.bmp, 1.bmp, up to 31. They're also stored in a folder called "tiles", but nothing changes by taking them out of said folder.
        – Naberius
        Apr 2 '12 at 13:12












      • @Naberius Then try using File file = new File("tiles"+i+".bmp"); this.
        – Chandra Sekhar
        Apr 2 '12 at 13:20


















      8














      ImageIO.read(file); will return null if no registered ImageReader is not found. Please check whether you have registered any ImageReader or not.



      I think this code snippet could help you



      File file = new File("bear.jpg"); // I have bear.jpg in my working directory  
      FileInputStream fis = new FileInputStream(file);
      BufferedImage image = ImageIO.read(fis); //reading the image file


      You just need to wrap the file into an FileInputStream and then pass it to read()






      share|improve this answer





















      • I'm probably still not doing this properly, but what I did do was unsuccessful. I changed the code above to show what was changed.
        – Naberius
        Mar 30 '12 at 13:59










      • Can you say what are your image file names in the disk?
        – Chandra Sekhar
        Mar 30 '12 at 14:07










      • @Naberius Can you say what are your image file names in the disk? It is perfectly working in my case.
        – Chandra Sekhar
        Mar 30 '12 at 14:16










      • Sorry, no weekend internet access. The files are named 0.bmp, 1.bmp, up to 31. They're also stored in a folder called "tiles", but nothing changes by taking them out of said folder.
        – Naberius
        Apr 2 '12 at 13:12












      • @Naberius Then try using File file = new File("tiles"+i+".bmp"); this.
        – Chandra Sekhar
        Apr 2 '12 at 13:20
















      8












      8








      8






      ImageIO.read(file); will return null if no registered ImageReader is not found. Please check whether you have registered any ImageReader or not.



      I think this code snippet could help you



      File file = new File("bear.jpg"); // I have bear.jpg in my working directory  
      FileInputStream fis = new FileInputStream(file);
      BufferedImage image = ImageIO.read(fis); //reading the image file


      You just need to wrap the file into an FileInputStream and then pass it to read()






      share|improve this answer












      ImageIO.read(file); will return null if no registered ImageReader is not found. Please check whether you have registered any ImageReader or not.



      I think this code snippet could help you



      File file = new File("bear.jpg"); // I have bear.jpg in my working directory  
      FileInputStream fis = new FileInputStream(file);
      BufferedImage image = ImageIO.read(fis); //reading the image file


      You just need to wrap the file into an FileInputStream and then pass it to read()







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 30 '12 at 13:34









      Chandra Sekhar

      12k125692




      12k125692












      • I'm probably still not doing this properly, but what I did do was unsuccessful. I changed the code above to show what was changed.
        – Naberius
        Mar 30 '12 at 13:59










      • Can you say what are your image file names in the disk?
        – Chandra Sekhar
        Mar 30 '12 at 14:07










      • @Naberius Can you say what are your image file names in the disk? It is perfectly working in my case.
        – Chandra Sekhar
        Mar 30 '12 at 14:16










      • Sorry, no weekend internet access. The files are named 0.bmp, 1.bmp, up to 31. They're also stored in a folder called "tiles", but nothing changes by taking them out of said folder.
        – Naberius
        Apr 2 '12 at 13:12












      • @Naberius Then try using File file = new File("tiles"+i+".bmp"); this.
        – Chandra Sekhar
        Apr 2 '12 at 13:20




















      • I'm probably still not doing this properly, but what I did do was unsuccessful. I changed the code above to show what was changed.
        – Naberius
        Mar 30 '12 at 13:59










      • Can you say what are your image file names in the disk?
        – Chandra Sekhar
        Mar 30 '12 at 14:07










      • @Naberius Can you say what are your image file names in the disk? It is perfectly working in my case.
        – Chandra Sekhar
        Mar 30 '12 at 14:16










      • Sorry, no weekend internet access. The files are named 0.bmp, 1.bmp, up to 31. They're also stored in a folder called "tiles", but nothing changes by taking them out of said folder.
        – Naberius
        Apr 2 '12 at 13:12












      • @Naberius Then try using File file = new File("tiles"+i+".bmp"); this.
        – Chandra Sekhar
        Apr 2 '12 at 13:20


















      I'm probably still not doing this properly, but what I did do was unsuccessful. I changed the code above to show what was changed.
      – Naberius
      Mar 30 '12 at 13:59




      I'm probably still not doing this properly, but what I did do was unsuccessful. I changed the code above to show what was changed.
      – Naberius
      Mar 30 '12 at 13:59












      Can you say what are your image file names in the disk?
      – Chandra Sekhar
      Mar 30 '12 at 14:07




      Can you say what are your image file names in the disk?
      – Chandra Sekhar
      Mar 30 '12 at 14:07












      @Naberius Can you say what are your image file names in the disk? It is perfectly working in my case.
      – Chandra Sekhar
      Mar 30 '12 at 14:16




      @Naberius Can you say what are your image file names in the disk? It is perfectly working in my case.
      – Chandra Sekhar
      Mar 30 '12 at 14:16












      Sorry, no weekend internet access. The files are named 0.bmp, 1.bmp, up to 31. They're also stored in a folder called "tiles", but nothing changes by taking them out of said folder.
      – Naberius
      Apr 2 '12 at 13:12






      Sorry, no weekend internet access. The files are named 0.bmp, 1.bmp, up to 31. They're also stored in a folder called "tiles", but nothing changes by taking them out of said folder.
      – Naberius
      Apr 2 '12 at 13:12














      @Naberius Then try using File file = new File("tiles"+i+".bmp"); this.
      – Chandra Sekhar
      Apr 2 '12 at 13:20






      @Naberius Then try using File file = new File("tiles"+i+".bmp"); this.
      – Chandra Sekhar
      Apr 2 '12 at 13:20















      31














      ImageIO.read(*...) will only load these image types GIF, PNG, JPEG, BMP, and WBMP.



      Any other image type will return null without error.



      reference:
      http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html



      I do realize this is not a solution to the specific original problem but it is a solution to the question asked.






      share|improve this answer


























        31














        ImageIO.read(*...) will only load these image types GIF, PNG, JPEG, BMP, and WBMP.



        Any other image type will return null without error.



        reference:
        http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html



        I do realize this is not a solution to the specific original problem but it is a solution to the question asked.






        share|improve this answer
























          31












          31








          31






          ImageIO.read(*...) will only load these image types GIF, PNG, JPEG, BMP, and WBMP.



          Any other image type will return null without error.



          reference:
          http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html



          I do realize this is not a solution to the specific original problem but it is a solution to the question asked.






          share|improve this answer












          ImageIO.read(*...) will only load these image types GIF, PNG, JPEG, BMP, and WBMP.



          Any other image type will return null without error.



          reference:
          http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html



          I do realize this is not a solution to the specific original problem but it is a solution to the question asked.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 31 '13 at 4:18









          Irrationalkilla

          41345




          41345























              0














              Try wrap you InputStream into BufferedInputStream:



              fis = new FileInputStream(file); ==>
              new BufferedInputStream(new FileInputStream(file));






              share|improve this answer


























                0














                Try wrap you InputStream into BufferedInputStream:



                fis = new FileInputStream(file); ==>
                new BufferedInputStream(new FileInputStream(file));






                share|improve this answer
























                  0












                  0








                  0






                  Try wrap you InputStream into BufferedInputStream:



                  fis = new FileInputStream(file); ==>
                  new BufferedInputStream(new FileInputStream(file));






                  share|improve this answer












                  Try wrap you InputStream into BufferedInputStream:



                  fis = new FileInputStream(file); ==>
                  new BufferedInputStream(new FileInputStream(file));







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 14 '14 at 13:40









                  Ahmed MANSOUR

                  1,18111425




                  1,18111425






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9943962%2fimageio-read-returns-null-with-no-errors%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith