0

我正在尝试将值传递给可能尚未声明的变量。从我的主要源类中,我给另一个类一些值,但后来似乎这些值已经消失了。

源代码:

server.java(主要):

public class server {

public static void main(String[] args)  {

    //Print a simple message to the user to notify there is something going on...
    System.out.println("Starting server, please wait...");

    //Connecting all class files to the server.
    filehandler filehandlerclass = new filehandler();
    networking networkingclass = new networking();
    //End of class files connecting.

    //Preparing the filehandler's file information to open a new filestream.
    filehandlerclass.filetohandlename = "server";
    filehandlerclass.filetohandleextention = "ini";
    filehandlerclass.filetohandlepath = "configs\\";

    //Request a new filestream using the filehandler's file variables.
    filehandlerclass.openfilestream(filehandlerclass.filestream, filehandlerclass.filetohandle);

    //Checks if the filehandler has tried to open a filestream.
    if(filehandlerclass.filestreamopen == true) {
        //Request a check if the filestream was opened sucessfully.
        filehandlerclass.filestreamexists(filehandlerclass.filestream);
    }
    //If the filehandler has not tried to open a filestream...
    else    {
        System.out.println("Error: The filehandler does not seem to have tried to open a filoestream yet.");
        System.out.println("A possibility is that the server could not call the method from the filehandler properly.");
    }

    //Checks if the boolean "filestreamexists" from the filehandlerclass is true.
    if(filehandlerclass.filestreamexists(filehandlerclass.filestream) == true)  {
        //The filestream seems to exist, let's read the file and extract it's information.
        filehandlerclass.readfile(filehandlerclass.filestream);
    }
    else    {
        filehandlerclass.openfilestream(filehandlerclass.filestream, filehandlerclass.filetohandle);
    }
}
}

文件处理程序.java:

//Imports the java.io library so the filehandler can read and write to text files.
import java.io.*;

public class filehandler    {

//Variables for the filehandler class.
public String filetohandlename;
public String filetohandleextention;
public String filetohandlefullname = filetohandlename + "." + filetohandleextention;
public String filetohandlepath;
public String filetohandle = filetohandlepath + filetohandlefullname;

//Boolean that is true if the filehandler's "openfilestream"-method has tried to open a filestream.
//Is false as long as none filestreams have been touched.
public boolean filestreamopen = false;

//Declares a variable for the filestream to access text files.
public File filestream;
//End of variable list.

//Called to open a filestream so the server can load properties from text files.
public void openfilestream(File filestream, String filetohandle)    {

    //Tell the user that a filestream is about to be opened.
    System.out.println("Opening filestream for \"" + filetohandlefullname + "\"...");
    //Open a filestream called "filestream" using the variable "filetohandle"'s value
    //as information about wich file to open the filestream for.
    filestream = new File(filetohandle);
    //Turn the boolean "filestreamopen" to true so next time the server checks it's
    //value, it knows if the filehandler has tried to open a filestream.
    filestreamopen = true;
}

//Boolean that checks if the filestream exists.
public boolean filestreamexists(File filestream)    {
    //Tell the user that a check on the filestream is going on.
    System.out.println("Checking if filestream for \"" + filetohandlefullname + "\" exists...");
    //If the filestream exists...
    if(filestream.exists()) {
        //Tell the user that the filestream exists.
        System.out.println("Filestream for \"" + filetohandlefullname + "\" exists!");
        //Make the boolean's value positive.
        return true;
    }
    //If the filestream does not exist...
    else    {
        //Tell the user that the filestream does not exist.
        System.out.println("Filestream for \"" + filetohandlefullname + "\" does not exist!");
        //Make the boolean's value negative.
        return false;
    }
}

//Called to read files and collect it's information.
public void readfile(File filestream)   {
    //Checks if the file that is going to be read is a configuration file.
    if(filetohandleextention == "ini")  {
        //Tell the user that a configuration file is going to be read.
        System.out.println("Extracting information from the configuration file \"" + filetohandle + "\".");
    }
}
}

网络.java:

public class networking {

}

问题:

server.java 将为源文件提供命令并告诉他们该做什么。除非 server.java 给了它们一个命令,否则源文件不会自行运行。通过这种方式,我计划能够在 server.java 中编写简单的函数调用,以从不同的源文件中执行更大的任务。

server.java 似乎在声明变量之前传递了变量“filetohandlename”、“filetohandleextention”和“filetohandlepath”,当它们被声明时,它们被声明为“null”作为值。

结果:

Java 编译/运行错误。

编译时没有错误。我认为正在发生的只是与指定要读取的文件的正确值的变量不匹配。它还会引发一个我暂时不关心的异常,要么是因为“null.null”不存在,要么是我写错了代码。

最终要求:

有谁知道我是否可以制作一种接收变量值的方法,或者是否有另一种更合适的方法?我可以在 server.java 中创建一个变量数组并从该数组中收集值吗?

非常感谢您的参与。

4

2 回答 2

3

这个:

public String filetohandlename;
public String filetohandleextention;
public String filetohandlefullname = filetohandlename + "." + filetohandleextention;

将前两个变量初始化为 null,将第三个变量初始化为“null.null”。请注意,如果您更改组成 的组件变量之一filetohandlefullname,则不会更改 的值filetohandlefullname。如果您希望发生这种情况,filetohandlefullname则应替换为执行附加操作的方法。

这个:

public void openfilestream(File filestream, String filetohandle)  

将不同的变量传递给filetohandle方法。该变量不同于.this.filetohandle

我认为这段代码(上面)有很多问题,我会做以下事情。

  1. 用动态执行此操作的方法替换通过其他变量实例化的变量。这样,当您更改var1您希望更改的值时var2,这将通过方法返回自动发生。例如创建一个私有方法getFileToHandleFullName()并将相应的变量装箱
  2. 范围类成员this
  3. 在可能的情况下,制作这些成员final,这样您就不会无意中更改它们
于 2012-11-07T00:03:14.847 回答
0

这条线

public String filetohandlefullname = filetohandlename + "." + filetohandleextention;

当您实例化类时执行(即filehandler filehandlerclass = new filehandler();)。在那个时间点,两个变量都没有设置,因此filetohandlefullname被初始化为null.null

但是您的代码也存在许多其他问题,例如

//Request a new filestream using the filehandler's file variables.
filehandlerclass.openfilestream(filehandlerclass. filestream,filehandlerclass.filetohandle);

例如,您传递的参数是来自同一实例的字段。这完全没用,因为该方法已经可以访问这些,而且非常令人困惑。

并且可能有点争议:

//Make the boolean's value positive.
return true;

注释只有在它们阐明代码时才有用,如果没有它们会不那么明显,并且它们必须是 100% 真实的,而不是像经常发生的那样,程序希望代码会做什么。在这种特定情况下,这些条件都不满足,因为注释没有说明发生了什么,实际上设置了方法的返回值,而不是一些不起眼的“布尔值”

于 2012-11-07T00:07:16.823 回答