Not sure why image is no being displayed - java

So im importing an image to use as a background and for some reason its giving me:
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
Could someone help me?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class PixelLegendsMain extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent e){
}
public static void main(String[ ] args)throws Exception{
PixelLegendsMain plMain = new PixelLegendsMain();
arenaBuild arena = new arenaBuild();
plMain.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
plMain.add(arena);
plMain.setSize(600,460);;
plMain.setVisible(true);
plMain.setResizable(false);
plMain.setLocation(200, 200);
}
}
This is the main class and this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
import java.io.*;
import javax.swing.Timer;
public class arenaBuild extends JPanel{
String picPath = "pictures/";
String[] fileName = {picPath+"stageBridge.png", picPath+"turret.png"};
ClassLoader cl = arenaBuild.class.getClassLoader();
URL imgURL[] = new URL[2];
Toolkit tk = Toolkit.getDefaultToolkit();
Image imgBG;
public arenaBuild()throws Exception{
for (int x=0;x<2;x++){
imgURL[x]= cl.getResource(picPath+fileName[x]);
}
imgBG = tk.createImage(imgURL[0]);
}
public void paintComponent(Graphics g){
g.drawImage(imgBG,0,0,600,460,0,0,600,460, this);
}
}
Thjis is where im calling the image in. Im new to this so i'd appreciate if someone can explain why this is happening and help me fix it :D

The most likely explanation is that your tk.createImage(imgURL[0]) call is passing a null URL.
How could this happen? Well the ClassLoader.getResource(String) method is specified as returning null if it can't find a resource ... so it would seem that the problem is that you are using the wrong path for the first resource.
The path you are using appears to be this: "pictures/pictures/stageBridge.png":
It seems unlikely that you really put your images in a directory called "pictures/pictures".
Since you are calling the method on a ClassLoader object (rather than a Class object), the notionally relative path you are using will be treated as an absolute path; i.e. you will get "/pictures/..." rather than "/PixelLegendsMain/pictures/..."

Seems that i unfortuantely did not look at my own code long enough, it seems that i was calling the picPath twice so instead of the path being
"pictures/stageBridge.png"
it was
"pictures/pictures/stageBridge.png"
Sorry for the waste of time, and thank you all for answering

Try printing the current working directory, in order to check if your images path are correct.
This is a simple way of doing that:
System.out.println(new File(".").getAbsolutePath());
Maybe the problem is that the images pictures/tageBridge.png and pictures/turret.png are not the correct path to the file.
Hope it helps!

Related

Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(Unknown Source)

I am working on a little program for school however I am getting this error whenever I run it:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Creatieve.Opracht.Main.main(Main.java:14)
The langugae I am coding in is Java, and before someone comments on it, no this is not a computer sience project so I can't ask a teacher.
This is de code I have written:
package Creatieve.Opracht;
import java.awt.*;
import javax.swing.*;
public class Main {
static JFrame frame;
static Puzzel puzzel;
public static void main(String[] args){
frame = new JFrame("CKV Creatieve Oprdacht 2.0");
frame.setSize(900, 900); //Lengte en breedte van de foto
puzzel = new Puzzel(new ImageIcon(Main.class.getResource("/image.png")).getImage());
frame.add(puzzel);
frame.setVisible(true);
}
}
And also this:
package Creatieve.Opracht;
import java.awt.*;
import javax.swing.*;
public class Puzzel extends JPanel{
Onderdeel[] onderdelen;
Image img;
public Puzzel(Image img){
this.img = img;
onderdelen = new Onderdeel[9];
int onderdeelGrootte = img.getWidth(null)/3;
for(int i = 0; i != onderdelen.length; i++){
onderdelen[i] = new Onderdeel(this, i, onderdeelGrootte);
}
}
public void paint(Graphics g){
super.paint(g);
for (int i = 0; i != onderdelen.length; i++){
onderdelen[i].paint(g);
}
}
}
It would be awesome if one of you could tell me how I can fix the problem.
Thank you in advance!
Exception came on this line in your code when you are accessing image,
new ImageIcon(Main.class.getResource("/image.png")).getImage().
you have to check that /image.png image available at this location or not, if possible use qualified path name of your folder to resolve issue.
The resource file /image.png does not exists. So Main.class.getResource("/image.png") is returning null. Hence while creating the ImageIcon object with null is resulting in NullPointerException
to solve this issue.....
just copy your images to /your_Drive_in_which_workspace_folder_is_present/workspace/projectname/bin/projectname/
now this statement should be like this:
Icon IconObject=new ImageIcon(getClass().getResource("Image_name.png"));

Error while loading image

Here is my code:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import model.Map;
public class MyView {
private BufferedImage img = null;
private static MyPanel panel;
//init image
try{
img = ImageIO.read(new File("/src/minepic/start.png"));
} catch (IOException e){
System.out.println(e.getMessage());
}
}
I want to load an PNG image from the src directory but I don't know why it doesn't work, anyone can help me?
Error in command "try catch" and NetBeans say "unreported exception IOexception; must be caught or declared to be thrown"
One more, even i declared img as a BufferedImage before, but in command "try catch" img just like didn't declared because in NetBeans it doesn't become green, still black.
There are few issues with the code:
It is incomplete. The braces are not matched.
you have written code outside main method (possible but not recommended)
To read the image from src folder (which is a part of your class path) use the below snippet:
Inputstream is = MyView.class.getResourceAsStream("minepic/start.png");
if(is==null){
is = MyView.class.getClassLoader().getResourceAsStream("minepic/start.png");
}
img = ImageIO.read(is);

VLCJ NullPointer (I just want a simple cross-platform java video player)

I am wanting to make a simple java applicaiton to play video. I want it to play mpeg4 and mov formats in particular. JMF is what I started with and I have a lovely working example. However, there is no support for mov or mpeg4 formats. I've looked at Xuggler but can't see a SIMPLE way to get it working. VLCJ seemed easy - I downloaded the jar files and attached them to my project (vlcj-2.1.0.jar, jna-3.4.0.jar, platform-3.4.0.jar, vlcj-2.1.0.jar)). I got the sample code and adapted it (below). But when I run the code, I get a java.lang.NullPointerException exception. I've tried adjusting the number and direciton of the slashes (forward and backward) in the filename. Nothing seems to work. Please could you help???
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JFileChooser;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import java.lang.Object;
import uk.co.caprica.vlcj.mrl.FileMrl;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
public class TestPlayer {
private final JFrame frame;
private EmbeddedMediaPlayerComponent mediaPlayer;
public static void loadLibs(){
NativeLibrary.addSearchPath(
RuntimeUtil.getLibVlcLibraryName(), "C:/Program Files/VideoLAN/VLC/"
);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}
public static void main(final String[] args){
loadLibs();
final String mrl = "file://C:/Test.mov";
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestPlayer().run(mrl);
}
});
}
public TestPlayer(){
frame = new JFrame("test VLCJ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100,100);
frame.setSize(600,400);
frame.setVisible(true);
}
private void run(String mrl){
System.out.println(mrl);
try{
mediaPlayer.getMediaPlayer().playMedia(mrl);
}catch(Exception e){
System.err.println(e.toString());
}
}
}
I'm using VLC version 2.0.2 and VLCJ 2.1.0 sources and JDK 1.7 on windows 32 bit. I hope it's something simple...
It looks like you are using mediaPlayerwithout ever initializing it, thus causing a NullPointerException in run().
Try initializing it in your constructor.

xuggler error - "cannot find symbol : class VideoImage" - what am i missing?

I am using xuggler to play video files from my code and the following is a snippet from the main code:
This snippet produces an error :
//The window we'll draw the video on.
private static VideoImage mScreen = null;
private static void updateJavaWindow(BufferedImage javaImage)
{
mScreen.setImage(javaImage);
}
// Opens a Swing window on screen.
private static void openJavaWindow()
{
mScreen = new VideoImage();
}
The error that i get is : cannot find symbol : class VideoImage
The header files used are :
import java.awt.image.BufferedImage;
import com.xuggle.xuggler.Global;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IPacket;
import com.xuggle.xuggler.IPixelFormat;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IVideoPicture;
import com.xuggle.xuggler.IVideoResampler;
import com.xuggle.xuggler.Utils;
Am i missing in some import statement ? If not , here are the libraries i am using apart from JDK :
What is the reason i am getting that error ?
VideoImage Javadoc
You are not importing the right class.
com.xuggle.xuggler.demos.VideoImage
It seems like you are already using an IDE. It should automatically tell you what import you are missing if the correct library is in the build path.
You need to import the VideoImage class.

Exception in thread "main" java.lang.noClassDefFoundError org/jdom/input/SAXBuilder

Hello this is Lonnie Ribordy,
I have a program i am trying to write and part of it uses a 3rd party api called JDom,
when i compile my program the it compiles perfectly fine.. but, when i try to run it i get the Exception in thread "main" java.lang.noClassDefFoundError org/jdom/input/SAXBuilder
my program is as below...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class COS extends JPanel implements ActionListener{
static JFrame f=new JFrame();
Image bgImage=null;
String message="";
public COS(){
try{
String xml="background.xml";
SAXBuilder builder=new SAXBuilder();
Document doc=builder.build(new File(xml));
Element root=null;
Element img=null;
String fimg=null;
try{
root=doc.getRootElement();
img=root.getChild("bgimage");
fimg=img.getText();
} catch(Exception e){
}
getFileImage(fimg);
} catch(Exception e){
message="File load failed: "+e.getMessage();
}
}
public void paintComponent(Graphics g){
if(bgImage!=null){
g.drawImage(bgImage,0,0,this);
}
else{
g.drawString(message,40,40);
}
}
public void getFileImage(String filein) throws IOException, InterruptedException{
FileInputStream in=new FileInputStream(filein);
byte[] b=new byte[in.available()];
in.read(b);
in.close();
bgImage=Toolkit.getDefaultToolkit().createImage(b);
MediaTracker mt=new MediaTracker(this);
mt.addImage(bgImage,0);
mt.waitForAll();
}
public void actionPerformed(ActionEvent e){
}
public static void main(String[] args){
COS newcos=new COS();
f.setSize(825,640);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);
newcos.setBounds(5,5,800,600);
f.setLocation(10,5);
f.getContentPane().add(newcos);
f.setVisible(true);
}
}
could anybody tell what's wrong?
i found out my problem, when i installed JDom into my java i forgot to include it in the jre lib/ext
and now everything works just like it is supposed to work, thankyou very much for the time you spent helping me
I believe the issue is that you have an old version of JDom in your classpath, and that is being loaded before the one you want.
Firstly, make sure that you don't have another version of JDom (besides the one you've downloaded) kicking about in your classpath. To find out where the
org.jdom.input.SAXBuilder
class is being loaded from download JWhich and use that to check where the class is being loaded from.
Secondly, if you're using maven check that another version of JDOM isn't including it as a dependency, to do this use the mvn dependency:tree command.

Categories

Resources