“无法找到符号”尽管正确的变量名
问题描述:
在下面的代码片段中,我真的不明白为什么编译器会发出 “找不到符号”错误消息。“无法找到符号”尽管正确的变量名
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
“拼写为”正确。 为什么编译器不能找到变量?
答
您在if
块中声明Mesg_Bufr
,因此它只在该块中可见。
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) ;
}
其他人也一样。我不知道你想做什么(和tbh我不在乎),但为了使其正确运行,您可能必须将所有代码放在if中,或者更好的情况下,如果返回mesg != 1
。
嗯,非常感谢...宁愿使用这种编译器和java语言 – 2011-04-30 15:56:34