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.
Printable View
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 errorQuote:
thereī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:Quote:
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:Quote:
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:Quote:
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);
}
}
Quote:
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.Quote:
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.Quote:
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.