I'm sort of a newbie at programming so I apologize if this is sort of a newbie question! I've been exposed to Java somewhat but I'm completely new at JavaFX. I'm trying to write a program that will open a window and display 3 random card images from a deck of 52 cards on a GridPane and I can't seem to figure out why I keep getting InvocationTargetException, RuntimeException, and ArrayIndexOutOfBoundsException. Should I include try and catch blocks, with these exceptions? Or would there be other exceptions I should be including? Or could it be the location of my image files? My current location of all 52 card image files is: C:\Users\Asus\Documents\NetBeansProjects\AdvancedJavaClass\src\Cards
The following is my code:
package Cards;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.util.Random;
public class ThreeCards extends Application {
#Override
public void start(Stage primaryStage) {
Image[] Cards = new Image[51];
ImageView firstCard = new ImageView();
ImageView secondCard = new ImageView();
ImageView thirdCard = new ImageView();
Random cardPicker = new Random();
int card1 = 0;
int card2 = 0;
int card3 = 0;
for(int i=1; i<53; i++) {
Cards[i-1] = new Image("file: "+i+".png");
}
if (card1 == card2 || card2 == card3 || card3 == card1) {
card1 = cardPicker.nextInt(51)+0;
card2 = cardPicker.nextInt(51)+0;
card3 = cardPicker.nextInt(51)+0;
}
firstCard.setImage(Cards[card1]);
secondCard.setImage(Cards[card2]);
thirdCard.setImage(Cards[card3]);
GridPane root = new GridPane();
root.getChildren().add(firstCard);
root.getChildren().add(secondCard);
root.getChildren().add(thirdCard);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Three Cards");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
And the following is what happens/what I get when I run it:
Executing C:\Users\Asus\Documents\NetBeansProjects\AdvancedJavaClass\dist\run2133970792\AdvancedJavaClass.jar using platform C:\Program Files\Java\jdk1.8.0_71\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 51
at Cards.ThreeCards.start(ThreeCards.java:34)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Exception running application Cards.ThreeCards
Java Result: 1
The IDE I am currently using is Netbeans.
Please help! I can't figure out why I'm getting these errors! Thank you!!
Change this:
for(int i=1; i<53; i++) {
Cards[i-1] = new Image("file: "+i+".png");
}
To:
for(int i=0; i<51; i++) {
Cards[i] = new Image("file: "+ String.valueOf(i+1) +".png");
}
You are over-indexing your array (Cards[51] does not exist).
Related
I wanted to do a test drive on JavaFx, and tried to create an app that can divide two values, but whenever i run it, it gives TextField output to be "".
Here's my code:
package com.example.javafxprojecttest;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import static java.lang.System.*;
public class Divide extends Application {
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("Divide values");
Label divisionVal1 = new Label("Dividend : ");
TextField dividend = new TextField();
TextField divisor = new TextField();
Label divisionVal2 = new Label("Divisor : ");
Button divide = new Button("Divide");
String dividendString = dividend.getText();
String divisorString = divisor.getText();
double d = Double.parseDouble(dividendString) / Double.parseDouble(divisorString);
divide.setOnAction(e -> {
out.println(d);
}
);
GridPane root = new GridPane();
root.addRow(0, divisionVal1, dividend);
root.addRow(1, divisionVal2, divisor);
root.addRow(2, divide);
Scene scene = new Scene(root, 400, 400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Output :
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NumberFormatException: empty String
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:651)
at com.example.javafxprojecttest/com.example.javafxprojecttest.Add.start(Add.java:24)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Exception running application com.example.javafxprojecttest.Add
Process finished with exit code 1
I dont know how to fix this nor do I know wht I have to add with this question for you to be able to answer it.
I tried to look at a lot of other questions here on StackOverflow but found none to be useful to me, and I am also rather new to StackOverflow(asking questions), so I dont know much on documenting the error.
Well i got it myself by just transferring all that to inside the button action.
public void start(Stage stage) throws Exception {
stage.setTitle("Divide values");
Label divisionVal1 = new Label("Dividend : ");
TextField dividend = new TextField();
TextField divisor = new TextField();
Label divisionVal2 = new Label("Divisor : ");
Button divide = new Button("Divide");
divide.setOnAction(e -> {
String dividendString = dividend.getText();
String divisorString = divisor.getText();
double d = Double.parseDouble(dividendString) / Double.parseDouble(divisorString);
out.println(d);
}
);
edit: I'm not sure how to get the stacktrace. Here's the error I get:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFTis already inside a scene-graph and cannot be set as root
at javafx.scene.Scene$9.invalidated(Scene.java:1100)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.Scene.setRoot(Scene.java:1072)
at javafx.scene.Scene.<init>(Scene.java:347)
at javafx.scene.Scene.<init>(Scene.java:223)
at application.Main.start(Main.java:108)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
... 1 more
Exception running application application.Main
I have an assignment where I need to read in information and create a graph from it. It also needs to automatically resize a graph when the user drags the window. I've got the graph working but can't figure out how to get it resize.
I found a different thread that used something like this:
StackPane root = new StackPane(pane);
root.setAlignment(Pos.TOP_LEFT);
// use gridPane size to determine the factor to scale by
NumberBinding maxScale = Bindings.min(root.widthProperty().divide(350),
root.heightProperty().divide(100));
pane.scaleXProperty().bind(maxScale);
pane.scaleYProperty().bind(maxScale);
but that gave me a runtime error.
package application;
import javafx.scene.control.Label;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.NumberBinding;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws FileNotFoundException {
GridPane pane = new GridPane();
pane.setMinSize(400, 500);
pane.setMaxSize(350, 10000);
StackPane root = new StackPane(pane);
ArrayList <Team> aT = new ArrayList <Team>();
Scanner scannerIOInput = null;
FileInputStream fis = null;
BorderPane root2 = new BorderPane();
try{
fis = new FileInputStream(new File("mp3_hockey_stats.txt"));
scannerIOInput = new Scanner(fis);
// System.out.println("File is read in");
String c = null;
int indx;
//read in data
for (int i = 0; i < 7; i++){
c = scannerIOInput.nextLine();
indx = c.indexOf(",");
if (indx > 0)
{
String name = c.substring(0, indx);
String s1 = c.substring(indx + 1);
int stat = Integer.parseInt(s1);
aT.add(new Team (name, stat));
}
}
//print data
int recX, recY, recWidth, recHeight;
recX = 10;
recY = 10;
recWidth = 10;
recHeight = 10;
for (int i = 0; i < aT.size(); i++)
{
Label name = new Label (aT.get(i).teamName1);
Rectangle r = new Rectangle();
r.setX(recX);
r.setY(recY + (i * 100));
r.setWidth(aT.get(i).stats1);
r.setHeight(recHeight);
r.setFill(Color.BLUEVIOLET);
pane.add(name, 1, i);
pane.add(r, 2, i);
//pane.add(" ", 1, i+1);
}
Scene scene = new Scene(pane,350, 100);
primaryStage.setScene(scene);
primaryStage.setTitle("Hockey Statistics Chart");
primaryStage.show();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} catch (NullPointerException e){
System.out.println("NullPointerException.");
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBoundsException.");
}catch (RuntimeException e){
System.out.println("Runtime exception.");
} finally {
scannerIOInput.close();
}
}
public static void main(String[] args) {
launch(args);
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am trying to make a basic game in which a dot is moved randomly around the screen, and when it is clicked, the user gets some points. When I run the program, I get a NullPointerException at the start method. I am wondering what I am doing wrong as well as any types for improving the program. Thank you!
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class CatchTheDot extends Application{
//create ball for game
public static Circle dot;
//create pane to run game
public static Pane window;
//create score counter
int score = 0;
//creat random
Random random = new Random();
#Override
public void start(Stage primaryStage) throws Exception {
window.getChildren().add(dot);
// create scene and place on pane
Scene s = new Scene(window, 800, 800);
primaryStage.setTitle("Catch The Dot");
primaryStage.setScene(s);
primaryStage.show();
//move dot
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
#Override
public void run(){
window.getChildren().remove(dot);
int ranX = random.nextInt(800-1); // random value from 0 to width
int ranY = random.nextInt(800-1); // random value from 0 to height
window.getChildren().add(ranY, dot);
}
}, 0, 5000);
// create listener
dot.setOnMouseClicked(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent e){
if(e.getSource()==dot)
{
score = score + 10;
if(score == 50)
{
popUp(primaryStage);
}
}
}
});
}
public void popUp(final Stage primaryStage)
{
primaryStage.setTitle("You won!");
final Popup popup = new Popup();
popup.setX(300);
popup.setY(200);
Text t = new Text("You won! Nice job!");
Text tt = new Text("Play again?");
Button yes = new Button("yes");
Button no = new Button("no");
popup.getContent().addAll(t, tt, yes, no);
yes.setOnAction(e -> Yes());
no.setOnAction(e -> No());
}
public void Yes()
{
restartGame();
}
public void No()
{
System.exit(0);
}
public void restartGame()
{
score = 0;
}
public static void main(String[] args)
{
launch(args);
}
}
Error log:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at CatchTheDot.start(CatchTheDot.java:34)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application CatchTheDot
Window and Dot have been declared but not defined
In line 34, as the trace says:
window.getChildren().add(dot);
either window or dot (or both) are not initialized by then. Hence the null pointer exception.
This is my code that I have so far in my Netbeans program. It is supposed to bring up a window with a picture in it, the picture being called seawave. But for some reason it is not running. Do you have any idea why?
package myseaapp;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class MySeaApp extends Application {
private ImageView seawave;
private Rectangle seawaveClip;
#Override
public void start(Stage primaryStage) {
primaryStage.initStyle(StageStyle.TRANSPARENT);
seawave = new ImageView(new Image(MySeaApp.class.getResourceAsStream("images/seawave")));
seawaveClip = new Rectangle(300, 220);
seawaveClip.setArcHeight(20);
seawaveClip.setArcWidth(20);
seawave.setClip(seawaveClip);
Pane root = new Pane();
root.getChildren().add(seawave);
Scene myScene = new Scene(root, 300, 250);
myScene.setFill(null);
primaryStage.setScene(myScene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
and this is what I get...
Executing /Users/SethRataiczak/NetBeansProjects/MySeaApp/dist/run1104410607/MySeaApp.jar using platform /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Image.java:1110)
at javafx.scene.image.Image.<init>(Image.java:694)
at myseaapp.MySeaApp.start(MySeaApp.java:30)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application myseaapp.MySeaApp
Java Result: 1
Deleting directory /Users/SethRataiczak/NetBeansProjects/MySeaApp/dist/run1104410607
jfxsa-run:
BUILD SUCCESSFUL (total time: 1 second)
First of All use try and catch block for error handling like this
try {
// code
}catch {
// error handling code
}
and second you did not use your image type example .png, .jpg etc
seawave = new ImageView(new Image(MySeaApp.class.getResourceAsStream("images/seawave")));
change this line of code with correct image type
Like this:
seawave = new ImageView(new Image(MySeaApp.class.getResourceAsStream("images/seawave.jpg")));
Problem:
everything was working fine until I decided to add the media with the mediaPlayer into the program. The reason I know the mediaPlayer is the culprit is because I set the function that runs the mediaPlayer to the end of the main function and the screen would then appear for second and then vanish as oppose to not running at all if I set the function anywhere else.
Relevant code that is causing the problem
private void playThemeIntro()
{
Media gameIntroTheme = new Media("GameIntroTheme.MP3");
mediaPlayer = new MediaPlayer(gameIntroTheme);
mediaPlayer.setAutoPlay(true);
}
Entire code
package whowantstobeamillionairetriviagame;
import java.io.File;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class WhoWantsToBeAMillionaireTriviaGame extends Application
{
private VBox menuLayout;
private MediaPlayer mediaPlayer;
#Override
public void start(Stage startingStage) throws Exception
{
StackPane backgroundSettings = new StackPane();
Image backgroundColor = new Image("http://1.bp.blogspot.com/-p0s06MBIx_U/T8zKIBZ24pI/AAAAAAAAA7Y/n8hMZfpRic0/s1600/dark+blue+wallpaper+10.jpg");
ImageView background = new ImageView();
background.setImage(backgroundColor);
createMenuLayout();
backgroundSettings.getChildren().addAll(background, menuLayout);
playThemeIntro(); // If you comment this out, the program works properly
Scene backgroundScene = new Scene(backgroundSettings);
startingStage.setScene(backgroundScene);
startingStage.setTitle("Who Wants to be a Millionaire");
startingStage.show();
}
private void playThemeIntro()
{
Media gameIntroTheme = new Media("GameIntroTheme.MP3");
mediaPlayer = new MediaPlayer(gameIntroTheme);
mediaPlayer.setAutoPlay(true);
}
private VBox createMenuLayout()
{
menuLayout = new VBox();
menuLayout.setSpacing(20);
menuLayout.setAlignment(Pos.TOP_CENTER);
Image millionaireLogo = new Image(new File("MillionaireLogo1.PNG").toURI().toString());
ImageView logoPicture = new ImageView();
logoPicture.setImage(millionaireLogo);
logoPicture.setPreserveRatio(true);
logoPicture.setSmooth(true);
logoPicture.setCache(true);
menuLayout.getChildren().add(logoPicture);
Button menuButtons[] = new Button[]
{
new Button("Play"),
new Button("Options"),
new Button("Help"),
new Button("Exit")
};
for (int i = 0; i < 4; i++)
{
menuButtons[i].setPrefSize(200, 30);
Rectangle r = new Rectangle(200, 30, Paint.valueOf("346699"));
r.setArcHeight(30);
r.setArcWidth(30);
menuButtons[i].setOnMouseEntered(e -> r.setFill(Paint.valueOf("0f69b4")));
menuButtons[i].setOnMouseExited(e -> r.setFill(Paint.valueOf("346699")));
menuButtons[i].setBackground(Background.EMPTY);
menuButtons[i].setTextFill(Paint.valueOf("White"));
menuButtons[i].setFont(Font.font("Serif", FontWeight.BOLD, 16));
VBox.setMargin(menuButtons[i], new Insets(0, 0, 0, 8));
VBox.setVgrow(menuButtons[i], Priority.ALWAYS);
StackPane sp = new StackPane();
sp.getChildren().addAll(r, menuButtons[i]);
menuLayout.getChildren().add(sp);
}
return menuLayout;
}
public static void main(String[] args)
{
launch(args);
}
}
Error that I got
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1642360923.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'GameIntroTheme.MP3'
at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:211)
at javafx.scene.media.Media.<init>(Media.java:391)
at whowantstobeamillionairetriviagame.WhoWantsToBeAMillionaireTriviaGame.playThemeIntro(WhoWantsToBeAMillionaireTriviaGame.java:57)
at whowantstobeamillionairetriviagame.WhoWantsToBeAMillionaireTriviaGame.start(WhoWantsToBeAMillionaireTriviaGame.java:46)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/792965399.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/355629945.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/266742917.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
... 1 more
Exception running application whowantstobeamillionairetriviagame.WhoWantsToBeAMillionaireTriviaGame
Java Result: 1
The Media constructor requires a string that represents a URL with a file:, http:, or jar: scheme.
I don't know your project layout, so I can't give a definitive answer, but you probably need something like
Media gameIntroTheme =
new Media(getClass().getResource("GameIntroTheme.MP3").toExternalForm());
Here is the answer
Media gameIntroTheme = new Media(newFile("GameIntroTheme.MP3").toURI().toString());