JavaFX IllegalAccessException during FXML load()

若如初见. 提交于 2020-01-01 04:18:15

问题


I have a dialog window that is invoked by the following code (DialogController is a helper class for using modal dialog windows; it mainly bundles together a controller reference with its window):

void handleServicesEdit(ActionEvent event) throws IOException {

    DCServRecEditor sre = DialogController.<DCServRecEditor>loadFXML(
            CensusAssistant.RES_FXML_DIALOG_SERVEDIT,
            CensusAssistant.RES_STRING_SERVEDIT,
            this.getDialog());
    sre.setDialogMode(DB.DBEDIT_MODE_EDIT,
                      tbvService.getItems(),
                      tbvService.getSelectionModel().getSelectedIndex(),
                      m_encCal);
    sre.showAndWait();

    sre.release();
    this.updateGUI();
}

I have confirmed that I get an exception during the FXMLLoader.load() method. I have also determined that the error occurs before any code in my initialize() method has a chance to run. Some of the stack trace that I get from load() is here:

java.lang.IllegalAccessException: Class sun.reflect.misc.ReflectUtil 
    can not access a member of class org.kls.md.censusassistant.DCServRecEditor 
    with modifiers ""
file:/D:/Documents/NetBeansProjects/CensusAssistant/dist/run1284250063/CensusAssistant.jar!/org/kls/md/censusassistant/fxml/GUIServRecEditor.fxml:13
  at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:738)
  at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:775)
  at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:180)
  at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:563)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2314)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2131)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
  at org.kls.md.censusassistant.DialogController.loadFXML(DialogController.java:63)
  at org.kls.md.censusassistant.DCMainEditor.handleServicesEdit(DCMainEditor.java:330)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        ...

Caused by: java.lang.IllegalAccessException: Class sun.reflect.misc.ReflectUtil
    can not access a member of class org.kls.md.censusassistant.DCServRecEditor
    with modifiers "" 
  at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
  at java.lang.Class.newInstance0(Class.java:368)
  at java.lang.Class.newInstance(Class.java:327)
  at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:46)
  at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:731)
... 66 more

My class DCServRecEditor is a subclass of DialogController. It is a pretty normal looking FXML controller class:

class DCServRecEditor extends DialogController {

    private int m_dialogMode = DB.DBEDIT_MODE_ADD;
    private int m_selServ = -1;
    private GregorianCalendar m_cal = null;


    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;

    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    @FXML // fx:id="ancMatchSelector"
    private AnchorPane ancMatchSelector; // Value injected by FXMLLoader

    @FXML // fx:id="ancServEditor"
    private AnchorPane ancServEditor; // Value injected by FXMLLoader

    @FXML // fx:id="ancServRecEditor"
    private AnchorPane ancServRecEditor; // Value injected by FXMLLoader

        ...
}

I have double and triple checked to make sure that there wasn't a named control in the FXML that didn't also have an instance field in the controller class. All the instance fields are tagged with @FXML.

The name of the controller class in the FXML is the same as my java file and is properly qualified. The error occurs before initialize() is called, so I don't think it's anything with initialize(), although I have checked to make sure it is also tagged with @FXML.

The skeleton for my controller class was copied and pasted from Scene Builder ... I've gone back and repasted blocks of stuff from Scene Builder to be sure that there wasn't a control I was missing in my java file.

The error message gives me no specifics about the member it is having its problem with, other than to say it has modifiers "". I went back to my controller class and made all the members with default access public, and I still get the error.
I don't even know where in my class the problem is coming from. Anyone have any ideas about what is going wrong here?


回答1:


Yet another embarrassingly simple problem.

I'm surprised someone didn't jump on this by now.

The problem was in my class DCServRecEditor. Note that the class was declared with default access permission.

JavaFX requires that controller classes be made public.

To be fair to myself, Java's error reporting in this situation is abominable and misleading. The stack trace clearly shows that Java is complaining about being unable to access a member of my class, hence my focus on my instance fields and methods. Java really should have complained that it was the class itself that it could not access and not its members.




回答2:


I prefer to declare every method and filed public.



来源:https://stackoverflow.com/questions/16504300/javafx-illegalaccessexception-during-fxml-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!