My code compiled without errors, but when I modified three pics in photoshop and turned them to gif (from jpg) the program wont compile since thereīs some error with the getResource(). How to fix? Canīt change back either.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
My code compiled without errors, but when I modified three pics in photoshop and turned them to gif (from jpg) the program wont compile since thereīs some error with the getResource(). How to fix? Canīt change back either.
What error and where in what code? We can only guess without the full text of the error and code accompanying said errorthereīs some error with the getResource()
Ok, thought the error may come from some rule like that the getResource() loads stuff or something.
Error:
Hereīs the code:fileName=pink.gif
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Pointer.<init>(Pointer.java:18)
at Clock.<init>(Clock.java:26)
at Main.<init>(Main.java:7)
at Main.main(Main.java:17)
Main:
Clock:import javax.swing.JFrame;
public class Main extends JFrame {
public Main(){
add(new Clock());
setTitle("Klocka");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400,400);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
}
public static void main(String[] args){
new Main();
}
}
Pointer:import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Clock extends JPanel {
private Image back;
public Pointer pPink;
public Pointer pWeird;
public Pointer pRose;
public Clock(){
pPink = new Pointer(3600,"pink.gif");
pWeird = new Pointer(60,"weird.gif");
pRose = new Pointer(1,"rose.gif");
}
public void paint(Graphics g) {
if (pPink.img != null){
System.out.println("got through if statement pPink");
g.drawImage(pPink.getImg(), 200, 200, null);
}
if (pWeird.img != null){
System.out.println("got through if statement pWeird");
g.drawImage(pWeird.getImg(), 250, 200, null);
}
if (pRose.img != null){
System.out.println("got through if statement pRose");
g.drawImage(pRose.getImg(), 300, 200, null);
}
ImageIcon i = new ImageIcon(this.getClass().getResource("wheel.png") );
back = i.getImage();
g.drawImage(back,50,50, null);
}
}
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Pointer {
public int speed;
public String fileName;
public Image img;
public Pointer(int speed, String fileName) {
this.speed = speed;
this.fileName = fileName;
System.out.println("fileName=" + this.fileName);
ImageIcon ii = new ImageIcon(getClass().getResource(this.fileName));
img = ii.getImage();
}
public Image getImg(){
System.out.println("args=" + fileName);
return img;
}
}
As I mentioned earlier: the code worked before changing the jpgīs to gifīs (I changed all .jpg names in the code to .gif also).
Look at the code at line 18.main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Pointer.<init>(Pointer.java:18)
Does the getResource method return a null?
Does the file referenced in the fileName variable exist?
The getResource() method returns the fileName
And yes, the file exists.
I thinks itīs quite weird since all I did was to modify the pics from jpg to gif, (deleted the jpgs and inserted the gifs to my drawable folder, and turned every jpg in the code to gif).
What is causing the NullPointerException? What variable or value is null?
The code shows that the fileName variable being passed to the getResource() method.The getResource() method returns the fileName
Your statement does not make sense.
Read the API doc for the getResource method to see what it returns.