0

I've been tasked to create a campusmap with my universities' campus as the background stage, but everytime I tried to run it, an exception error pops up. This has been stressing me out because I've been stuck for hours on end. Did I set the BackgroundImage parameter wrong? I'm only a beginner here so I apologize if its quite simple to fix. Here is my code.

package campusmap;

import java.io.FileInputStream;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

/**
 *
 * @author Admin
 */
public class CampusMap extends Application {
    
    
    
    @Override
    public void start(Stage primaryStage) {
        Button btn1 = new Button();
        Button btn2 = new Button();
        btn1.setText("Say 'Hello World'");
        btn2.setText("'Heh'");
        
        
        btn1.setLayoutY(100);
        
        btn1.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        
        Rectangle r = new Rectangle(20, 20, 200, 100);
        r.setFill(Color.ANTIQUEWHITE);
        r.setStroke(Color.BLACK);
        r.setStrokeWidth(1);
        r.setX(0);
        r.setY(0);
        
        Rectangle r2 = new Rectangle(20, 20, 100, 500);
        r2.setFill(Color.ANTIQUEWHITE);
        r2.setStroke(Color.BLACK);
        r2.setStrokeWidth(1);
        r2.setX(200);
        r2.setY(-50);
        
        Rectangle r3 = new Rectangle(20, 20, 100, 500);
        r3.setFill(Color.ANTIQUEWHITE);
        r3.setStroke(Color.BLACK);
        r3.setStrokeWidth(1);
        r3.setX(200);
        r3.setY(700);
        
        Rectangle r4 = new Rectangle(20, 20, 200, 300);
        r4.setFill(Color.ANTIQUEWHITE);
        r4.setStroke(Color.BLACK);
        r4.setStrokeWidth(1);
        r4.setX(400);
        r4.setY(0);
        
        Rectangle r5 = new Rectangle(20, 20, 200, 300);
        r5.setFill(Color.ANTIQUEWHITE);
        r5.setStroke(Color.BLACK);
        r5.setStrokeWidth(1);
        r5.setX(400);
        r5.setY(800);
        

        
        
        Image background = new Image(getClass().getResourceAsStream("images/UCcampus.png"));
        ImageView backgroundView = new ImageView(background);
        backgroundView.setPreserveRatio(true);
        
        
        
        
        
        
        
        
        Group root = new Group();
        root.getChildren().add(r);
        root.getChildren().add(r2);
        root.getChildren().add(r3);
        root.getChildren().add(r4);
        root.getChildren().add(r5);
        root.getChildren().add(r6);
        root.getChildren().add(txt);
        root.getChildren().add(txt2);
        root.getChildren().add(txt3);
        root.getChildren().add(txt4);
        root.getChildren().add(c);
        root.getChildren().add(line);
        root.getChildren().add(line2);
        root.getChildren().add(line3);
        root.getChildren().add(line4);
        root.getChildren().add(backgroundView);
      
        
        
        
        
        
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
 
    }

Stack trace:

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: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:873)
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(Thread.java:748)
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 campusmap.CampusMap.start(CampusMap.java:159)
    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:185)
    ... 1 more
Exception running application campusmap.CampusMap
C:\Users\Admin\Documents\NetBeansProjects\CampusMap\nbproject\build-impl.xml:1052: The following error occurred while executing this line:
C:\Users\Admin\Documents\NetBeansProjects\CampusMap\nbproject\build-impl.xml:806: Java returned: 1
BUILD FAILED (total time: 1 second)
        
James_D
  • 201,275
  • 16
  • 291
  • 322
Zawny222
  • 11
  • 3

0 Answers0