Abob
Abob

Reputation: 760

NullpointerException when I use my own multiple method to paint an image

I am trying to paint multiple images depending on whether a Boolean is true or not. But instead when the boolean becomes true or not, I get a NullPointerException involving anything with my methods. (comment in code specifically pointing to where), and I'm 99% sure its because of the graphics and it's null.

Basically I'm also asking how to fix this and how to properly paint images using my own methods in one class. I know how to do it with each image in one class, but I have WAY more than just 2 images, I think I have almost 100, so i don't want to make 100 classes (:|). Here's my code:

List of images class:

public class Images{
   public static Toolkit tk = Toolkit.getDefaultToolkit(); 
   public static final Image Image1 = tk.getImage("src/images/image1.png"), Image2 = tk.getImage("src/images/image2.png");
   public ImageObserver observer = null; //i just did this for no reason
   public static Graphics g = Main.graphics;
   public void paintImage1(Graphics g){
      Images.g = g;
      g.drawImage(Image1, 10, 10, observer); //NullPointerException points here, even if I replace 'pbserver' with null
   }
   public void paintImage2(Graphics g){
      Images.g = g;
      g.drawImage(Image2, 10, 10, observer); //strangely, it doesn't point here
   }
}

Then, I cite and use it in my class that paints the image with my boolean like so:

public class PaintHandler{
   public static Graphics graphics = Images.g;
   public void PaintImages(boolean upheld){
      if (upheld){
         Images.paintImage1(graphics);//NullPionterException points here
      }
      else if (!upheld){
         Images.paintImage2(graphics);//doesn't point here for some reason
      }
   }
}

The exception also points to the keybindings methods I use to make upheld true or not, another thing that is strange.

Like before, I don't want to make a class for every single image, I would prefer if they were all in one class. Additionally, when I try to use getGraphics() on the JFrame I'm using, I do through this GIGANTIC loop between making thing statics and not being able to apply static terms to non-static context, such as making a variable static, but then it says can't be used in static context, but then when you don't make it static a different variable says change it back to static, you don't make that static, and you go through this huge loop between making it static and changing to not be static (sorry for long explanation, was trying to be specific).

Upvotes: 0

Views: 130

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

" when I try to use getGraphics() on the JFrame im using" - DON'T, this is not how custom is painting is done in Swing. getGraphics will, at best, return a snap shot of the last paint cycle and anything painted to it will be painted over on the next paint cycle and at worst, will return null.

Take a look at Painting in AWT and Swing and Performing Custom Painting for dore details about how painting works in Swing and how you should work with it...

Instead, create your self a custom component, extending from something like JPanel and use it's paintComponent to paint your images.

Normally, content stored in the src will be made available your application as embedded resources (this will depend on your IDE and build system), but generally speaking, you should not access any resource with the path containing src, this should be an immediate read flag for potential issues.

In this case you should be using Class#getResource to load the resource...

 public class Images{
    public static final Image Image1 = tk.getImage(Images.class.getResource("/images/image1.png"));

for example.

I would discourage you from using Toolkit.getImage as it will use a background thread to load the image and doesn't throw any kind of exception if the image wasn't loaded

Instead considering using ImageIO.read, see Reading/Loading an Image for more details

Like before, I don't want to make a class for every single image, I would prefer if they were all in one class. Additionally, when I try to use getGraphics() on the JFrame I'm using, I do through this GIGANTIC loop between making thing statics and not being able to apply static terms to non-static context, such as making a variable static, but then it says can't be used in static context, but then when you don't make it static a different variable says change it back to static, you don't make that static, and you go through this huge loop between making it static and changing to not be static (sorry for long explanation, was trying to be specific).

Your images can be stored wherever you like, maybe in a Map of some kind. But they must be painted within a valid context, ie a component's paint method. You can paint as many images you want within in single component or spread them out if your prefer, that's tp up...

The issue with static is probably because you're ...

  1. Still in the main method, which is static and/or
  2. Trying to references (non-static) variables in other classes to which you've not created an instance to.

Only a runnable example will highlight which (if not both)

Upvotes: 2

Related Questions