在下面的代码片段中,我真的不明白编译器发布的原因 “找不到符号”错误消息。
public class LU62XnsCvr extends Object
{
// these two variables (among many others) are declared here as "public"
static StringBuffer message_data = new StringBuffer();
static File Mesg_File = new File("C:\\...\\Mesg_File.txt"); // path snipped
public static void SendMesg() // This "method" is "called" from various
// sections of the LU62XnsCvr program
{
if (mesgcount == 1)
{
// On First call Connect the LU62XC Message File
FileOutputStream MesgOut = new FileOutputStream(Mesg_File);
FileChannel MesgChnl = MesgOut.getChannel();
ByteBuffer Mesg_Bufr = ByteBuffer.allocate(128);
}
// Send Message to the Message Log
String mesg_str = message_data.toString(); // convert buffer to a string
MesgWork = mesg_str.getBytes(); // Convert string to byte array
Mesg_Bufr.put( MesgWork, bufroffset, MGbuflen ); // copy MesgWork to buffer
MesgChnl.write( Mesg_Bufr ); // write message buffer out to the file channel
Mesg_Bufr.clear();
message_data.append(" "); // set message_data to 16 blanks
for ( ndx = 0; ndx < MGbuflen; ++ndx )
{
MesgWork[ndx] = 0x20; // clear MesgWork byte area using blank character
}
} // End of Send Message log write sub-routine
以上看起来对我好;但我得到以下内容:
src\LU62XnsCvr.java:444: cannot find symbol
symbol : variable Mesg_Bufr
location: class APPC_LU62.java.LU62XnsCvr
Mesg_Bufr.put( MesgWork, bufroffset, MGbuflen );
^
src\LU62XnsCvr.java:445: cannot find symbol
symbol : variable Mesg_Bufr
location: class APPC_LU62.java.LU62XnsCvr
MesgChnl.write( Mesg_Bufr );
^
src\LU62XnsCvr.java:445: cannot find symbol
symbol : variable MesgChnl
location: class APPC_LU62.java.LU62XnsCvr
MesgChnl.write( Mesg_Bufr );
^
src\LU62XnsCvr.java:446: cannot find symbol
symbol : variable Mesg_Bufr
location: class APPC_LU62.java.LU62XnsCvr
Mesg_Bufr.clear();
^
除非我在这里遗漏了某些内容,否则Mesg_Bufr
似乎拼写正确。
为什么编译器不能“找到”变量?
答案 0 :(得分:4)
您在Mesg_Bufr
块中声明if
,因此只能在该块中显示。
if (mesgcount == 1)
{
//On First call Connect the LU62XC Message File
FileOutputStream MesgOut = new FileOutputStream(Mesg_File) ;
FileChannel MesgChnl = MesgOut.getChannel() ;
ByteBuffer Mesg_Bufr = ByteBuffer.allocate(128) ;
}
其他人也一样。我不知道你想要做什么(并且我不在乎)但是为了使运行正确你可能必须将所有代码放在if中,或者更好的是,返回if {{1} }。