Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8612003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:31:12+00:00 2026-06-12T04:31:12+00:00

I’m learning about double buffering and the book I got from my teacher tells

  • 0

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;
    }
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:31:13+00:00Added an answer on June 12, 2026 at 4:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.