JXL library call - java

i am a barely new to the java (was always on c# before) and need to create a swing application where i need to read a data from xls file. So i use jXL.
I have a class, which returns name of a first sheet from excel file, choosen in jFileChooser. Here is the code:
import java.io.File;
import jxl.Sheet;
import jxl.Workbook;
public class ExcelObject
{
private String filename = null;
private Workbook wb = null;
private Sheet sheet = null;
public ExcelObject(String f)
{
filename = f;
}
public String getSheetName()
{
String sheet_name = null;
try
{
wb = Workbook.getWorkbook(new File(filename));
sheet = wb.getSheet(0);
sheet_name = sheet.getName();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
wb.close();
}
return sheet_name;
}
}
in the program call looks like :
ExcelObject ex = new ExcelObject(filename);
String s = ex.getSheetName();
lblReport.setText(s);
So the issue is : when ran in the eclipse (3.4.2) i am getting a correct value, when jar is compiled, NO VALUE IS RETURNED! I mean lblReport is empty, with no exceptions and warnings.
Keep in mind : all other external jars work fine.
I tried a lot of things but none is working.
Also, if i do something like
ExcelObject ex = new ExcelObject(filename);
String s = ex.getSheetName();
// lblReportRun.setText(s);
lblReportRun.setText("Test");
lblAnyOtherLabel.setText("Test");
no text is displayed in the labels either, in compiled jar, and fine in eclipse.

This is probably because if some exception occurs when opening the workbook, you catch it, print the stack trace, and then close the workbook in the finally block. But since an exception occurred when calling Workbook.getWorkbook, the wb variable is still null, and you're trying to invoke close on it. So a NullPointerException happens while in the finally block.
Watch your console, you must have some exception popping up.
Also, note that fileName should be the only instance variable of your class, that Java variable normally don't contain underscores and use camelCase, and thet the fileName variable should be of type File, rather than String : you get a File from the file chooser, transform it into a string, and then transform it back to a File.
Another possibility, since it works in Eclipse and not when run externally, is that you forgot to put the jXL jar in the classpath, which causes some ClassNotFoundException when trying to use the ExcelObject class.

Related

No NullPointerException on the logs

I have a strange behaviour in my java code I would like to ask some advice.
In a multithreading application I wrote this code:
scratchDir.resolve(directoryTree).toFile().mkdirs();
For a bug the Object scratchDir is null, I was expecting a stack trace on the logs but there's nothing about the error.
I have checked the code and I never try to catch the NullPointerException.
Here is the complete method code:
#Override
public void write(JsonObject jsonObject) throws FileSystemException {
Path directoryTree = getRelativePath();
scratchDir.resolve(directoryTree).toFile().mkdirs();
String newFileName = getHashFileName(jsonObject);
Path filePath = scratchDir.resolve(directoryTree).resolve(newFileName);
logger.debug("Write new file Json {} to persistent storage dir {}", newFileName, scratchDir);
File outputFile = filePath.toFile();
if (outputFile.exists()) {
throw new FileAlreadyExistsException(filePath.toString());
}
try (FileWriter fileWriter = new FileWriter(outputFile)) {
fileWriter.write(jsonObject.toString());
fileWriter.flush();
} catch (Exception e) {
logger.error(e);
}
}
Why I don't have the exception in my logs?
Why are you doing this?
The proper way to do this is:
Files.createDirectories(scratchDir.resolve(directoryTree));
don't mix old and new API. The old mkdirs() api DEMANDS that you check the return value; if it is false, the operation failed, and you do not get the benefit of an exception to tell you why. This is the primary reason for why there is a new API in the first place.
Are you sure you aren't confused - and that is the actual problem? The line as you have it will happily do absolutely nothing whatsoever (no directories, and no logs or exceptions). The line above will throw if it can't make the directories, so start there.
Then, if that line IS being run and nothing is logged, then you've caught the NPE and discarded it, someplace you didn't paste.

"remove type argument" while reading from excel file in java

I'm trying to read from an excel file in my java code (IDE is eclipse). My code is attached below. In the line which Iterator is declared i face this error:
The type HTMLDocument.Iterator is not generic; it cannot be parameterized with argument .
and offers me to "remove type argument."
I don't know what the problem is. I have downloaded necessary jar files and added them to my class path but it didn't help.
public static void readMapReduceTasksfromFile(String fileAddress) throws FileNotFoundException
{
try
{
FileOutputStream out = new FileOutputStream("...directory...");
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet();
Iterator <Row> rowIterator = sheet.iterator();//HERE
}
catch(Exception ex)
{
System.out.println("File Not Found");
}
}
Your problem lies in an area of your code, which you have not posted: the import statements. You need to import java.util.Iterator. Instead it seems like you have importedjavax.swing.text.html.HTMLDocument.HTMLDocument.Iterator.
Remove the erroneous import statement and use the "organize import" function in eclipse, this time make sure to choose java.util.Iterator.

Change in file after creating FileOutputStream object even without writing anything into file

EDIT (for the sake of confusion): null has been written into the files "abc" and "efg".
After running the following code, the contents of file "abc" change which were initially null , and I get EOFException in every next execution :
ObjIStream = new ObjectInputStream(new FileInputStream("abc"));
M[][] objs = (M[][]) ObjIStream.readObject();
FS.objs = objs;
ObjIStream.close();
Here, FS.objs is a static member of class FS of type M[][] type.
On the other hand, this one has no effect on the file and I don't get any Exceptions after any number of executions:
ObjIStream = new ObjectInputStream(new FileInputStream("abc"));
M[][] objs = (M[][]) ObjIStream.readObject();
ObjIStream.close();
EDIT: I just found the trouble that exists in class FS in this form:
static{
try {
ObjOStream = new ObjectOutputStream(new FileOutputStream("abc"));
ObjOStream.close();
ObjOStream = new java.io.ObjectOutputStream(new java.io.FileOutputStream("efg"));
ObjOStream.close();
}
catch (IOException ex) { }
}
How is it troubling anyways?
The problem is new FileOutputStream("abc") itself, which means new FileOutputStream("abc", false). It cleans up all the data in file because you are not going to append anything. It calls FileOutputStream.open(String name, boolean append) which is a private native function. It erases everything in file in overwrite mode.

Aspose with RJB (Ruby Java Bridge) is not working

I have a code in Java that opens a excel template by aspose library (it runs perfectly):
import com.aspose.cells.*;
import java.io.*;
public class test
{
public static void main(String[] args) throws Exception
{
System.setProperty("java.awt.headless", "true");
FileInputStream fstream = new FileInputStream("/home/vmlellis/Testes/aspose-cells/template.xlsx");
Workbook workbook = new Workbook(fstream);
workbook.save("final.xlsx");
}
}
After I run this on Ruby with RJB (Ruby Java Bridge):
require 'rjb'
#RJM Loading
JARS = Dir.glob('./jars/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])
system = Rjb::import('java.lang.System')
file_input = Rjb::import('java.io.File')
file_input_stream = Rjb::import('java.io.FileInputStream')
workbook = Rjb::import('com.aspose.cells.Workbook')
system.setProperty("java.awt.headless", "true")
file_path = "/home/vmlellis/Testes/aspose-cells/template.xlsx"
file = file_input.new(file_path)
fin = file_input_stream.new(file)
wb = workbook.new(fin)
I get this error:
test.rb:57:in `new': Can't find file: java.io.FileInputStream#693a317a. (FileNotFoundException)
from aspose-test.rb:57:in `<main>'
Why? I run the same code... but in Ruby is not working! How do I fix this?
Update:
In documentation there is the the initializer: Workbook(java.io.InputStreamstream)... but it's not working in RJB. (How is this possible?)
Your program should have worked, but I could not find any reason why it didn't and I am looking into it.
Now the alternate approaches.
Approach 1
Use Workbook(String) constructor instead of Workbook(FileInputStream). This worked flawlessly at my end. The sample code is
require 'rjb'
#RJM Loading
JARS = Dir.glob('/home/saqib/cellslib/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])
system = Rjb::import('java.lang.System')
workbook = Rjb::import('com.aspose.cells.Workbook')
system.setProperty("java.awt.headless", "true")
file_path = "/home/saqib/rjb/template.xlsx"
save_path = "/home/saqib/rjb/final.xlsx"
wb = workbook.new(file_path)
wb.save(save_path)
Approach 2
Write a new Java class library. Write all your Aspose.Cells related code in it. Expose very simple and basic methods that needs to be called from Ruby (RJB).
Why?
It is easy to write program in native Java language. If you use RJB, you need to perform a lot of code conversions
It is easy to debug and test in Java.
Usage of RJB will only be limited to calling methods from your own Java library. The RJB code will be small and basic.
Similar Example using own library
Create a new Java project, lets say "cellstest". Add a new public class in it.
package cellstest;
import com.aspose.cells.Workbook;
public class AsposeCellsUtil
{
public String doSomeOpOnWorkbook(String inFile, String outFile)
{
String result = "";
try
{
// Load the workbook
Workbook wb = new Workbook(inFile);
// Do some operation with this workbook
// ..................
// Save the workbook
wb.save(outFile);
// everything ok.
result = "ok";
}
catch(Exception ex)
{
// Return the exception to calling program
result = ex.toString();
}
return result;
}
}
Like this, add as many methods as you like, for each operation.
Build the project and copy the "cellstest.jar" in same folder where you copied Aspose.Cells jar files. You can return a String from your methods and check the return value in Ruby program for success or error code. The Ruby program will now be like
require 'rjb'
#RJM Loading
JARS = Dir.glob('/home/saqib/cellslib/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])
system = Rjb::import('java.lang.System')
AsposeCellsUtil = Rjb::import('cellstest.AsposeCellsUtil')
system.setProperty("java.awt.headless", "true")
file_path = "/home/saqib/rjb/template.xlsx"
save_path = "/home/saqib/rjb/final.xlsx"
# initialize instance
asposeCellsUtil = AsposeCellsUtil.new()
# call methods
result = asposeCellsUtil.doSomeOpOnWorkbook(file_path, save_path)
puts result
PS. I work for Aspose as Developer Evangelist.
In your Java code, you pass a file name string into FileInputStream() constructor:
FileInputStream fstream = new FileInputStream("/home/vmlellis/Testes/aspose-cells/template.xlsx");
In your Ruby code, you pass a file object:
file = file_input.new(file_path)
fin = file_input_stream.new(file)
Have you tried to do the same thing as in Java?
fin = file_input_stream.new(file_path)

Error in displaying a doc file reading that from a document on console in java

I tried to display the data from a doc file on console then i got this error
run:
The document is really a RTF file
Exception in thread "main" java.lang.NullPointerException
at DocReader.readDocFile(DocReader.java:36)
at DocReader.main(DocReader.java:47)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
can any one explain where i went wrong
the code is
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
public class DocReader {
public void readDocFile() {
File docFile = null;
WordExtractor docExtractor = null ;
WordExtractor exprExtractor = null ;
try {
docFile = new File("C:\\web.doc");
FileInputStream fis=new FileInputStream(docFile.getAbsolutePath());
HWPFDocument doc=new HWPFDocument(fis);
docExtractor = new WordExtractor(doc);
}
catch(Exception exep)
{
System.out.println(exep.getMessage());
}
String [] docArray = docExtractor.getParagraphText();
for(int i=0;i<docArray.length;i++)
{
if(docArray[i] != null)
System.out.println("Line "+ i +" : " + docArray[i]);
}
}
public static void main(String[] args) {
DocReader reader = new DocReader();
reader.readDocFile();
}
}
The document is really a RTF file
That's a typical message of an IllegalArgumentException from the HWPFDocument constructor. To the point it means that the supplied file is actually a (Wordpad) RTF file whose .rtf extension has incorrectly been renamed to .doc.
Supply a real MS Word .doc file instead and fix your code to not continue the flow when an exception has occurred. You need to throw it.
Just open the file in some Document program like Microsoft Office. Now save the same file with "Save As" option and choose .doc format.
That means, at line 36 of DocReader.java file, you are trying to invoke an API from an object but the object is not being created yet. So, the solution is to create an instance of the class first before making that API invocation.
UPDATE
My hunch tells me the NullPointerException happens at docExtractor.getParagraphText() because the docExtractor doesn't get initialized properly. Instead of swallowing the exception, print the stacktrace to figure out the actual problem, like this:-
try {
...
}
catch(Exception exep) {
exep.printStackTrace(); // do this
}

Categories