Overclock.net banner

1 - 5 of 5 Posts

·
Premium Member
Joined
·
8,165 Posts
Discussion Starter · #1 ·
Hello peeps!

I started learning Java a couple of weeks ago, at the moment I'm trying to create a pretty simple program with a login screen. I have one main class with various tabs and panes then a separate class for a login screen. When the login is correct, two tabs should be added to the main but I get a NullPointerException error and I have no idea why... here's the code:
Code:

Code:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.effect.Reflection;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class Assignment1 extends Application {
        public static void main(String[] args) {
                launch (args);
        }

        Scene scene;
        TabPane root;
        Tab tab1, tab2, tab3, tab4, tab5, tab6;
        Button loginButton, startButton;

        @Override
        public void start(Stage stage) throws Exception {
                stage.setTitle("BBC Assignment 1 - Andrew Moore");
                TabPane root = new TabPane();
                scene = new Scene(root,800, 600, Color.WHITE);
                stage.setScene(scene);

                root.setSide(Side.BOTTOM);

                Pane pane1 = new Pane();
                GridPane pane2 = new GridPane();
                pane2.setAlignment(Pos.CENTER);
        pane2.setHgap(15);
        pane2.setVgap(15);
        pane2.setPadding(new Insets(25, 25, 25, 25));

                Image home = new Image("home149.png");
                ImageView homeView = new ImageView(home);
                homeView.setFitWidth(16);
                homeView.setPreserveRatio(true);
                homeView.setSmooth(true);
                homeView.setCache(true);

                Image cross = new Image("warning37.png");
                ImageView crossView1 = new ImageView(cross);
                crossView1.setFitWidth(16);
                crossView1.setPreserveRatio(true);
                crossView1.setSmooth(true);
                crossView1.setCache(true);
                ImageView crossView2 = new ImageView(cross);
                crossView2.setFitWidth(16);
                crossView2.setPreserveRatio(true);
                crossView2.setSmooth(true);
                crossView2.setCache(true);

                Image tick = new Image("tick7.png");
                ImageView tickView = new ImageView(tick);
                tickView.setFitWidth(16);
                tickView.setPreserveRatio(true);
                tickView.setSmooth(true);
                tickView.setCache(true);

                Image done = new Image("mark1.png");
                ImageView doneView = new ImageView(done);
                doneView.setFitWidth(16);
                doneView.setPreserveRatio(true);
                doneView.setSmooth(true);
                doneView.setCache(true);

                Tab tab1 = new Tab();
                tab1.setText("Welcome");
                tab1.setClosable(false);
                tab1.setGraphic(homeView);
                tab1.setContent(pane1);

                Tab tab2 = new Tab();
                tab2.setText("Your Information");
                tab2.setClosable(false);
                tab2.setGraphic(crossView1);
                tab2.setContent(pane2);

                Tab tab3 = new Tab();
                tab3.setText("Your Opinion");
                tab3.setClosable(false);
                tab3.setGraphic(crossView2);

                Tab tab4 = new Tab();
                tab4.setText("Finished");
                tab4.setClosable(false);
                tab4.setGraphic(doneView);

                Tab tab5 = new Tab();
                tab5.setText("Survey Database");
                tab5.setClosable(false);

                Tab tab6 = new Tab();
                tab6.setText("Statistics");
                tab6.setClosable(false);

                root.getTabs().add(tab1);

                Image logo = new Image("aeolus_logo.png");
                ImageView logoView = new ImageView(logo);
                logoView.setLayoutX(200);
                logoView.setLayoutY(100);
                logoView.setCache(true);
                logoView.setSmooth(true);
                Reflection logoRef = new Reflection();
                logoRef.setFraction(0.9);
                logoView.setEffect(logoRef);

                Image sculpture = new Image("Canary_Wharf_Aeolus_sculpture_2.jpg");
                ImageView sculptureView = new ImageView(sculpture);
                sculptureView.setFitWidth(800);
                sculptureView.setPreserveRatio(true);
                sculptureView.setSmooth(true);
                sculptureView.setCache(true);
                sculptureView.setOpacity(0.1);

                loginButton = new Button();
                loginButton.setText("Staff Login");
                loginButton.setLayoutX(700);
                loginButton.setLayoutY(25);

                EventHandler<ActionEvent> lButton = new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent e) {
                                new StaffPage(Assignment1.this);
                        }
                };

                loginButton.setOnAction(lButton);
                stage.show();
        }
}
Code:

Code:
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class StaffPage extends Assignment1 {

        Assignment1 parent;
        String user = "Staff";
        String password = "password";
        String checkUser, checkPassword;

        public StaffPage(Assignment1 parent)
        {
                Stage dialogStage = new Stage();
                this.parent=parent;
                dialogStage.initStyle(StageStyle.UTILITY);
                dialogStage.initModality(Modality.APPLICATION_MODAL);
                GridPane grid = new GridPane();
                Scene scene = new Scene(grid, 300, 275);
        dialogStage.setScene(scene);
                dialogStage.setTitle("Staff Login");

        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));

        Image logo = new Image("aeolus_logo.png");
        ImageView logoView = new ImageView(logo);
        logoView.setFitWidth(225);
                logoView.setPreserveRatio(true);
                logoView.setCache(true);
                logoView.setSmooth(true);
                grid.add(logoView,0,0,2,1);

        Label userName = new Label("Username:");
        grid.add(userName, 0, 2);

        TextField userTextField = new TextField();
        userTextField.setText("Staff");
        grid.add(userTextField, 1, 2);

        Label passwordLabel = new Label("Password:");
        grid.add(passwordLabel, 0, 3);

        PasswordField passwordBox = new PasswordField();
        passwordBox.setPromptText ("Password");
        grid.add(passwordBox, 1, 3);

        Button button = new Button("Sign in");
        button.setDefaultButton(true);
        HBox boxButton = new HBox(10);
        boxButton.setAlignment(Pos.BOTTOM_RIGHT);
        boxButton.getChildren().add(button);
        grid.add(boxButton, 1, 5);

        Text incorrectText = new Text();
        grid.add(incorrectText, 1, 7);

        EventHandler<ActionEvent> lButton = new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent e) {
                        checkUser = userTextField.getText().toString();
                        checkPassword = passwordBox.getText().toString();
                        if (checkUser.equals(user) && checkPassword.equals(password)) {
                                dialogStage.close();
                                root.getTabs().addAll(tab5, tab6);
                                root.getSelectionModel().select(tab5);
                        }
                        else { 
                                incorrectText.setFill(Color.RED);
                                incorrectText.setText("Password incorrect");

                        }
                }};

        button.setOnAction(lButton);

        Button quitButton = new Button("Cancel");
        quitButton.setCancelButton(true);
        HBox boxQuit = new HBox(10);
        boxQuit.setAlignment(Pos.BOTTOM_LEFT);
        boxQuit.getChildren().add(quitButton);
        grid.add(boxQuit, 0, 5);

        quitButton.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle (ActionEvent e) {
                        dialogStage.close();
                }
        });

        dialogStage.show();

        }
}

The error is on line 83 of the login screen

Code:

Code:
root.getTabs().addAll(tab5, tab6);
Thank you in advance to anyone who can help!
 

·
Registered
Joined
·
740 Posts
Do you have a addall function?

I program in C++ so I might not get it right. It seems like your telling it do the function which hasnt been created yet.
 

·
Premium Member
Joined
·
8,165 Posts
Discussion Starter · #3 ·
Quote:
Originally Posted by Said Nobody View Post

Do you have a addall function?

I program in C++ so I might not get it right. It seems like your telling it do the function which hasnt been created yet.
There is an addAll function
smile.gif


If I put it in the main class it works, so there is an issue with it being in a separate class. Being new to programming I have no idea what the issue is.
 

·
Registered
Joined
·
396 Posts
Quote:
Originally Posted by Corruption View Post

Try this...

in your Main class, change

Code:

Code:
TabPane root = new TabPane()
to

Code:

Code:
root = new TabPane()
This.

You should be able to add a warning/error for variable hiding in your ide to avoid mistakes like this.

In eclipse it's under Preferences->Java->Compiler->Errors/Warnings -> Name shadowing and conflicts

You would see something like this:

 
1 - 5 of 5 Posts
Top