Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.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_Exception_Exception Handling - Fatal编程技术网

基本java错误处理加上变量错误

基本java错误处理加上变量错误,java,exception,exception-handling,Java,Exception,Exception Handling,编辑:我已声明变量超出范围 因此,我在尝试编译时遇到一个错误,上面说: cannot find symbol symbol : variable bos location: class steganography byte[] bytes = bos.toByteArray(); 违规代码: //We'll use this to read the file in FileInputStream fis = new FileInputStream(file); //We'll use

编辑:我已声明变量超出范围

因此,我在尝试编译时遇到一个错误,上面说:

cannot find symbol
symbol  : variable bos
location: class steganography
    byte[] bytes = bos.toByteArray();
违规代码:

//We'll use this to read the file in
FileInputStream fis = new FileInputStream(file);
//We'll use this to push the file out
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
  for (int readNum; (readNum = fis.read(buf)) != -1;) {
    bos.write(buf, 0, readNum); 
    //no doubt here is 0
    /*Writes len bytes from the specified byte array starting at offset off to this byte array output stream.*/
    System.out.println("read " + readNum + " bytes,");
  }
} catch (IOException ex) {
 System.out.println(ex);
  //Logger.getLogger(ConvertImage.class.getName()).log(Level.SEVERE, null, ex);
}

很抱歉没有提供更多信息,我是java n00b。

不太确定这是您所说的令人不快的代码。你能告诉我们这是哪里吗

byte[] bytes = bos.toByteArray(); 

如果可能的话,很高兴看到完整的问题以及解决方案!这使得这个问题和答案对以后的观众更有用:


看起来您可能将变量bos声明为超出范围,因此编译器无法识别该特定类型及其方法。这听起来像是您所做的吗?

您剪切粘贴的例程中不会出现有问题的代码,因此无法向您提供建议。我的猜测是,这是一个范围问题:您认为变量名在该上下文中是可见的,但事实并非如此。