0

My JavaFX project structure is:

src
- main
  - java
    - Second
      - Second.java
      - SecondView.java
      - SecondViewModel.java
    - First.java
    - FirstView.java
    - FirstViewModel.java
...
  - resources
    - fxml
      - First.fxml
      - Second.fxml

I got a getter-function for the primarystage in my First.java file. When I try to access it from SecondView.java I'm getting the error Can't resolve symbol.

When I move the SecondView.java into the java root package, so that they're just on the same level, there is no problem at all.

First.java

import javafx.application.Application;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.sql.SQLException;

public class First extends Application {
    private static Logger LOG = LogManager.getLogger(First.class);
    private static Stage primaryStage;

    public static Stage getPrimaryStage() {
        return primaryStage;
    }

    public static void setPrimaryStage(Stage oStage) {
        this.primaryStage = oStage;
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws SQLException, IOException, ClassNotFoundException {
        FirstView.initMainWindow();
    }
}

SecondView.java

package Second;

import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import sun.applet.Main;
import util.DatabaseHandler;
import util.I18N;
import util.StageHandler;
import util.Util;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class SecondView {
    private static Logger LOG = LogManager.getLogger(SecondView.class);

    public static void initSecondWindow() throws IOException, SQLException, ClassNotFoundException {
        LOG.debug("initSecondWindow start...");

        Stage primaryStage = First.getPrimaryStage();
        ResourceBundle bundle = ResourceBundle.getBundle("i18n.myproject", I18N.getLocale());

        FXMLLoader fxmlLoader = new FXMLLoader(SecondView.class.getResource("fxml/Second.fxml"), bundle);
        Parent parentRoot = fxmlLoader.load();
        Scene rootScene = new Scene(parentRoot);
        primaryStage.setScene(rootScene);
        primaryStage.setTitle(Util.getConfValue("conf/general.properties", "application.title"));
        primaryStage.show();

        StageHandler.addStage("gui_Second", primaryStage);

        DatabaseHandler.dbConnect();

        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                StageHandler.delStage("gui_Second");
            }
        });
        LOG.debug("...finished.");
    }
}

Update Solution provided by @Jamed_D in his comment and linking to the possible duplicate.

Yaerox
  • 608
  • 2
  • 11
  • 27
  • 2
    java uses instead of directories ***packages*** can we instead get that info? – ΦXocę 웃 Пepeúpa ツ May 23 '17 at 11:47
  • its is a access modifier issue i feel – Rahul Singh May 23 '17 at 11:48
  • Getter and setter are just normal methods . They can be static or not . The only constraint is , do not use non-static filed and method in the static method. As static method and static filed belong to a class ,and non-static method and field belong to the object – nuriselcuk May 23 '17 at 11:48
  • @RahulSingh public class, private variable, public getter. Any hints regards your feeling? @nuriselcuk I'm using static because I want to have them belonging to the class isntead of the object. I'm calling the getter on my `Second.java` like `Stage myStage = First.getPrimaryStage();` – Yaerox May 23 '17 at 12:02
  • the static var dosent make sense here either you keep it public static or remove static i feel . – Rahul Singh May 23 '17 at 12:04
  • Which symbol can it not resolve? – James_D May 23 '17 at 12:06
  • @James_D `Stage myStage = First.getPrimaryStage();` - Can't resolve symbol`First` – Yaerox May 23 '17 at 12:10
  • That's where? In `Second.java`? Can you post the code for `Second.java`, including `import` and `package` statements? – James_D May 23 '17 at 12:11
  • @Rahul Singh I believe I got your point, but unfortunate it won't resolv the problem. – Yaerox May 23 '17 at 12:20
  • @RahulSingh Sorry: but what you are saying here makes no sense at all. – James_D May 23 '17 at 12:27
  • @James_D he might not have imported the classes and without seeing the class b this was what i felt – Rahul Singh May 23 '17 at 12:28
  • @RahulSingh Please don't comment with "what you feel". If you *know* the answer, post it. If you are making blind guesses, those usually are not helpful. You can always write some code to test your blind guesses and see if they are correct, then post something if you are right. – James_D May 23 '17 at 12:30
  • 1
    You're trying to refer to a class in the default package (i.e. the package with no name) from a class in a named package: there is no way to do this in Java. You should not use the default package: move the classes in the default package into another named package (e.g. `first`) (and then you can import them in the usual way: `import first.First ;`). – James_D May 23 '17 at 12:32
  • @James_D but i feel its is right as you stated in your comment that he is using a default package right this is what i wanted to express. – Rahul Singh May 23 '17 at 12:37
  • @RahulSingh That is not what your comment said *at all*. None of your comments were about packages: you simply made verifiably false statements about using access modifiers and `static`. – James_D May 23 '17 at 12:40
  • okay @James_D i dnt wanna get into a argument regarding the same its a community for helping programmers i feel . i tried helping always someone with a known cant help right i tried if i could – Rahul Singh May 23 '17 at 12:42

0 Answers0