0

I have tried a million different things, but Java just gives me errors or straight up doesn't work when I tell it to display an image. I have to use the default "Graphics" coordinate grid because this will result in a playable game. Please tell me (ASAP - As Simply As Possible) how to get the code below working.

import java.awt.Container;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class main extends JFrame {

    static JFrame frame = new JFrame();
    static Container pane = frame.getContentPane();
    static Graphics graphics;

    static ImageIcon background = new ImageIcon("background.png");
    static JLabel backgroundLabel = new JLabel(background);

    static File titleFile = new File("title.png");
    static BufferedImage title;

    public static void main(String[] args0) {

        frame.add(backgroundLabel);
        backgroundLabel.setVisible(true);
        frame.setContentPane(backgroundLabel);

        frame.setVisible(true);
        frame.setTitle("Fastball");
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.pack();

        try{

            title = ImageIO.read(titleFile);

        } catch (IOException e) {

            System.out.println(e);

        }finally{

        };

        if(title != null) {

            graphics.drawImage(title, 15, 15, 440, 112, null);

        }
    }
}

With this code, I am getting the error Exception in thread "main" java.lang.NullPointerException at main.main(main.java:60) and I don't know exactly what it's refering to...

Phyremaster
  • 87
  • 2
  • 8
  • can you point us to line 60 in your code? – Kick Buttowski Oct 13 '15 at 22:41
  • **It seems you have overused static keyword in your code?!!!!** – Kick Buttowski Oct 13 '15 at 22:42
  • 1
    The obvious thing is `Graphics` will be `null`, but you should not be using `Graphics` in this way, instead, take a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for how custom painting should be done in Swing – MadProgrammer Oct 13 '15 at 22:46
  • @MadProgrammer fast and sharp as always ;) – Kick Buttowski Oct 13 '15 at 22:47

0 Answers0