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

  • SEARCH
  • Home
  • 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 8146767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:06:48+00:00 2026-06-06T14:06:48+00:00

I’m still fairly new to Java, but I’m fairly sure this shouldn’t happen. There’s

  • 0

I’m still fairly new to Java, but I’m fairly sure this shouldn’t happen. There’s some sort of bizarre logic error in my paintComponent method, which is preventing images from being drawn properly. Normally this wouldn’t be an issue, and I could find a way around it. However, the problem doesn’t always exist. Commenting and decommenting one line at a time will sometimes get around it, as will closing and reopening the IDE. (Eclipse.) What could possibly cause this, and what can be done to avoid it?

Here’s my method:

public void paintComponent(Graphics g){
    g.drawImage(titlebg, 0, 0, null);
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(300, 250, 300, 50);
    //g.fillRect(400, 400, 300, 50);
    g.drawImage(ccground, 0, 0, null);
}

EDIT: The drawImage methods are not functioning when the setColor and fillRect methods are uncommented. Sometimes they don’t work even if it’s just the two of them. Sometimes they do.

EDIT 2: The only other call to the images is in the following code, which initializes them.

Toolkit tk = Toolkit.getDefaultToolkit();

public Image retrieveImage(String x){
    URL y = this.getClass().getResource(x);
    Image img = tk.getImage(y);
    return img;
}

This is referenced with:

public void loadImages(){
    ccground = rl.retrieveImage("\\Chrysaline Caverns ground tile.png");
    titlebg = rl.retrieveImage("\\Title Screen.png");
}

in the same class as the paintComponent method.

EDIT 3: Stacktrace.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at gui.Main.createGui(Main.java:32)
at gui.Main$1.run(Main.java:21)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Main.class

public class Main extends JFrame{
static Window w;
JFrame f;
Image ccground;
Main(){
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {
            createGui();
        }
    });

}
public static void main(String[] args){
    w = new Window();
}
public void createGui(){
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(w);
    f.pack();
    f.setVisible(true);
    f.setBackground(Color.BLACK);
}
}

EDIT 4: Window.java

public class Window extends JPanel{
Main m;
ResourceLoader rl;
Image ccground;
Image titlebg;
public Window(){
    rl = new ResourceLoader();
    m = new Main();
    loadImages();
}
public Dimension getPreferredSize(){
    return new Dimension(800, 592);
}
public void loadImages(){
    ccground = rl.retrieveImage("\\Chrysaline Caverns ground tile.png");
    titlebg = rl.retrieveImage("\\Title Screen.png");
}
public void paintComponent(Graphics g){
    g.drawImage(titlebg, 0, 0, null);
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(300, 250, 300, 50);
    //g.fillRect(400, 400, 300, 50);
    g.drawImage(ccground, 0, 0, null);
}
}
  • 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-06T14:06:49+00:00Added an answer on June 6, 2026 at 2:06 pm

    Toolkit.getDefaultToolkit().getImage(url) is the culprit. The method gets you an image that is not necessarily loaded yet (an empty dummy in a way).
    Since you don’t do anything to ensure the image is loaded, it sometimes is and sometimes it isn’t.

    One of many ways to ensure the image is completely loaded before using it is (shortest one I know of) to excute this for each image obtained from Toolkit before using it:

    new ImageIcon(image).getImage();


    EDIT:

    Alter you retrieveImage like this and it should work OR clearly indicate the image was not found:

    public Image retrieveImage(String x){
        URL y = this.getClass().getResource(x);
        Image img = tk.getImage(y);
        if (img == null)
            throw new IllegalArgumentException("image " + x + " not found");
        return new ImageIcon(img).getImage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.