Sorry newbie here. I get an error in Netbeans while trying to add an Icon to a J label. The error is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
After importing all of the images I used for my project. I still get this error.
This is the code in default in Netbeans:
diceButton = new javax.swing.JButton();
diceButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/dice.png"))); // NOI18N
(https://i.stack.imgur.com/VCANk.png)
I'm expecting to add it to a jlabel so that the image will also be at the jar file
You should also try this method to better understand label and button's icon (image) property.
Using absolute path, like my image in D drive in img folder,
ImageIcon icon = new ImageIcon("D:\\img\\img-name.jpg");
img_label.setIcon(icon);
Or you can also do this from label/image icon- property by specifying absolute path.
Related
So, I am creating a GUI interface for the project via Netbeans (using it is a requirement). Everything is fine until I add an Icon for a label. The generated code is below:
jLabel25 = new javax.swing.JLabel();
jLabel25.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/mycompany/sostarnyba/images/map.png")));
jLabel25.setText("");
I add the icon exactly as the person in this question shows it: Netbeans ImageIcon not displaying
Except my app does not run at all.
When the icon is removed, everything runs, but when it is added it does not. The error is: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null.
What am I doing wrong?
The problem was solved by putting not a full path within the project but the absolute path in your computer starting from /Users
I'm developing a cross platform desktop application using Jython and Swing, and I found a hitch.
I would like to develop a button that allows me to load an image in its background and to change it when I reclick on the image loaded.
I attach as an example some pictures on how I would like my widget to be.
Upload pane without image
And then, when I load an image:
Upload Pane with image
I tried with the following code:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
fileName = fd.getFile()
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
"""'self.bigDict['imgButton']' is the current JButton"""
self.bigDict['imgButton'].setIcon(ImageIcon(fileAbsPath))
When I click on "Open", the image is not inserted into the button. I do not understand why.
I also tried with the following code:
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
img = ImageIO.read(getClass().getResource(fileAbsPath))
self.bigDict['imgButton'].setIcon(img)
In the above example the following error is reported:
img = ImageIO.read(getClass().getResource(fileAbsPath))
TypeError: getClass(): expected 1 args; got 0
I would be curious to know why the button is not updated with the image loaded, and why the above error in java would not happen.
Thanks in advance!
getClass() needs 1 argument: the implicit this argument. You must call the method on an object, or use the MyClass.class notation.
The problem is very simple.
When an image is loaded with FileDialog it is "virtually located" in the FileDialog window, but the image does not exist. I realized this when I tried to copy the image from the absolute path to the destination folder, using shutil.copy2(self.imgAbsPath, destinationPath+'/'+self.imgName), which reported an error message indicating that the image does not exist.
To provide the concrete path of a file, it is necessary to add information about the folder where it is located.
In practice you have to create the relative path, before generating the absolute one:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
self.imgName = fd.getFile()
relativePath = fd.getDirectory() + fd.getFile() """Here is the missing piece!"""
if self.imgName != None:
self.imgAbsPath = os.path.abspath(relativePath) """Here the absolute path is correctly obtained!"""
self.bigDict['imgButton'].setIcon(ImageIcon(ImageIcon(self.imgAbsPath).getImage().getScaledInstance(130, 130, Image.SCALE_DEFAULT)))
"""The image, before being inserted into the button, is slightly resized, to avoid graphic bumps!"""
I hope I've been helpful.
I was following a reference and some tutorial on setting the path of an image for a JLabel but can't get the path correctly.
I have
String pathToUserPictureImage = "resources/Assets/studentPhoto.png";
ImageIcon userPicture = new ImageIcon(getClass().getClassLoader().getResource(pathToUserPictureImage));
JLabel userPictureImagePlaceHolder = new JLabel(userPicture, HEIGHT);
And my folders in the project are:
When I run it, it would return an error message about a Null Pointer which I suspect to be a problem with the path supplied.
I would appreciate any suggestion and correction to my code. I recently just switched from dragging and dropping components to writing code by hand.
(First of all, I'm sorry If there is like a million questions like this. I've tried everything I can and this is driving me nuts!)
I'm trying to had an icon to a JButton but I keep getting an IllegalArgumentException caused by ImageIO.
Here's what I have:
//Other UI elements ^
JButton X = new JButton("Clear");
//com.oliveira.ux is the package name
Image img = ImageIO.read(getClass().getResource("/com.oliveira.ux/resource/gtk-clear.png"));
Icon clear = new ImageIcon(img);
//More UI elements
The Icon is located under src/PACKAGE NAME/resource/. (I use eclipse).
I've tried to change the location on the code above (the one I wrote here was the last one I tried) but all I get is an IllegalArgumentException when I run de .jar. Any suggestions?
Many thanks in advance
Here's the full error message:
Caused by: java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.oliveira.ux.Main.<init>(Main.java:146)
at com.oliveira.ux.Main.main(Main.java:75)
... 5 more
This points to the ImageIO part in the code I wrote above.
The path appears to be wrong...
Image img = ImageIO.read(getClass().getResource("/com.oliveira.ux/resource/gtk-clear.png"));
getResource isn't expecting the pack name, but the "path" to the resource from the context of the class path (so the path is appended to the class path elements)
Something like...
Image img = ImageIO.read(getClass().getResource("/com/oliveira/ux/resource/gtk-clear.png"));
Should give a better result
I'm trying to display two pictures on my JFrame, the way I found was to use icons and JLabels, this seems pretty straightforward and I'm not having problems with this. But when it comes to locating the image I can't get it to work. I'm on a linux machine thus the forwardslash style. I created a folder called pics in my project which is called 399assig1.
ImageIcon icon1 = createImageIcon("/home/dsk03/ugrad/jeanbern/workspace/C291/workspace/399assig1/pics/fur-05.jpg","First");
this.label1 = new JLabel("Picture 1", icon1, JLabel.CENTER);
ImageIcon icon2 = createImageIcon("pics/fur.png","Second");
this.label2 = new JLabel("Picture 2", icon2, JLabel.CENTER);
this is the error I get
Couldn't find file: /home/dsk03/ugrad/jeanbern/workspace/C291/workspace/399assig1/pics/fur-05.jpg
Couldn't find file: pics/fur.png
if createImageIcon() is searching the CLASSPATH for the file, you'd need to add the root directory to the CLASSPATH. A better approach would be use a path that is relative to a directory that is already included in the CLASSPATH.
Like so:
%>CLASSPATH=$CLASSPATH;/home/dsk03/ugrad/jeanbern/workspace
then your call would be
ImageIcon icon1 = createImageIcon("399assig1/pics/fur-05.jpg", "MyIcon");