I have checked the tutorials and this should work but it doesn’t. I have a string in one class that I want to use in another, but when I do, I get a null exception.
@ViewScoped
@ManagedBean
public class FileDirectoryViewer {
FileUploadController destination = new FileUploadController();
NewDestination = destination + username + "/";
And I am trying to get the destination from
@ViewScoped
@ManagedBean(name = "fileUploadController")
public class FileUploadController {
public String destination = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/"; // main location for uploads
How do I get the destination from FileUploadController to FileDirectoryViewer?
The
destinationfield ofFileUploadController, has no access modifier, so it is package private by default.If those two classes are in the same package, you can access it by creating an instance of the class, and using the
.operator on the instance to access the property:If they are not, you should implement a
public String getDestination()method inFileUploadControllerthat would returndestination. You should call this method also by using the.operator:controller.getDestination().Take into account there are several issues with the code you posted:
You’re placing code outside a method in the
FileDirectoryViewerclass. In a class you can only define members (such as fields, or methods). Generally speaking, behavorial code goes inside of method declarations.Using a hardcoded variable for a path property can be considered bad practice. Look into
java.util.Propertiesfor a start. Anyway, by the looks of the code, it doesn’t make sense for thedestinationfield to be an instance field, it could bestaticor defined in aninterface.Naming conventions for java recommend variable names to be camel case, and starting with lower case letters.