illegal forward reference in java

匿名 (未验证) 提交于 2019-12-03 02:02:01

问题:

 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.

How to resolve this?

回答1:

"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 wb in the declaration of wb.

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


回答2:

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("----")); - wb is uninitialized before calling the getWorkbook() method. For avoiding the FIR you should initialize wb.



回答3:

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

Workbook wb = Workbook.getWorkbook(...) 


回答4:

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".



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!