48
 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?

Amir
  • 8,821
  • 7
  • 44
  • 48
Karthik.m
  • 771
  • 5
  • 12
  • 20
  • If the comment in the code is the question, when do you get it? From where? As an exception? It sure isn't a concept of javac to throw illegal forward references in the face of the user. If it is an exception it is probably very related to the xls-file which we don't have access to. – Fredrik Nov 17 '09 at 05:24
  • This error comes when i tried to compile the code – Karthik.m Nov 17 '09 at 05:27
  • Can you show the compiler error? – notnoop Nov 17 '09 at 05:31
  • 1
    after doing the correction that erickson mentioned, consider putting the code in a constructor. – Amarghosh Nov 17 '09 at 05:44
  • Does this answer your question? [Illegal forward Reference java issue](https://stackoverflow.com/questions/14624919/illegal-forward-reference-java-issue) – asyard May 06 '20 at 12:08

4 Answers4

109

"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(...);
erickson
  • 265,237
  • 58
  • 395
  • 493
2

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(...)
sanastasiadis
  • 1,182
  • 1
  • 15
  • 23
0

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.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
-1

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

nitin1706
  • 395
  • 3
  • 4