java中的非法前向引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1746758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:10:22  来源:igfitidea点击:

illegal forward reference in java

java

提问by Karthik.m

 import java.io.*;
 import jxl.*;
 class Xlparsing
 {
   Workbook wb =wb.getWorkbook(new File(
    "C:\Documents and Settings\kmoorthi\Desktop\ak\new.xls"));
   // Illegal forward reference What it means
   Sheet st = wb.getSheet(0);
   Cell cell1 = st.getCell(0,0);
   String a1 = cell1.getContents();
   public static void main(String s[])
   {
     System.out.println(new Xlparsing().a1);
   }
 }

Hi When I tried to extract data from excel sheet illegal forward reference error comes in the file object creation.

嗨,当我尝试从 Excel 工作表中提取数据时,文件对象创建中出现非法前向引用错误。

How to resolve this?

如何解决这个问题?

采纳答案by erickson

"Illegal forward reference" means that you are trying to use a variable before it is defined.

“非法前向引用”意味着您试图在定义变量之前使用它。

In this case, you are trying to invoke a method on wbin the declaration of wb.

在这种情况下,您试图wb在 的声明中调用方法wb

Workbook wb = wb.getWorkbook(...);

回答by nitin1706

although getWorkbook is static, so accordingly, this code should have worked. But here, using the reference before its declaration or in the same statement as declaration is causing error "Forward referencing i.e. using reference before declaration".

尽管 getWorkbook 是静态的,因此相应地,此代码应该有效。但是在这里,在声明之前或在与声明相同的语句中使用引用会导致错误“前向引用,即在声明之前使用引用”。

回答by user2461831

Forward Illegal Reference is a term which comes into picture when an uninitialized non global variable value is assigned to a global variable.

前向非法引用是一个术语,当未初始化的非全局变量值被分配给全局变量时就会出现。

In your case Workbook wb = wb.getWorkbook(new File("----"));- wbis uninitialized before calling the getWorkbook()method. For avoiding the FIR you should initialize wb.

在您的情况下Workbook wb = wb.getWorkbook(new File("----"));-wb在调用该getWorkbook()方法之前未初始化。为了避免 FIR,您应该初始化wb.

回答by sanastasiadis

I guess that the intention was to call 'statically' the getWorkbook()method, as you should. So, you should change your wbmember initialization as:

我想这样做的目的是getWorkbook()像您应该的那样“静态地”调用该方法。因此,您应该将wb成员初始化更改为:

Workbook wb = Workbook.getWorkbook(...)