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 7933163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:14:41+00:00 2026-06-03T21:14:41+00:00

I am developing a java project in eclipse. In order to distribute the program

  • 0

I am developing a java project in eclipse. In order to distribute the program I have created a runnable .jar-file via the export-function in eclipse.

In my program a load several images that are stored in a folder called ‘tableImages’. I load the images via the ClassLoader (you’ll find the code snippets below). The problem is the following: When executing the program from the .jar-file, it throws a NullPointerException when loading one of the .png-files in the above mentioned folder. Now the funny thing is, that some of the .png-files in the exact same folder are loaded correctly, meaning they don’t cause a NullPointerException.

I checked the content of the .jar using the jar tf command. The image that apparently cannot be loaded, was packed into the jar. So what is causing this NullPointerException and how can I solve it?

Here is the class with which I draw images to my swing-frame.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.net.*;

public class LoadBackgroundImage extends Component {

    private static final long serialVersionUID = 1L;

    BufferedImage img;

    public void paint(Graphics g){
        g.drawImage(img, 0, 0, null);
    }

    public LoadBackgroundImage(String image){
        URL url = ClassLoader.getSystemResource(image);
        try{
            img = ImageIO.read(url);
            this.setSize(img.getWidth(), img.getHeight());
        }catch(IOException e){
            System.err.println(e.getMessage());
        }
    }

    public Dimension getPreferredSize(){
        if(img == null){
            return new Dimension(100, 100);
        }else{
            return new Dimension(img.getWidth(null), img.getHeight(null));
        }
    }

    public BufferedImage getImage(){
          return img;
         }

}

Thank you very much for your help!

  • 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-03T21:14:42+00:00Added an answer on June 3, 2026 at 9:14 pm
    URL url = ClassLoader.getSystemResource(image);
    

    This URL translates to absolute path in the file system which will be an invalid path for a file within jar. For example, if you have a file called foo.png under bar.jar the URL will be /path_to_current_dir/bar.jar/!foo.png which is invalid. Instead you can read the file as a stream:

    InputStream is = getClass().getClassLoader().getResourceAsStream(image);
    ImageIO.read(is);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a JSP project in Eclipse and have chosen File -> New
While developing a Java project, I place all of my jar files in a
For you all information i am developing one java project using Eclipse to interact
I am developing a dynamic web project j2ee web application using eclipse. I have
I have just created a .properties file in Java and got it to work.
Possible Duplicate: Developing for Android in Eclipse: R.java not generating I cleaned my project
I am developing a Java project in which i have a sub-module where i
I have a project this year. It is about developing a Java application with
I am currently developing an android project using eclipse JAVA and I had just
I'm developing a Java application using Eclipse. My project has two source directories that

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.