«не могу найти символ», несмотря на правильное имя переменной - PullRequest
0 голосов
/ 30 апреля 2011

В следующем фрагменте кода я действительно не понимаю, почему компилятор выдает сообщение об ошибке «не удается найти символ».

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 написано правильноПочему компилятор не может "найти" переменную?

1 Ответ

4 голосов
/ 30 апреля 2011

Вы объявляете 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 mesg != 1.

...