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).