Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.4K
Views
How to add an ImageIcon in java? this code is not working

I want to add an ImageIcon to the Panel, but I got this Exception (

java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.swing.JLabel.setBounds(int, int, int, int)"

because "this.LABEL2" is null and I do not know how to solve it , help me please.

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

public class ShowQR extends JFrame{
    private JPanel PANEL;

    private JLabel LABEL2;

    private ImageIcon ICON;
    
    

    public ShowQR(){
    
    super("Choose background color");
    

    
    ICON = new ImageIcon(getClass().getResource("qr-code2.png"));
    LABEL2= new JLabel(ICON);
    
    PANEL.setLayout(null);

    LABEL2.setBounds(50, 50, 50, 50);

    PANEL.add(LABEL2);


    add(PANEL);
    
    setVisible(true);



    setDefaultCloseOperation(EXIT_ON_CLOSE);



    setSize(400,400);
    }
}

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

public ImageIcon image = new ImageIcon(Test.class.getResource("give image path"));

Please try this way. give the image path in you file structure.

over 4 years ago · Santiago Trujillo Report

0

This might help

ICON = new ImageIcon("specify icon path here "); 

//you might need to scale the icons size 
Image image = image.getImage().getScaledInstance(width, height,Image.SCALE_DEFAULT);

ImageIcon ICON2 = new ImageIcon(image);

LABEL2 = new JLabel(ICON2);

LABEL2.setBounds(x,y,width,height);

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!