Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在我的代码中不断遇到类变量问题_Java_Variables - Fatal编程技术网

Java 在我的代码中不断遇到类变量问题

Java 在我的代码中不断遇到类变量问题,java,variables,Java,Variables,我正在玩一点Java,开始用Java代码为一个基于聊天的小应用程序创建一个客户端。我试图使变量ssocket成为一个类变量,它将是连接服务器的变量(尚未编写,但这不是问题所在)。因此,我在类声明的正下方声明类变量ssocket,然后(我一直这样做并注释掉)将ssocket声明为构造函数中的空套接字。然后,在一个名为sendToSever的方法中,我尝试引用它来设置主机、端口和内容,但它一直说它应该是一个局部变量,而不是类变量。但是,几行之后,用同样的方法,我引用了它。另外,当我尝试在try块中将

我正在玩一点Java,开始用Java代码为一个基于聊天的小应用程序创建一个客户端。我试图使变量
ssocket
成为一个类变量,它将是连接服务器的变量(尚未编写,但这不是问题所在)。因此,我在类声明的正下方声明类变量
ssocket
,然后(我一直这样做并注释掉)将
ssocket
声明为构造函数中的空套接字。然后,在一个名为
sendToSever
的方法中,我尝试引用它来设置主机、端口和内容,但它一直说它应该是一个局部变量,而不是类变量。但是,几行之后,用同样的方法,我引用了它。另外,当我尝试在
try
块中将
ssocket
设置为构造函数中的主机和端口时,它也没有使用class变量。我想在
sendToSever
中正式设置套接字,以便他们每次执行操作时都可以尝试重新连接,但我不知道如何解决此问题。如果有什么不清楚的地方,我很乐意编辑这篇文章

我试过:

  • 设置为静态
  • 在构造函数中完全定义ssocket(为其分配真实主机和端口)
  • 引用try块外部但仍在sendToServer()内部的ssocket 我想将ssocket声明为一个类变量,然后在sendToServer中设置它(这样,如果它无法连接,该变量仍然为null或其他值,然后下次调用它时,它会尝试重新连接)


    您的错误是未关闭构造函数方法:

    public ClientGUI() throws IOException {
        ssocket = new Socket();
    // } should be closed her, but was not
    
     public int sendToServer(String text) {
         ...
     }
    

    我注意到代码中的一些问题,并做了以下更改

  • 将ssocket变量设为类级别
  • 无需在构造函数内将其初始化为null
  • sendToServer将在初始化它之前检查它是否为null(此方法中有输入错误)
  • 正如@xtratic已经提到的,构造函数没有正确关闭
  • 下面是修改后的代码

    // static variable, single reference is shared across all instances of the class
    private static Socket ssocket = null;
    
    public ClientGUI() throws IOException {
        // ssocket is not initialized here
    }
    
    public int sendToServer(String text) {
        try (
    
            if (ssocket == null) {
                // typo, socket is used in code instead of ssocket 
                ssocket = new Socket("127.0.0.1", 123456);
            }
            // ...
        } catch (Exception e) {
            // if connectivity issue occurred, you can close the ssocket and set to null here
            // ...
        }
    }
    

    希望这有帮助。

    不要再链接到您的代码。相反,请修复格式错误,以便正确地包含它。我已经编辑了您的问题,将您的代码包含在Pastebin中。在您的构造函数之后,您缺少一个右大括号
    }
    。凯,谢谢,我现在就去试试这个。我可能错过了
    }
    ,因为我复制粘贴不好(不想复制整个程序,因为它有200多行)。@neurophicker我也认为丢失的}是复制粘贴问题,不管怎么说。希望这些说明能推动你前进。好吧,看起来它奏效了;我不得不去掉括号,但是Java一直说它需要大括号;如果是这样的话,那会导致更多的错误。
    // static variable, single reference is shared across all instances of the class
    private static Socket ssocket = null;
    
    public ClientGUI() throws IOException {
        // ssocket is not initialized here
    }
    
    public int sendToServer(String text) {
        try (
    
            if (ssocket == null) {
                // typo, socket is used in code instead of ssocket 
                ssocket = new Socket("127.0.0.1", 123456);
            }
            // ...
        } catch (Exception e) {
            // if connectivity issue occurred, you can close the ssocket and set to null here
            // ...
        }
    }