javafx error even though url is correct - css

Even though the path given is correct & image is displaying in scene builder, it is throwing error while running the application.
Executing C:\Users\433240\Documents\NetBeansProjects\UI\dist\run547088191\UI.jar using platform C:\Program Files (x86)\Java\jdk1.8.0_40\jre/bin/java
Device "Intel(R) G41 Express Chipset" (\\.\DISPLAY1) initialization failed :
WARNING: bad driver version detected, device disabled. Please update your driver to at least version 8.15.10.2302
null/Images/home.png
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1091)
... 23 more
Exception running application ui.Main
Java Result: 1

I had the same problem
Solution:
go to java controller class and write this code
private Image image;
#FXML
ImageView imageview; // type your imageview fixid
private void setImage(String url) {
try {
image = new Image(url);
imageview.setImage(image);
} catch (Exception e) {
System.out.println(e);
}
}

Related

Problem in playing an MP3 file in JavaFX - compatibility issue?

I'm trying to play a small MP3 file or two using JavaFX, trying with MediaPlayer and AudioClip but getting an exception in each case. Some kind of compatibility issue?
Technology stack (1) Windows 10 home (2) Java 8 update 211 (3) Java SE Development kit 13.0.1 (4) JavaFX 11.0.2.
Exception reported :
(1) MediaPlayer (when I create a new Media object)
Exception in thread "JavaFX Application Thread" java.lang.IllegalAccessError: class com.sun.media.jfxmediaimpl.NativeMediaManager (in unnamed module #0x30696d74) cannot access class com.sun.glass.utils.NativeLibLoader (in module javafx.graphics) because module javafx.graphics does not export com.sun.glass.utils to unnamed module #0x30696d74
(2) AudioClip (when I create a new AudioClip object)
Exception in thread "JavaFX Application Thread" java.lang.IllegalAccessError: class com.sun.media.jfxmediaimpl.NativeMediaManager (in unnamed module #0x403bc23b) cannot access class com.sun.glass.utils.NativeLibLoader (in module javafx.graphics) because module javafx.graphics does not export com.sun.glass.utils to unnamed module #0x403bc23b
Code listing :
// play a sound clip using Media
private void playMedia1() {
fullFileName = "short.mp3";
if (checkFileExists(fullFileName)) {
File f = new File(fullFileName);
System.out.println("File URI : " + f.toURI().toString());
Media media = new Media(f.toURI().toString());
MediaPlayer mplayer = new MediaPlayer(media);
mplayer.setAutoPlay(true);
} else {
System.out.printf("File %s not found%n", fullFileName);
}
}
// play a sound clip using AudioClip
private void playMedia2() {
fullFileName = "short.mp3";
if (checkFileExists(fullFileName)) {
File f = new File(fullFileName);
System.out.println("File URI : " + f.toURI().toString());
AudioClip clip1 = new AudioClip(f.toURI().toString());
clip1.play();
} else {
System.out.printf("File %s not found%n", fullFileName);
}
}
// test we can open the specified file
private boolean checkFileExists(String filename) {
File f = new File(filename);
return f.exists();
}

Error while running client code in EJB

Getting error while running client code in EJB:
Exception in thread "Main Thread" java.lang.NoClassDefFoundError:
weblogic/kernel/KernelStatus at
weblogic.jndi.Environment.(Environment.java:78) at
weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:117)
at
javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at
javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223) at
javax.naming.InitialContext.(InitialContext.java:197) at
User.main(User.java:21)
I added wlfullclient.jar, server's Remote interface jar, also I saw article in which I saw to add wlclient.jar and weblogic.jar but then also got same error.
Help highly appreciated.
Client code:
import com.amdocs.Stateful.*;
import java.util.Hashtable;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class User
{
public static void main(String args[]){
Hashtable<String,String> ht = new Hashtable<String,String>();
ht.put(InitialContext.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");// you have to start from root location to search for JNDI tree
//JNDI registry tree is normally maintained as Binary tree, JNDi is normally binary tree which have root node at top
ht.put(InitialContext.PROVIDER_URL,"t3://localhost:7001"); // address of registry
try{
System.out.println("Hello");
InitialContext ic = new InitialContext(ht); // start searching wrt what we set in ht , it's constructor takes HashTable only
System.out.println("Hello2");
MyCartRemote ref=(MyCartRemote)ic.lookup("MyCartBeanJNDI#com.amdocs.Stateful.MyCartRemote");
ref.add("Hi");
ref.add("Jeetendra");
MyCartRemote ref1=(MyCartRemote)ic.lookup("MyCartBeanJNDI#com.amdocs.Stateful.MyCartRemote");
ref1.add("Hi");
ref1.add("Subhash");
ref1.add("Ghai");
System.out.println("Object 1 data: "+ref.show());
System.out.println("Object 2 data: "+ref1.show());
}
catch (NamingException e){ e.printStackTrace(); }
}
}
Try removing WebLogic System Libraries from the Bootstrap Entries section. It worked for me when i got the same error.

Error loading fxml files from a folder other than the bin folder

I am a fairly new java programmer. I only have about five weeks of experience, starting from zero, and I am having problems getting javafx fxml files created in Scene Builder to load correctly if they are not in the same folder as the controller classes.
I am using
Win7x64 running jre7x86 for this build
Eclipse Juno Service Release 1
Build id: 20120920-0800
jre version 1.7.0_07
javaFx version 2.2.1-b03
SceneBuilder version 1.0-b50
My scene loader code is
private static final String RESOURCE_PATH = "/resources/";
public static Stage buildStage(#SuppressWarnings("rawtypes") Class pClass,
String stageTitle, String resourceLocation)
{
Stage temp = new Stage();
Pane page = null;
try
{
page = FXMLLoader.load(pClass.getResource(RESOURCE_PATH + resourceLocation), null,
new JavaFXBuilderFactory());
}
catch (IOException e)
{
e.printStackTrace();
}
Scene scene = new Scene(page);
temp.setScene(scene);
temp.setTitle(stageTitle);
temp.setResizable(false);
return temp;
}
I know that the project directory is the /workspace/projectName/ so theoretically the loader should be able to find the files if it's given a relative path right? The error I'm getting is
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2737)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)
at outofunit.system.StageFactory.buildStage(StageFactory.java:32)
at outofunit.desktop.ArcMaster.start(ArcMaster.java:28)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
... 1 more
I've tried giving the loader an absolute path as well but it just isn't accepting anything that isn't directly in the same directory. I'm grasping at straws here trying to figure this out.
I've tried to research this on my own but I haven't gotten much, the few answers on here that seemed similar didn't seem to help, that or I am too dense and/or inexperienced to make sense of them. They are JavaFX 2.0 loading fxml files with event handlers fails and JavaFX 2.0 FXML resource loading error
A buddy of mine was able to overcome this problem by using this code
public void load(String pFileName, Stage pStage, String pTitle)
{
String fName = RESOURCE_PATH + pFileName;
try
{
String externalForm = getClass().getResource(fName)
.toExternalForm();
InputStream inStream = new URL(externalForm).openStream();
FXMLLoader loader = new FXMLLoader();
Pane p = (Pane)loader.load(inStream);
pStage.setTitle(pTitle);
pStage.setScene(new Scene(p));
mWindowControl = loader.getController();
mWindowControl.setUp(pStage);
pStage.show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
The biggest difference and the reason why I haven't been using his code is because my root pane is an Anchor pane instead of a normal pane, and his way URL instream forces a normal pane. Am I at fault for not using a normal pane as my base? I would love any help or input you guys can give. Thank you.
Edit: So I've been working on this problem and the error message has changed.
I changed the code to this
private final String RESOURCE_PATH = "resources/";
public void load(String pFileName, Stage pStage, String pTitle)
{
String fName = RESOURCE_PATH + pFileName;
Pane page = null;
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fName));
try
{
page = (Pane) fxmlLoader.load();
}
catch (IOException exception)
{
throw new RuntimeException(exception);
}
Scene scene = new Scene(page);
pStage.setScene(scene);
pStage.setTitle(pTitle);
mWindowControl = fxmlLoader.getController();
mWindowControl.setUp(pStage);
pStage.show();
}
but the error I'm now getting is
java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2021)
at outofunit.desktop.WindowLoader.load(WindowLoader.java:136)
at outofunit.desktop.WindowLoader.load(WindowLoader.java:62)
at outofunit.desktop.ArcMaster.start(ArcMaster.java:30)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Unknown Source)
I don't understand how the location is not being set
It's a path issue, java needs to know where to get the file from the package hierarchy.
So using "/package1/resourcename.fxml" should find the source from the root of the tree.
You normally leave off the "/" at the start as you are already at the top of the package tree.
Both exceptions say "the (FXML) file you are trying to load cannot be found in the location you have provided". Your filename is wrong or its path. Give your package structure or at least the path of class file that includes load(String pFileName, Stage pStage, String pTitle) method and the path of FXML file you want to load. Read the javadoc API of getResource() and examine sample codes about it on the net.

Problems with javaFX building path MVC fxml

I am currently working on an administrator build in java FX 2.1 using netbeans 7.2
I have the following issues:
I am developing this particular tool in an MVC pattern, so I've created 3 packages called Model, view and Controller.
My problem is that when building the project in netbeans it would only read the files supposed to be in the view package if they're outside of it. Let me give you a context path:
.../administradorInfinix/view/
.../administradorInfinix/controller/
.../administradorInfinix/model
so it would only read the fxml files regarding the view if they are outside the view package (.../administradorInfinix/)
This is where I set the address of the file:
private void irInicioSesion() {
try {
replaceSceneContent("InicioSesion.fxml");
} catch (Exception ex) {
Logger.getLogger(AdministradorINFINIX.class.getName()).log(Level.SEVERE, null, ex);
}
}
You can see the file name is InicioSesion.fxml, which should be inside the view package but it won't load if this is the case.
This is the replaceSceneContent I'm using to search for the fxml files:
private Parent replaceSceneContent(String fxml) throws Exception {
Parent page = (Parent) FXMLLoader.load(AdministradorINFINIX.class.getResource(fxml), null, new JavaFXBuilderFactory());
Scene scene = stage.getScene();
if (scene == null) {
scene = new Scene(page,548,416);
//scene.getStylesheets().add(AdministradorINFINIX.class.getResource("demo.css").toExternalForm());
stage.setScene(scene);
} else {
stage.getScene().setRoot(page);
}
stage.sizeToScene();
return page;
}
And this is the error it gives me when trying to run (it builds just fine but it won't run)
> administradorinfinix.AdministradorINFINIX irInicioSesion
Grave: null
java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at administradorinfinix.AdministradorINFINIX.replaceSceneContent(AdministradorINFINIX.java:126)
at administradorinfinix.AdministradorINFINIX.irInicioSesion(AdministradorINFINIX.java:110)
at administradorinfinix.AdministradorINFINIX.start(AdministradorINFINIX.java:46)
at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$3.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:722)
where line 110 is
replaceSceneContent("InicioSesion.fxml");
and line 126 is
Parent page = (Parent) FXMLLoader.load(AdministradorINFINIX.class.getResource(fxml), null, new JavaFXBuilderFactory());
I hope you can help me fix this problem.
You need to call the method FXMLLoader#setLocation with the URL of the FXML file. Have a look at the following source for an example of how to load FXML files:
https://github.com/cathive/fx-guice/blob/master/src/main/java/com/cathive/fx/guice/GuiceFXMLLoader.java
The FXMLLoader fails to locate the .fxml-file.
The problem is that your call to Class.getResource() returns null.
The exception thrown by FXMLLoader.load(null) is quite misleading, it should rather be something such as an ArgumentNullException.
You can fix the problem with loading your resource file by specifying the full package path, in my case a call like this works:
FXMLLoader loader = new FXMLLoader(new Employee().getClass().getResource("/de/mycompany/mypackage/view/loginform.fxml"));
I hope this helps.

Internet data connection fails

My users are reporting that when they download a JSON file using WiFi, the app works like charm, but when they attempt to download the same file using their data connection: GPRS, 3G, 3.5G etc... the app force closes. It doesn't happen with all types of smartphones, e.g, with my LG Optimus Black it doesn't happen.
To retrieve this file I use an AsyncTask.
private class GetData extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... args) {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://url.com);
HttpResponse response = httpclient.execute(httpget);
strPlayers = inputStreamToString(response.getEntity().getContent()).toString();
}
catch (IOException e) { e.printStackTrace(); }
finally { httpclient.getConnectionManager().shutdown(); }
return null;
}
protected void onPostExecute(Void result) {
Players players = new Gson().fromJson(strPlayers, Players.class);
quantity_darkmatter.setText(String.valueOf(players.userName));
}
}
Here's the StackTrace()
java.lang.RuntimeException: Unable to start activity ComponentInfo{carl.fri.fer.omegan/carl.fri.fer.omegan.Research}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at carl.fri.fer.omegan.Research.onStart(Research.java:613)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3791)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1632)
... 11 more
What can be the problem?
For non-WIFI the connection might take considerably more time, so it might be an issue of connection timeout, when that happens strPlayers will be blank and as there is no errorhandling in onPostExecute this might give Gson.fromJson hickups and make it throw an exception... try testing that strPlayers!=null before trying to parse it or putting it in a try-catch block to see if it throws an exception of some sort.
Is there any output of the e.printStackTrace() in the doInBackground function?

Resources