#Making a Training Tool and getting Compiler Errors

1 messages · Page 1 of 1 (latest)

rapid gladeBOT
#

This post has been reserved for your question.

Hey @frail wigeon! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

frail wigeon
#

I have looked at Stack Overflow and stuck it in ChatGPT and nothing

rapid gladeBOT
#
private JLabel createIcon(String imagePath, int x, int y) {
	try {
		BufferedImage image = ImageIO.read(new File(imagePath));
		if (image == null) {
			System.out.println("Failed to load image: " + imagePath);
			return null;
		}
		ImageIcon icon = new ImageIcon(image);
		JLabel label = new JLabel(icon);
		label.setSize(100, 100);
		label.setLocation(x, y);
		label.addMouseListener(new DragAndDropListener());
		return label;
	} catch (IOException e) {
		e.printStackTrace();
		return null;
	}
}
frail wigeon
#

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at Maritime.main(MARITIME.java:90)
rapid gladeBOT
quaint stone
#

Probably just a wrong path. Try using the absolute path to the image first. If that workd chrck that your relazive path is correct

frail wigeon
#

ok

#

Line 90 down:
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Maritime();
});
}

quaint stone
#

Can you share the full stacktrace

frail wigeon
#

cant add it to line here to long!

quaint stone
#

And the stacktrace ?

frail wigeon
#

PS C:\Users\trcoo> & 'C:\Program Files\Java\jdk-20\bin\java.exe' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'C:\Users\trcoo\AppData\Local\Temp\vscodesws_6c664\jdt_ws\jdt.ls-java-project\bin' 'Maritime'
Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at Maritime.main(MARITIME.java:90)
quaint stone
frail wigeon
#

I thought so too!

quaint stone
#

I mean yeah the code crashes when the paths are not correct but other than that it should work

frail wigeon
#

ran it on a linux box (kali) and get errors:

#

I have changed the directory to:

#

I have stripped it back to 1 file to test at the moment

#

and chmod it!

frail wigeon
#

does it need double \

quaint stone
#

Lets imagine this is your project structure

Project
--src
----Maritime.java
--Picutres
----Picture1.png
----Picture2.png
...

Then the correct relative path would be: Pictures/Picture1.png.
The error indicates that your method createIcon returned a null value. As I said that happens when the path is wrong.
Could you share your project structure ?