• Post Reply Bookmark Topic Watch Topic
  • New Topic

getResource problem to load a picture

 
francois lebel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at bucky.Gui.<init>(Gui.java:25)
at bucky.apples.main(apples.java:8)


I'm new begginer in JAVA!
I'm doing the tutorial http://thenewboston.org/watch.php?cat=31&number=63

The problem comme when I try to load a png file
What is the goo syntax???

"C://Usersfrancois//workspace//bucky//src//bucky//b.png" TRIED EVERYTHING!!!


===============================================CLASS GUI============

package bucky;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Gui extends JFrame {

private JButton reg;
private JButton custom;

public Gui (){
super("the title");
setLayout(new FlowLayout());
reg = new JButton("reg Button");
add(reg);

//Icon b = new ImageIcon(getClass().getResource("C://Usersfrancois//workspace//bucky//src//bucky//b.png"));
//Icon b = new ImageIcon(getClass().getResource("C://Usersfrancois//workspace//bucky//src//bucky//b.png"));
Icon b = new ImageIcon(getClass().getResource("/C:/Usersfrancois/workspace/bucky/src/bucky/b.png"));
Icon x = new ImageIcon(getClass().getResource("/C:/Usersfrancois/workspace/bucky/src/bucky/x.png"));
custom = new JButton("Custom",b);
custom.setRolloverIcon(x);
add(custom);

HandlerClass handler = new HandlerClass();
reg.addActionListener(handler);
custom.addActionListener(handler);

}


private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
}
}


}
======================================Class apples==============




package bucky;
import javax.swing.JFrame;


class apples {
public static void main(String[] args){
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}

}

=======================================class apples======================



package bucky;
import javax.swing.JFrame;


class apples {
public static void main(String[] args){
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}

}



 
Stephan van Hulst
Bartender
Posts: 6584
84
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably shouldn't use absolute paths with getResource(). This method is for obtaining resources relative to your classpath.

Since your image is in the same package as the class you're loading it in, you can just perform getResource("b.png").

For resources that are relative to a classpath, use a leading slash: getResource("/bucky/b.png").
 
francois lebel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably shouldn't use absolute paths with getResource(). This method is for obtaining resources relative to your classpath.

Since your image is in the same package as the class you're loading it in, you can just perform getResource("b.png").

For resources that are relative to a classpath, use a leading slash: getResource("/bucky/b.png").
francois lebel
Post Yesterday 18:46:52 Subject: getResource problem to load a picture
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at bucky.Gui.<init>(Gui.java:25)
at bucky.apples.main(apples.java:8)

I tried your advise, still doesen't work STILLL NEED YOUR HELP!

I tried : getResource("/bucky/b.png") and Icon x = new ImageIcon(getClass().getResource("/bucky/src/bucky/x.png"));

Still not able to find the file!



C:\Users\francois\workspace\bucky\src\bucky MY 2 files are in this folder by the way , my 2 classes
I don't see my 2 pictures png in My project, but I put in the same folder than my 2 classes



 
Rob Camick
Ranch Hand
Posts: 2701
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learn how to use the forum tags. When you post code use the "Code" tag. When you quote a reply (or part of a reply) then use the "Quote" tag.

Don't use the leading "/" in your file name.

Read the section from the Swing tutorial on How to Use Icons for more information and working examples.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the important part: The URL you pass to the getResource() method is relative to the classpath. You are still assuming that your folder structure means something. It doesn't. You haven't told us what is in your classpath, but if you're going to use an absolute URL then you need to know that the search will go through each of your classpath roots and look for the URL. If you use a relative URL, then it's relative to the class whose getResource() method you called.
 
francois lebel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still have the same problem, how I can add .png in Eclipse

How I can import PGN file in a project in Eclipse, just to be able to see the file, like my class

I thought it was easy, but this is difficult

I tried the Import/export doesen't work! I tried GENERAL , doesen't work!!!
 
francois lebel
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK it's right, I FOUND THE AWNSER MYSELF

THANK YOU!!!
 
Tapan Agarwal
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
francois lebel wrote:OK it's right, I FOUND THE AWNSER MYSELF

THANK YOU!!!



Hi Francois,

I have been facing the same issue.



Error is
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at org.taps.cm.View.main(View.java:27)

Any idea whats wrong here.
 
Gunjan Jadon
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapan Agarwal wrote:
francois lebel wrote:OK it's right, I FOUND THE AWNSER MYSELF

THANK YOU!!!



Hi Francois,

I have been facing the same issue.



Error is
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at org.taps.cm.View.main(View.java:27)

Any idea whats wrong here.

Hello Tapan Agarwal,
I am not sure, May be this will help you. You can link it with your reuirement.
you can use this-

Note: C:\\Users\\ranig\\My\\spaceinvaders\\ball.png is the whole path of ball.png image.

instead of this:

 
Rob Camick
Ranch Hand
Posts: 2701
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Gunjan,

You should NOT be hardcoding a directory structure to load files. You can not guarantee the same structure will exist on all computers.

The better way is to use the "getResource()" method.

Read the section from the Swing tutorial on How to Use Icons for more information.

 
Gunjan Jadon
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:@Gunjan,

You should NOT be hardcoding a directory structure to load files. You can not guarantee the same structure will exist on all computers.

The better way is to use the "getResource()" method.

Read the section from the Swing tutorial on How to Use Icons for more information.



Hello Rob Camick,
Actually I faced same problem and I solved out myself using this. But you are right. This is not the correct way. Now I have to use "getResource()".
Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!