Jump to content

BufferedImage returning null


Recommended Posts

Posted

For some reason the BufferedImage that I want to paint is constantly returning null even through it's not throwing any exceptions when I read it.

public class SpillsFisher extends Script {

   private BufferedImage background;

   public final void onStart() throws InterruptedException {

          try {
              background = ImageIO.read(getScriptResourceAsStream("resources/background.png"));
              log("No exceptions");
          } catch (IOException e) {
              log(e.fillInStackTrace());
          }
      }

   public void onPaint(Graphics2D g) {

          if(background != null){
              g.drawImage(background, null, 10, 10);
              log("Not null");
          } else {
              log("is null"); <-- it's always returning null here
          }
	}

Structure within IDE

F1UI8Zz.png

Structure within 7zip (It has the resources folder and the image inside)

GUjOzmt.png

I was using this post as a point of reference:

I've also tried:

background = ImageIO.read(SpillsFisher.class.getResourceAsStream("/resources/background.png"));

From

Having the same issue either way, any ideas?

Posted

What I do is fetch the image from an url like imgur :)
Save it in the Osbot/data folder and load it in next time, if it's not there download it again.
 

    public static BufferedImage getImage(Script script, String url) {
        try {
            return ImageIO.read(new URL(url));
        } catch (IOException e) {
            script.log("Failed to load image with url: " + url);
        }
        return null;
    }

 

  • Like 1
Posted
3 hours ago, Khaleesi said:

What I do is fetch the image from an url like imgur :)
Save it in the Osbot/data folder and load it in next time, if it's not there download it again.
 

    public static BufferedImage getImage(Script script, String url) {
        try {
            return ImageIO.read(new URL(url));
        } catch (IOException e) {
            script.log("Failed to load image with url: " + url);
        }
        return null;
    }

 

Ahah! I appreciate the response, funnily enough I was thinking about doing it this way, thank you!

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...