The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem opening multiple workbooks in a single java program using ExcelAcce

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Fahd

Posts: 1
Nickname: fahd
Registered: Dec, 2002

Problem opening multiple workbooks in a single java program using ExcelAcce Posted: Dec 2, 2002 12:58 AM
Reply to this message Reply
Advertisement
I am using ExcelAccessor to open multiple workbooks in a single java program. Now whether i open the same excel file with different ExcelWorkbook objects or different files, it gives a null pointer exception when i set the worksheet of a range (range.setWorksheetName(sheet[0]) the second time. The first iteration runs fine and writes the data i want in the excel sheet but on the second iteration of the loop the same code gives a null pointer exception. The code is given below, a prompt reply would be greatly appreciated.

The code is a mess....sorry about it but in desperation i have tried everything but nothing has so far worked.

package excelaccessor;

import com.ibm.excelaccessor.ExcelWorkbook;
import com.ibm.excelaccessor.ExcelRange;
import java.util.Vector;
import com.ibm.excelaccessor.ExcelFormat;
import com.ibm.excelaccessor.ExcelFormatModel;

public class test implements com.ibm.excelaccessor.event.ExcelRangeListener,
com.ibm.excelaccessor.event.ExcelWorkbookListener,
com.ibm.excelaccessor.event.FetchArgumentsListener,
com.ibm.excelaccessor.event.FileSavingListener
{

private java.util.Vector datavector = null;
private String[] worksheets = null;


public test() {
try {
System.out.println("const start");
System.out.println("const end");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}

public static void main(String []args)
{
for(int i=1; i<=5; i++)
{
test demo = new test();
demo.driver(i);
}
}

public void driver(int i)
{


try
{

ExcelWorkbook ivjExcelWorkbook1 = null;
ExcelRange ivjExcelRange1 = null;
ExcelRange range = null;
ExcelRange loopRange = null;
ExcelRange innerLoopRange = null;


////////////

ivjExcelRange1 = new ExcelRange();
range = new ExcelRange();
innerLoopRange = new ExcelRange();
System.out.println("new workbook");
ivjExcelWorkbook1 = new ExcelWorkbook();
ivjExcelWorkbook1.newWorkbook();
ivjExcelWorkbook1.setReadOnly(false);
// System.out.println("name = c:\\FeatureAndOptions.xls");
ivjExcelWorkbook1.setWorkbookName("c:\\FeatureAndOptions.xls");
ivjExcelRange1.addExcelRangeListener(this);
range.addExcelRangeListener(this);
ivjExcelWorkbook1.addExcelWorkbookListener(this);
ivjExcelRange1.setXlWorkbook(ivjExcelWorkbook1);
range.setXlWorkbook(ivjExcelWorkbook1);


/////////////

System.out.println("driver start");
if(ivjExcelWorkbook1 == null)
{
System.out.println("workbook is null");
}
else
{
System.out.println("work book name = " + ivjExcelWorkbook1.getWorkbookName());
System.out.println("in else");
ivjExcelWorkbook1.openWorkbook();

if(ivjExcelWorkbook1.isOpened())
System.out.println("workbooks open");
else
{
System.out.println("in inner esle");
ivjExcelWorkbook1.newWorkbook();
ivjExcelWorkbook1.setReadOnly(false);
// System.out.println("name = c:\\FeatureAndOptions.xls");
ivjExcelWorkbook1.setWorkbookName("c:\\FeatureAndOptions.xls");
ivjExcelWorkbook1.addExcelWorkbookListener(this);
ivjExcelWorkbook1.openWorkbook();

}
worksheets = ivjExcelWorkbook1.getWorksheets();
System.out.println("sheet name = " + worksheets[0]);
ivjExcelRange1.setWorksheetName(worksheets[0]);
range.setWorksheetName(worksheets[0]);
ivjExcelRange1.setRangeName("E19:E22");
ivjExcelRange1.initRange();

/*range.setRangeName("J8:J13");
range.initRange();*/

ExcelFormatModel model = new ExcelFormatModel();
model.setRowCount(4);
model.setColumnCount(1);
model.initialize();

ExcelFormat format = new ExcelFormat();
format.setSize(8);
format.setBold(true);

ExcelFormat plainFormat = new ExcelFormat();
plainFormat.setSize(8);

model.addExcelFormat(0,0,format);
model.addExcelFormat(1,0,plainFormat);
model.addExcelFormat(2,0,format);
model.addExcelFormat(3,0,format);

System.out.println("1");
ivjExcelRange1.setExcelFormatModel(model);
ivjExcelRange1.saveExcelFormatModel();

System.out.println("2");
Vector value = new Vector();

value.add("fahd1");
value.add("sasf");
value.add("tkjhk");
value.add("fourth");
System.out.println("3");


ivjExcelRange1.setValue(value);

System.out.println("4");

ivjExcelRange1.removeExcelRangeListener(this);
range.removeExcelRangeListener(this);
ivjExcelWorkbook1.removeExcelWorkbookListener(this);

System.out.println("5");
ivjExcelWorkbook1.setSaveOnClose(true);
System.out.println("6");
ivjExcelWorkbook1.closeWorkbook();
System.out.println("7");
ivjExcelWorkbook1.closeExcelApplication();
// ivjExcelWorkbook1 = null;
// ivjExcelWorkbook1.finalize();
System.out.println("driver end");
}
}
catch(java.lang.Throwable ex)
{
ex.printStackTrace(System.out);
}
}

public void fetchArgumentsCalled(com.ibm.excelaccessor.event.FetchArgumentsEvent arg1) {
System.out.println("in fetchArgumentsCalled");
/*if ((arg1.getSource() == ivjExcelRange1) ) {
try {
ivjExcelRange1.setAutoCreateSheet(false);
if (worksheets != null) {
if (worksheets.length >= 1)
ivjExcelRange1.setWorksheetName(worksheets[0]);
}
ivjExcelRange1.setRangeName("");
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}*/
}

public void filledModel(com.ibm.excelaccessor.event.ExcelFormatEvent arg1)
{
System.out.println("filled model");
}

public void fillExcelFormatModel(com.ibm.excelaccessor.event.ExcelFormatEvent arg1)
{
System.out.println("fillExcelFormatModel");
}

public void savedModel(com.ibm.excelaccessor.event.ExcelFormatEvent arg1)
{
System.out.println("saved model");
}

public void saveExcelFormatModel(com.ibm.excelaccessor.event.ExcelFormatEvent arg1)
{
System.out.println("saveExcelFormatModel");
}


public void rangeInitialized(com.ibm.excelaccessor.event.ExcelRangeEvent arg1) {
try {
System.out.println("in rangeInitialized");
/*datavector = ivjExcelRange1.getValue();
String cellValue = null;
if (datavector != null) {
for (int i = 0; i < datavector.size(); i++) {
for (int j = 0; j < ((java.util.Vector)(datavector.elementAt(i))).size(); j++) {
cellValue = "cell:("+String.valueOf(i+1)+":"+String.valueOf(j+1)+")";
((java.util.Vector)(datavector.elementAt(i))).setElementAt(cellValue,j);
}
}
}
ivjExcelRange1.setValue(datavector);*/
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}

public void rangeUninitialized(com.ibm.excelaccessor.event.ExcelRangeEvent arg1) {
System.out.println("in rangeuninitialized");
}

public void savingFile(com.ibm.excelaccessor.event.FileSavingEvent arg1) {
System.out.println("in savingfile");
/* if ((arg1.getSource() == ivjExcelWorkbook1) ) {
try {
ivjExcelWorkbook1.setSaveOnClose(true);
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}*/
}

public void workbookClosed(com.ibm.excelaccessor.event.ExcelWorkbookEvent arg1) {
System.out.println("in workbookclosed");
}

public void workbookOpened(com.ibm.excelaccessor.event.ExcelWorkbookEvent arg1) {
System.out.println("in workbookOpened");
/* if (arg1.getSource() == ivjExcelWorkbook1) {
worksheets = ivjExcelWorkbook1.getWorksheets();
System.out.println("Worksheets:");
for (int j = 0; j < worksheets.length; j++)
System.out.println("\t"+worksheets[j]);
}*/
}

public void workbookStructureUnprotected(com.ibm.excelaccessor.event.ExcelWorkbookEvent arg1) {
System.out.println("in workbookstructunprotected");
}

/////////////////////////////////////////////// ///////////////////////////////////////////////////////////
// Do the error handling code here ! //

private void handleException(java.lang.Throwable exception) {
System.out.println("--------- UNCAUGHT EXCEPTION ---------");
exception.printStackTrace(System.out);
}

}

Topic: END PART OF CODING Previous Topic   Next Topic Topic: Using a method in another method

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use