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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:44:38+00:00 2026-06-16T09:44:38+00:00

I am using eclipse and I want to add a image onto a label

  • 0

I am using eclipse and I want to add a image onto a label or a logo for my GUI.

I want it to LOOK LIKE THIS!

http://imageshack.us/a/img593/4957/skeletancalcimage.png

I uploaded it onto the web because I dont have 10 reputation.

(Names cool program) is the image

 import java.awt.*;
    import javax.swing.*;
    import javax.swing.JFrame;

    public class MainClass extends JFrame{ // super class JFrame

        // Kind of a tutorial for creating GUI's in java
        private JLabel label;
        private JLabel label1;
        private JButton button;
        private JTextField textfeild;
        private ImageIcon image1; // image for my logo

        public MainClass () {
            setLayout (new FlowLayout());

            image1 = new ImageIcon (getClass ().getResource ("logo.gif")); // declares image

            label = new JLabel ("This is the label");
            add (label);

            label1 = new JLabel (image1); // adds image
            add (label1);

            textfeild = new JTextField (15);
            add (textfeild);

            button = new JButton ("Click");
            add (button);

        }

        public static void main(String[] args) {        

            MainClass gui = new MainClass ();


            gui.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // creates window
            gui.setSize (300,300);
            gui.setVisible (true);
            gui.pack ();
            gui.setTitle ("Title");

        }

    }

Program compiles but doesn’t run. Gives me

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at JonsCalc.<init>(JonsCalc.java:18)
    at JonsCalc.main(JonsCalc.java:38)
  • 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-16T09:44:39+00:00Added an answer on June 16, 2026 at 9:44 am

    This piece of code getClass().getResource ("logo.gif") means that the image should be loaded from the same location as your class MainClass has. Check that you image is in the same package as MainClass. If you use Eclipse, than put image file in the package in src folder, it will be copied to bin automatically.

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

Sidebar

Related Questions

I'm using Eclipse Java EE IDE for Web Developers and want something like that:
I am using eclipse with the maven 2 plugin. I want to add the
I am using Eclipse 3.6, and want to add Richfaces to Palette.Could anyone give
I am using tomcat server in eclipse and I want to add a background
I am using eclipse as my ide. I want to add log4j to the
I'm trying to build a new web app using Eclipse, I want to use
I want to add a combo box to the toolbar (coolbar) of eclipse using
I'm using Eclipse 3.6 Helios (for Java Developers) and want to add the javax.servlet
We're using Eclipse (with the eGit plugin) and I want to host a repo
i'm using ECLiPSe programming logic system. i want to implement the yield method for

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.