vendredi 10 novembre 2017

I keep getting this error while my code looks non problematic

So I'm trying to use JavaFX. I'm trying to animate stuff and use BPane with javafx. In this program, i'm trying to animate the Earth moving around the sun. It doesn't give any errors before running but I get this block of errors when I run it. It might because I might have not imported the images correctly, but I'm pretty confident I have.

Exception in Application constructor
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:498)
    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:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class gui.test.Animation
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:819)
    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
Caused by: java.lang.NullPointerException: Input stream must not be null
    at javafx.scene.image.Image.validateInputStream(Image.java:1128)
    at javafx.scene.image.Image.<init>(Image.java:706)
    at gui.test.Animation.<init>(Animation.java:22)
    ... 13 more
Exception running application gui.test.Animation

Here's the code:

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Animation extends Application {
    public int canvasSize = 512;                // constants for relevant sizes
    public double orbitSize = canvasSize / 3;
    public double sunSize = 80;
    public double earthSize = 30;
    public GraphicsContext gc; 
    public Image earth = new Image(getClass().getResourceAsStream("earth.png"));
    public Image sun = new Image(getClass().getResourceAsStream("sun.png"));
            // note loading of images, which should be in package

    /**
     * drawIt ... draws object defined by given image at position and size
     * @param i
     * @param x
     * @param y
     * @param sz
     */
    public void drawIt (Image i, double x, double y, double sz) {
        gc.drawImage(i, x - sz/2, y - sz/2, sz, sz );
    }
    /**
     *  draw system, with Earth at x,y
     * @param x
     * @param y
     */
    private void drawSystem(double x, double y) {
        // now clear canvas and draw sun and moon
        gc.clearRect(0,  0,  canvasSize,  canvasSize);      // clear canvas
        drawIt( sun, canvasSize/2, canvasSize/2, sunSize ); // draw Sun
        drawIt( earth, x, y, earthSize );                   // draw Earth
    }

    /**
     * draw system with Earth at specified angle
     * @param t
     */
    private void drawSystem(double t) {
        double x = canvasSize/2 + orbitSize * Math.cos(t);  // calc x coord
        double y = canvasSize/2 + orbitSize * Math.sin(t);  // and y
        drawSystem(x,y);                                    // and draw system
    }

    /**
     * main function ... sets up canvas and timer
     */
    @Override
    public void start(Stage stagePrimary) throws Exception {
        stagePrimary.setTitle("Solar System");  // set title

        Group root = new Group();               // create group of what is to be shown
        Canvas canvas = new Canvas( canvasSize, canvasSize );
                                                // create canvas to draw on
        root.getChildren().add( canvas );       // add canvas to root
        gc = canvas.getGraphicsContext2D();
                                                // remember context of canvas drawn on
        Scene scene = new Scene( root );        // put root  in a scene
        stagePrimary.setScene( scene );         // apply the scene to the stage

        final long startNanoTime = System.nanoTime();
                                                // for animation, note start time

        new AnimationTimer()                    // create timer
            {
                public void handle(long currentNanoTime) {
                        // define handle for what do at this time
                    double t = (currentNanoTime - startNanoTime) / 1000000000.0; 
                                                // calculate time
                    drawSystem(t);              // draw system with Earth at this time
                }
            }.start();                          // start timer

        stagePrimary.show();                    // show scene
    }

    public static void main(String[] args) {
        Application.launch(args);           // launch the GUI
    }

}





Aucun commentaire:

Enregistrer un commentaire