-1
package HotelManagement;

import javax.swing.*; 

public class HotelManagementSystem extends JFrame{
    HotelManagementSystem(){
        setSize(1366,768);
        setLocation(100,100);
        ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("icons/hotel.jpg"));
        JLabel image = new JLabel(i1);
        add(image);

        setVisible(true);
    }

    public static void main(String[] args) {
        new HotelManagementSystem();        
    }

}

error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null.

This is my code and I have icons folder which contains hotel.jpg in src folder of the project in eclipse. I tried using "System.getProperty("user.dir")" and also tried using "./icons/hotel" and "(System.getProperty("user.dir")+ ".\icons\hotel.jpg")" and everything I found on internet.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sakshi
  • 1
  • 1
  • 1
    does the file exist? does it exist where you think it exists? Where do you think that path will look for a file? – Stultuske May 30 '23 at 07:00
  • couldn't understand you – Sakshi May 30 '23 at 07:03
  • most likely it's not looking in the directory you think it's looking – Stultuske May 30 '23 at 07:07
  • Please tell me a way to fix it – Sakshi May 30 '23 at 07:09
  • 1
    put the file in the right directory – Stultuske May 30 '23 at 07:09
  • The error says `location` is `null`, but your code does not contain a variable or field named `location`. Please show the full error including the stack trace. – howlger May 30 '23 at 07:11
  • Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null at java.desktop/javax.swing.ImageIcon.(ImageIcon.java:234) at HotelManagementSystem/HotelManagement.HotelManagementSystem.(HotelManagementSystem.java:10) at HotelManagementSystem/HotelManagement.HotelManagementSystem.main(HotelManagementSystem.java:20) – Sakshi May 30 '23 at 07:13
  • This `new ImageIcon(ClassLoader.getSystemResource("icons/hotel.jpg"))` will be looking for the `hotel.jpg` in the `HotelManagement` package. Depending on your IDE and build system will, however, change where you need to store it in order for it to be picked up – MadProgrammer May 30 '23 at 07:21
  • 1
    Your file is not a "system resource". – Mark Rotteveel May 30 '23 at 08:15

1 Answers1

-3

I guess that the problem exists in setLocation(100,100), which isn't displayed. If you show the code for that method, maybe we can help.

As Stultuske wrote, you can often suspect that a file is missing or is in the wrong place...

Harry
  • 5
  • 3
  • But I can access my file in that folder manually even when I am copying the path for the file, it's the same path – Sakshi May 30 '23 at 07:12