I've spent almost 2 days in trying to load files from inside of my netbeans project, but it always gives NullPointException.
currently my directory looks like:
JavaFXApplication:
src
--Manifest (contains Manifest.java)
--images (inside Manifest package aka Manifest.images)
--server.jpg (inside images package)
I'm trying to load the server.jpg from images package, but it always return NULL.
Here is the snippet of my code:
try {
rect.setFill(new ImagePattern(new Image(Manifest.class.getResourceAsStream("images\\server.jpg"))));
} catch (NullPointerException e) {
System.out.println(Manifest.class.getResourceAsStream("server.jpg"));
}
Exactly 2 days before, I saw this code from a YouTube Tutorial, and it doesn't worked. Try many of those solutions from here, but nothing yield for me.And suddenly it worked. Next day, tried to run the same code, and again same NULL error.
Can you guys please help me. I'm totally new to JavaFX. Don't have much experience with it.
Use getClass().getClassLoader().getResourceAsStream(""). When you do Manifest.class.getResourceAsStream("images\\server.jpg"), it will try to load the file relative to where the Manifest.class is present.
Related
I downloaded the odoo framework from the github, I installed it and locally every thing is working just fine, But it does not sync with the server no records came from the server and none go, It just work locally
I have seen the issues part in the github, but i does't give so much help
Is there any one knows what exaclly the problem?? Or any one have working copy of the framework??
I had the same problem but syncing worked fine after commenting out the some lines out of the file ContentResolver.java: search for lines 2326, 2327, 2328
public static void setMasterSyncAutomatically(boolean sync) {
setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId());
}
change them to:
// public static void setMasterSyncAutomatically(boolean sync) {
// setMasterSyncAutomaticallyAsUser(sync, UserHandle.myUserId());
// }
I know this is not the correct way to do it, because you seem to override the authorisation check, but for me, for now, and for testing purposes it works.
(hope it helps, please don't shoot me)
So I'm making the transition from BlueJ to Eclipse like any good college Freshman would and I can't, for the life of me, Find where to store an image in the workspace which would allow me to access it from my code. any ideas?
Usually, you store your images in a resources folder.
Here's an example from one of my projects.
My images are stored in an images folder, which is included in the Java classpath.
I read them with a method call:
model.setCheckMark(readImage("/check_mark.png"));
And here's the readImage method:
private Image readImage(String streamString) {
try {
return ImageIO.read(getClass().getResourceAsStream(streamString));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Right click on your project on the left side-bar and create a 'New -> Source Folder'. A common mistake and one I made is to simply create a 'New -> Folder'. This wont work and I can relate to anyones frustrations at this and similar hurdles when moving from BlueJ to a proper IDE like Eclipse.
I've been working in a little project to get a bit of practice programming. It's basically done, but I won't be satisfied until I can use images properly, a bit of help would be appreciated.
So, currently I'm using the the getImage method from the ImageIcon class, like so:
Image body = new ImageIcon("C:/Users/Centollo/Documents/NetBeansProjects/Chess/build/classes/chess/img/WhiteBishop.png").getImage();
I've been trying to figure out how to do the same thing without using an absolute path, but I don't know how to make the images a part of the jar so that it works fine in any other machine.
All I need to know is where to put the images and how would the code to access them look like.
Try to explain it like I'm stupid, please. I've read answers to similar questions but I can't make heads or tails of them.
I'm working in NetBeans with a "chess" package with all the .java and a "chess.img" package with all the .png.
If your class extends from JFrame, you can do this:
Image image = new ImageIcon(this.getClass().getResource("/images/MyImage.jpg")).getImage();
If your class extends Applet, you can go this way:
private URL base = null;
private Image myImage = null;
try
{
base = getDocumentBase();
} catch (Exception e)
{
e.printStackTrace();
}
myImage = getImage(base, "images/MyImage.jpg");
A very quick google search yields this:
URL resource = getClass().getClassLoader().getResource( "img/WhiteBishop.png" );
Image body = new ImageIcon( resource );
There are a couple ways to do this but here's the way I would suggest:
Make sure the chess.img folder is in your application's classpath. Then try referring to the path like chess.img/image (yes, you can use forward slash in windows.)
If that doesn't work use:
ChessClass.class.getClassLoader().loadResourceAsStream("/chess.img/image");
Note the forward slash at the beginning of the file reference. This points to the root of the classpath. It's a bit confusing as someone with unix/linux experience might think it refers to the root of the file system. This tends to work better than the other answer given for reasons I knew 10 years ago. This is an ugly bit of Java that was never quite cleaned up.
EDIT: To those idiots who negged this question merely over the title, the button is clearly meant to be pressed if the question is out of the blue, without any effort put into it at all. I have researched and I have asked, and I have tried. All I am asking for is help.
It's hardly necessary to say just how much work I have put into trying to find a solution to my problem - I have asked questions, Googled, read documents, you name it, but all to no avail.
What I want to do is something I thought I could figure out within minutes: How to run images with JApplet, and use these images in the paint(Graphics g) function. I am running the JavaProject.html file from the build (also known as bin) folder, and it is from the file system, not HTTP. I did not include the "package" line in my code as well.
A recap of my journey is that the following have not worked for me:
This is my HTML file:
<html>
<head>
<title> My First Web Page </title>
</head>
<body>
<applet code="JavaProject.class" width="500" height="600">
</applet>
</body>
</html>
This method gives me the "Access Denied" "java.io.FilePermission" "Image.jpg" "read" Error. Needless to say, trying to work with images on a website does not work either. This one is one of the more frustrating ones because it works to with other people, yet not for me.
import java.applet.*;
import java.awt.*;
public class JavaProject extends JApplet
{
Image image;
public void init()
{
image=getImage(getDocumentBase(),"/Space.gif");
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image,20,20,this);
}
}
So that one didn't work. They suggested the getResourceAsStream method, so I tried that.
Image image;
Exception lastException=null;
try
{
image=ImageIO.read(getClass().getResourceAsStream("/Space.gif"));
}
catch (IOException ex)
{
lastException=ex;
}
But this one ended up giving me the "Illegal Argument Exception" input=null! Error.
This is my file arrangement: http://oi61.tinypic.com/5ohydc.jpg
Most other methods do not really work or are just far too complex to write down here, but none seem to work. I ask then, is my last resort just to get this thing signed? I have no idea how to go about doing that, and I think it's ridiculous that I even have to go through the trouble just to display images on my JApplet.
I have really lost all faith, and if this is to be fixed no doubt it will take enormous patience, but I would really appreciate any help. I am new to Java, so I can't really discuss much technically, but I pick up from examples rather quickly.
The first method you attempted does work. The only change you need to make is to remove the slash ( / ) before the file name you are attempting to use. Here's your original code (which works fine) with that one character removed:
import java.applet.*;
import java.awt.*;
public class JavaProject extends JApplet
{
Image image;
public void init()
{
image = getImage(getDocumentBase(),"Space.gif");
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image,20,20,this);
}
}
Note that in order for this to work, your 'space.gif' image must be in the root directory of your project, alongside the Java file that references it, like in this picture:
See how the image is in the 'default package' directory where my JavaProject.java file is also located? Use the code above, make sure your image is in the right place, and your program will run successfully. Here's mine in a running state:
The reason that the second attempt is failing is that it can't find the resource at the path you gave. That causes getResourceAsStream to return null which then results in the exception message that you saw.
You say that the error message in the first case was "Access Denied" "java.io.FilePermission" "Image.jpg" "read" or something. But according to the code, you are not trying to load "Image.jpg". That might be the root of your original problem.
A related issue is that in the getResourceAsStream version, you are using the wrong absolute path. You've used the path "/Space.gif", but given where you have put the file, it should be "/javaproject/Space.gif". Alternatively, since you are calling getResourceAsStream as on a class in the same (javaproject) package that contains the file, you could also use a relative path; i.e. "Space.gif".
I have really lost all faith, and if this is to be fixed no doubt it will take enormous patience ...
Actually, the real solution is to carefully read the documentation for the classes / methods you are using, rather than replying on random examples you found on the internet.
I have a program in Java using LWJGL. It runs fine in Eclipse, but when I try to compile it as a jar file it crashes, giving me a NullPointerException. It's been asked before, I know, but I don't seem to get an answer that works. Any help here? Thanks in advance!
The thing that seems to be having the problem is the class TextureHelper:
public class TextureHelper {
public static Texture LoadTexture(String texture)
{
try {
return TextureLoader.getTexture("PNG", ResPlaceholder.class.getResourceAsStream(texture));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
Some notes:
I've also tried "/res/" + texture, as well as many other things like it.
ResPlaceholder is a class that sits in the res folder where I store all my images. It's a blank empty class.
This works perfectly in Eclipse.
My jar has these folders (just as an example):
foo.jar
----core
--------TextureLoader
----res
-------- Assorted Image Files
-------- ResPlaceholder
This is the same as the packages in eclipse.
Any help you have would be appreciated, I've been stuck on this for days with no progress.
EDIT:
META-INF/MANIFEST.MF
Main.class config/
config/Images.class
core/
core/LevelLoader.class
core/TextureHelper.class
core/TileSet.class
-Skipping some other stuff that has nothing to do with this-
res/
res/ResPlaceholder.class
res/BlankImg.png
res/test.txt
res/testImg.png
res/testTiles.png
If the path String is something like: /res/testImg.png it should work.
i.e.,
String resourcePath = "/res/testImg.png";
InputStream inStream = ResPlaceholder.class.getResourceAsStream(resourcePath );
BufferedImage img = ImageIO.read(inStream);
// use the img BufferedImage here
// for example:
ImageIcon icon = new ImageIcon(img);
JOptionPane.showMessageDialog(null, icon);
As an aside, it's always good to test problems and check new concepts in small test programs. In your case, I would create a small simple program with just a main method that attempts to extract a jar's image resource and display it in an option pane much like my small code above does. I'd try to avoid long lines and instead separate each step on its own line, again similar to my post above. This way if an exception is thrown, the code on the line will be much more informative as to what is causing the error.