Help me find a bug

package com.example.test; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class LoadImage { /** подгружаем картику */ public static BufferedImage loadImage() throws IOException{ return ImageIO.read(new File ("1.bmp")); } /** Переводим в матрицу */ public static void convertToMatrix(BufferedImage image){ int w = image.getWidth(); int h = image.getHeight(); System.out.println(w); System.out.println(h); } } 
 package com.example.test; public static void main(String[] args) { try { LoadImage.convertToMatrix(LoadImage.loadImage()); } catch (Throwable e) { e.printStackTrace(); } } 

Here is the error code:

 javax.imageio.IIOException: Can't read input file! at javax.imageio.ImageIO.read(ImageIO.java:1301) at com.example.test.LoadImage.loadImage(LoadImage.java:16) at com.example.test.Test.main(Test.java:10) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) Process finished with exit code 0 

I understand it does not see the picture?

    2 answers 2

    You all wrote a compiler.

    IOException - checked exception. You must either wrap it in try/catch or append it to the throws Exception function. In this example, option 2 does not resemble, so write something like this:

     public static void main(String[] args) { try { LoadImage.convertToMatrix(LoadImage.loadImage()); } catch (Throwable e){ e.printStackTrace(); } } 
    • javax.imageio.IIOException: Can't read input file! at javax.imageio.ImageIO.read (ImageIO.java:1301) at com.example.test.LoadImage.loadImage (LoadImage.java:16) at com.example.test. Test.main (Test.java:10) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoOd islahttp invoke (Method.java:498) at com.intellij.rt.execution.application.AppMain.main (AppMain.java:147) - Dasha Novikova

    In addition to the previous answer. An IDE is not just a tool for writing code. She also teaches, and in real time, answers the questions of the level of what was set in the first edition. For example:

    enter image description here

    You can try both fixes yourself, see how they are done (the code will be generated automatically) and what they will lead to.

     javax.imageio.IIOException: Can't read input file! 

    I understand it does not see the picture?

    It’s difficult to interpret this message in a different way ...