1

I'm learning about double buffering and the book I got from my teacher tells me to write what I have written. But when java tries to get the graphics object from the image it crashes.

frame = Image
engine = Graphics

Error

Exception in thread "main" java.lang.NullPointerException
at Engine.<init>(Engine.java:30)
at Game.<init>(Game.java:35)
at Game.main(Game.java:22)

Image 1

Image 2

Game.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;
import java.util.Timer;

public class Game implements ActionListener {

private static Game hosmos2;
private JFrame frmMain;
private AntiCheat holyanticheat;
private Dimension screen;
private Engine holyengine;
private int sx, sy;
private Timer timCheat;

public static void main(String[] args) throws FileNotFoundException, InterruptedException {

    hosmos2 = new Game();
}

private Game() throws FileNotFoundException, InterruptedException {

    frmMain = new JFrame("Hosmos 2");
    frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmMain.setSize(1366, 768);
    screen = Toolkit.getDefaultToolkit().getScreenSize();
    sx = (int) ((screen.getWidth() - frmMain.getWidth()) / 2);
    sy = (int) ((screen.getHeight() - frmMain.getHeight()) / 2);
    frmMain.setLocation(sx, sy);
    holyanticheat = new AntiCheat();
    holyengine = new Engine();
    timCheat = new Timer();
    frmMain.setVisible(true);
    timCheat.schedule(holyanticheat, 500);
    Thread.sleep(1000);
    timCheat.cancel();
}

public void actionPerformed(ActionEvent e) {


}

}

Engine.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.*;

public class Engine extends JPanel {

private boolean countdown, quality;
private Font text, text2;
private Graphics engine;
private Image frame;
private ImageIcon background, button1, button21, button22, button3, button4, button51, button52, header;
private int weedbackground, fps, fps2;
public Rectangle box1, box2, box3, box4, box5;

public static void main(String[] args) {


}

public Engine() {

    this.setBackground(Color.black);
    frame = createImage(getSize().width, getSize().height);
    text = new Font("Microsoft Sans Serif", Font.BOLD, 24);
    text2 = new Font("Microsoft Sans Serif", Font.BOLD, 8);
    engine = frame.getGraphics(); // Error
    background = new ImageIcon("textures/background.png");
    //background = background.GetThumbnailImage(source.ClientSize.Width, source.ClientSize.Height, null, IntPtr.Zero);
    button1 = new ImageIcon("textures/button 1.png");
    button21 = new ImageIcon("textures/button 2-1.png");
    button22 = new ImageIcon("textures/button 2-2.png");
    button3 = new ImageIcon("textures/button 3.png");
    button4 = new ImageIcon("textures/button 4.png");
    button51 = new ImageIcon("textures/button 5-1.png");
    button52 = new ImageIcon("textures/button 5-2.png");
    header = new ImageIcon("textures/header.png");
    box1 = new Rectangle(20, 20, 87, 37);
    box2 = new Rectangle(20, 60, 96, 37);
    box3 = new Rectangle(20, 103, 182, 43);
    box4 = new Rectangle(20, 146, 83, 43);
    box5 = new Rectangle(20, 189, 206, 43);
}

public Rectangle GetBox1() {

    return box1;
}

public Rectangle GetBox2() {

    return box2;
}

public Rectangle GetBox3() {

    return box3;
}

public Rectangle GetBox4() {

    return box4;
}

public Rectangle GetBox5() {

    return box5;
}

public void FPS()
{
    fps2 = fps;
    fps = 0;
}

public void Quality(boolean quality2)
{
    quality = quality2;
}

public void Render(boolean music, boolean effect, int time, int spawned, int points, int cracktime, int crackend, int shieldtime, int shieldend, int weedtime, int weedend, ImageIcon malcolm, int malcolmX, int malcolmY, int item, ImageIcon crack, int crackX, int crackY, ImageIcon nuke, int nukeX, int nukeY, ImageIcon shield, int shieldX, int shieldY, ImageIcon skull, int skullX, int skullY, ImageIcon trollface, int trollfaceX, int trollfaceY, ImageIcon weed, int weedX, int weedY, LinkedList<Hosmin> hosmins) {

    engine = frame.getGraphics();
    super.paintComponent(engine);
    if (effect == false) {

        if (quality == false) {

            background.paintIcon(this, engine, 0, 0);
        }
        else {

            this.setBackground(Color.black);
        }
    }
    else {

        if (weedbackground == 1) {

            this.setBackground(Color.green);
            weedbackground++;
        }
        else if (weedbackground == 2) {

            this.setBackground(Color.yellow);
            weedbackground++;
        }
        else if (weedbackground == 3) {

            this.setBackground(Color.red);
            weedbackground = 1;
        }
    }
    header.paintIcon(this, engine, 547, 10);
    button1.paintIcon(this, engine, 20, 20);
    if (music == false) {

        button21.paintIcon(this, engine, 20, 60);
    }
    else {

        button22.paintIcon(this, engine, 20, 60);
    }
    button3.paintIcon(this, engine, 20, 103);
    button4.paintIcon(this, engine, 20, 146);
    if (quality == false) {

        button51.paintIcon(this, engine, 20, 189);
    }
    else {

        button52.paintIcon(this, engine, 20, 189);
    }
    engine.setFont(text2);
    engine.setColor(Color.green);
    engine.drawString("FPS: " + fps2, 1293, 9);
    engine.setFont(text);
    engine.setColor(Color.red);
    engine.drawString("Time: " + time, 1178, 22);
    engine.drawString("Hosmins: " + spawned, 1124, 59);
    engine.drawString("Points: " + points, 1160, 96);
    engine.setFont(text2);
    engine.setColor(Color.green);
    engine.drawString("Developer: HolyDuFF", 1229, 708);
    malcolm.paintIcon(this, engine, malcolmX, malcolmY);
    if (countdown == true) {

        if (crackend - cracktime == 0) {

            countdown = false;
        }
        else if (cracktime >= 0) {

            engine.setFont(text);
            engine.setColor(Color.red);
            crackend = crackend - cracktime;
            if (crackend != 10) {

                engine.drawString("" + crackend, malcolmX + 17, malcolmY - 40);
            }
            else {

                engine.drawString("" + crackend, malcolmX + 7, malcolmY - 40);
            }
        }
        else if (shieldtime >= 0) {

            engine.setFont(text);
            engine.setColor(Color.red);
            shieldend = shieldend - shieldtime;
            if (shieldend != 10) {

                engine.drawString("" + shieldend, malcolmX + 17, malcolmY - 40);
            }
            else {

                engine.drawString("" + shieldend, malcolmX + 7, malcolmY - 40);
            }
        }
        else if (weedtime >= 0) {

            engine.setFont(text);
            engine.setColor(Color.blue);
            weedend = weedend - weedtime;
            if (weedend != 10) {

                engine.drawString("" + weedend, malcolmX + 17, malcolmY - 40);
            }
            else {

                engine.drawString("" + weedend, malcolmX + 7, malcolmY - 40);
            }
        }
    }
    for (int i = 0; i < hosmins.size(); i++) {

        Hosmin temp = hosmins.get(i);
        temp.Draw().paintIcon(this, engine, temp.GetX(), temp.GetY());
    }
    if (item == 1) {

        crack.paintIcon(this, engine, crackX, crackY);
    }
    else if (item == 2) {

        nuke.paintIcon(this, engine, nukeX, nukeY);
    }
    else if (item == 3) {

        shield.paintIcon(this, engine, shieldX, shieldY);
    }
    else if (item == 4) {

        skull.paintIcon(this, engine, skullX, skullY);
    }
    else if (item == 5) {

        trollface.paintIcon(this, engine, trollfaceX, trollfaceY);
    }
    else if (item == 6) {

        weed.paintIcon(this, engine, weedX, weedY);
    }
    engine.dispose();
    repaint();
    fps++;
}

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    g.drawImage(frame, 0, 0, this);
}

public void SetBackground(int background2) {

    weedbackground = background2;
}

public void SetCountdown(boolean countdown2) {

    countdown = countdown2;
}

public void SetBox2(int mode) {

    if (mode == 1) {

        box2.width = 96;
        box2.height = 37;
    }
    else {

        box2.width = 76;
        box2.height = 43;
    }
}

public void SetBox5(int mode) {

    if (mode == 1) {

        box5.width = 206;
    }
    else {

        box5.width = 213;
    }
}

}
Littm
  • 4,923
  • 4
  • 30
  • 38
Daniel Hammer
  • 39
  • 1
  • 5
  • What does `createImage()` do (other than the obvious)? It appears to be returning null, hence your error - but you need to show us the code of this method to enable us to help... it's also best to post code here directly rather than via image links, so the answers make sense later even if the links break. – DNA Sep 29 '12 at 21:35
  • 1
    When asking a question, please first sit in our shoes -- assume that we know nothing of the details of your problem or your current code, and then ask the question in a way so that we can understand what's going on. Your current question is *very* sparse on details, and I think that because of this is unanswerable. You may wish to fix this by telling and showing us more. – Hovercraft Full Of Eels Sep 29 '12 at 21:43
  • 1
    Sorry, added the code for the main class and the affected class. – Daniel Hammer Sep 29 '12 at 21:47
  • 1
    OK, now the error. You state "it crashes", but that tells us little, and I doubt that it will crash without informing you of something. Please post the entire error message, and then indicate with a comment in the code which line(s) are involved. – Hovercraft Full Of Eels Sep 29 '12 at 21:47
  • Added error and comment in the code were it appears. – Daniel Hammer Sep 29 '12 at 21:52

1 Answers1

1

You're calling getGraphics() on a null Image variable. If you look at the API for Component's createImage() you'll see why this is so:

returns an off-screen drawable image, which can be used for double buffering. The return value may be null if the component is not displayable. This will always happen if GraphicsEnvironment.isHeadless() returns true.

Engine is not displayed yet in its constructor. The GUI must be rendered first by calling pack() or setVisible(true) on the top level window before this JPanel will be displayed.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Lets, not mention, that Swing components provide double buffering support any way ;) – MadProgrammer Sep 29 '12 at 22:00
  • @MadP: nope, let's not mention that! – Hovercraft Full Of Eels Sep 29 '12 at 22:00
  • Tried to make the JFrame visible first but it didn't fix the problem. Anyway, got the game to work w/o double buffering and it flashes like shit. So i guess Swing doesn't support double buffering if these are Swing components. (super.paintComponent(engine)/paintIcon/this.setBackground/engine.setFont/engine.setColor/engine.drawString) – Daniel Hammer Sep 30 '12 at 04:12
  • @Daniel: A lesson that I learned a long time ago is don't assume anything, especially when you're just starting out. Swing supports double buffering just fine, but you're not using it correctly yet. Have you gone through the Java Swing painting tutorials? I would do that first before discarding Swing double buffering out of hand. – Hovercraft Full Of Eels Sep 30 '12 at 04:15
  • No, my teacher hasn't. But she did teach us a lot about graphics and swing. (38 pages .pdf file) – Daniel Hammer Sep 30 '12 at 04:55
  • Got dubble buffering to work :) – Daniel Hammer Sep 30 '12 at 20:45