• Post Reply Bookmark Topic Watch Topic
  • New Topic

array index out of bound exception  RSS feed

 
R. Joshi
Greenhorn
Posts: 22
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to read files in a folder and write their content to another single file, but I am facing ArrayIndexOutOfBoundException, I have tried to debug but I cannot find the problem.
The code that I am using is:



import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Scanner;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class CheckdataWithExcel {
public static void main(String[] args)
{
String FilesNames="";

String pathToFolder = "C:\\nova\\err";


//String pathToFolder = args[0];
String outputFilePath = "C:\\Documents and Settings\\sandeep.kumar\\Desktop\\ch\\chkdt_acc803_husjurent_upd_01.xml";
//String outputFilePath = args[1];
String filePath = "D:\\dynamicNodes.xls";
//String filePath = args[2];


//String filePathofStaticDatalist = "D:\\roadmap\\tools\\regression_test\\data\\static\\staticdatalist";
//String filePathofStaticDatalist = args[3];

String delimiter = "####@####";
String line = "", lineToUpperCase ="" ,staticline = "";

ArrayList tablenames = new ArrayList();
ArrayList tabledatas = new ArrayList();
String tabledata="";
String tablename = "Table NOT INTIALISED";

String pathToTables = "D:\\roadmap\\database\\tables";
ArrayList uniqueconstraints = new ArrayList();
String contraint;
String clause = "not initilised";


File folder = new File(pathToFolder);
File[] listOfFiles = folder.listFiles();

//Collecting all files starting with error
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
if(listOfFiles[i].getName().startsWith("error"))
FilesNames += listOfFiles[i].getName() + delimiter;
}
}

String fileNames[]=FilesNames.split(delimiter);
String static_tables=null;
String static_tablesArr[]=null;
int totalNumberofFiles = fileNames.length;;
System.out.println("totalNumberofFiles" + totalNumberofFiles);

try
{


for (int i = 0; i < totalNumberofFiles; i++)
{
Scanner infile1 = new Scanner(new File(pathToFolder + "\\" + fileNames[i]));
System.out.println("reading the file:: " + fileNames[i] + "\t:::");
while (infile1.hasNextLine())
{
line = infile1.nextLine();
line=line.toUpperCase();

if((line.contains("SPIT") || line.contains("SPUT"))
&& !line.contains("SYSNXT") && !line.contains("TUSR")
&& !line.contains("TCOYNXTID") && !line.contains("TDSITFLOG")
&& !line.contains("LCK") && !line.contains("PRSQUE")
&& !line.contains("SVSREL") && !line.contains("EVT")
&& !line.contains("URS") && !line.endsWith("CTL")

&& !line.contains("ARCHIVEREQUEST") && !line.contains("ALTSTTMED")
&& !line.contains("SYSPMTLIS") && !line.contains("PMT")
&& !line.contains("SRC") && !line.contains("TMP")
&& !line.contains("CTRGRP") && !line.contains("CTRGRPCHG")
&& !line.contains("BASSCH") && !line.contains("SECDATFLD")

&& !line.contains("DATGRP") && !line.contains("ALTSTTMED")
&& !line.contains("RSN") && !line.contains("MGNGRP")
&& !line.contains("STTEVTTYP") && !line.contains("BRN")
&& !line.contains("HLDTYP") && !line.contains("HLDFNDSRC")
&& !line.contains("ROU") && !line.contains("AUTPSN")
)

{
if(line.contains("EXESTOPRO") || line.contains("EXECUTING STORED PROCEDURE"))
{
System.out.println("EST short_form vs full_form" + line);

//::STARTS:: Fetching the Tablename
if(line.contains("SPIT")){
System.out.println("spit");
tablename = line.split("SPIT")[1];
}
if(line.contains("SPUT")){
System.out.println("sput");
tablename = line.split("SPUT")[1];
}

tablename = tablename.split(" ")[0].trim();
tablename = "T"+tablename;

if(!tablenames.contains(tablename))
{
tablenames.add(tablename);
tabledatas.add("");
}
else{
continue;
}
System.out.println("tablename =T"+tablename);

//::ENDS:: Fetching the Tablename
//String data1[] = line.split(":");
String row="";
//if(data1[2]!="" ){
row="\n\t\t\t<!--"+line+"-->\n\t\t\t<ROW>\n\t\t\t\t";
//}
while(true)
{
line = infile1.nextLine();
lineToUpperCase = line.toUpperCase();
if(line.contains(":i")||lineToUpperCase.contains("ALSTUPDDTETME")
|| lineToUpperCase.contains("ALSTUPDUSRCDE")
||lineToUpperCase.contains("PRVLSTUPDDTETME")
||lineToUpperCase.contains("PRVLSTUPDUSRCDE")
||lineToUpperCase.contains("ASRCCDE")
||lineToUpperCase.contains("AACCFRQFRDTE")
||lineToUpperCase.contains("AACCOPNSYSDTETME")
||lineToUpperCase.contains("AACCOPNDTE")
||lineToUpperCase.contains("NULL"))
//||lineToUpperCase.contains("' '"))
continue;
if(line.trim().isEmpty())
continue;
if(lineToUpperCase.contains("ACO: DETAIL"))
break;
String data[] = line.split(":");
if(data.length>2){
System.out.println(line);
}
//continue;
//for(int kh=0;kh<data.length;kh++){

// System.out.println(kh+" "+data[kh].trim());
//}
//}
String name = data[0].trim().toUpperCase();
String value = data[2].trim();

if(value.equalsIgnoreCase("NULL")){
value = "";
}
if(data.length > 3){
value = data[2].trim();
for(int il=3; il<data.length;il++){
value = value + ":" + data[il] ;
}
}

String valueReturned = canBeDynamic(filePath, name);
String canBeDynamicIndicator = valueReturned.split(";")[0];

if(canBeDynamicIndicator.equalsIgnoreCase("true")){
int rowIndex = Integer.parseInt(valueReturned.split(";")[1]);
value = valueToDynamic(filePath, name, rowIndex) + "_"+value;
}


if(value!=""&&value!=null)
{
//System.out.println(value);
String coloum = "<COLUMN Name='"+name+"' Rule='Equals'><PARAM Value='"+value+"'/></COLUMN>\n\t\t\t\t";
row += coloum;
}
}

row=row+"</ROW>\n";
int index = tablenames.indexOf(tablename);
Object rowdata=tabledatas.get(index);
tabledatas.set(index, rowdata+row);

}
}
}
}

int size=tablenames.size();
BufferedWriter outfile1 = new BufferedWriter(new FileWriter(outputFilePath));
outfile1.write("<CHECKDATA>\n");
for(int j=0;j<size;j++)
{
Object tablenam=tablenames.get(j);

try {
Scanner tableXml = new Scanner(new File(pathToTables + "\\" +tablenam+".xml"));
while (tableXml.hasNextLine()){

String temp = tableXml.nextLine();
//System.out.println(ji++ + "::" + temp);
if(temp.contains("UniqueConstraintColumn")){
contraint = temp.split("\"")[1].split("\"")[0];
uniqueconstraints.add(contraint);
}
}
} catch (FileNotFoundException e) {
contraint = "XML_::_" + tablenam +".xml_missing";
uniqueconstraints.add(contraint);
}

if(uniqueconstraints.size() == 0){
clause = "NO_UNIQUE_CONSTRAINT_DEFINED";
}
else{
clause = uniqueconstraints.get(0).toString();
}

for(int i=1; i<uniqueconstraints.size();i++){
System.out.println("i%3" + i%3);
if(i%3==0){
clause = clause + " = '' \n\t\t\t\t\t and " +uniqueconstraints.get(i) ;;
}
else{
clause = clause + " = '' and " + uniqueconstraints.get(i) ;
}
if(i==uniqueconstraints.size()-1){
clause = clause + " = ''";
}
}
clause = clause.toLowerCase();
Object row = tabledatas.get(j);
tabledata="\t<TABLE Name='"+tablenam+"'>\n\t\t <SELECT>\n\t\t\tSELECT * \n\t\t\t\t FROM "+tablenam.toString().toLowerCase()+"\n\t\t\t\tWHERE "+clause+"\n\t\t </SELECT>"+row+"\t</TABLE>\n";
outfile1.write(tabledata+"\n");
System.out.println(":::<<" + clause);
clause = "";
uniqueconstraints.removeAll(uniqueconstraints);
System.out.println(":::>>>" + clause);
}
outfile1.write("</CHECKDATA>");
String note = "\n\n<!--Note::\n1.NULL is repalced by ''-->";
outfile1.write(note);
outfile1.close();
}

catch (java.util.NoSuchElementException e){
System.out.println("moving on.!!");
}
catch (Exception e)
{
e.printStackTrace();
}
}

static String canBeDynamic(String filePath, String nodeValue){

String isDynamic_index = "false;No_INDEX";
File f = new File (filePath);
Workbook w = null ;
Sheet sh = null ;
Cell c = null;
try {
w = Workbook.getWorkbook(f);
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sh = w.getSheet(0);
for(int rowCount =0;rowCount <sh.getRows();rowCount++){
c = sh.getCell(0, rowCount);
String content = c.getContents();
if(nodeValue.equalsIgnoreCase(content)){
isDynamic_index = "true;" + rowCount;
break;
}
}
w.close();
return isDynamic_index;
}

static String valueToDynamic(String filePath, String nodeValue,int row_index ){

String dynamicValue = "VALUE_NOT_FOUND";
File f = new File (filePath);
Workbook w = null ;
Sheet sh = null ;
Cell c = null;
try {
w = Workbook.getWorkbook(f);
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sh = w.getSheet(0);
c = sh.getCell(1, row_index);
dynamicValue = c.getContents() + "DYNAMIC_FIELD";
w.close();
return dynamicValue;
}
}



Kindly look into it and please tell me what the error is..
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Joshi wrote:The code that I am using is:
...
Kindly look into it and please tell me what the error is.

First: Please look at the UseCodeTags page (←click), paying particular attention to the bit about not writing long lines, and edit your post accordingly.

Second: Nobody is likely to go through that much code. Presumably, you have some idea where the error is occurring, so just post the relevant section(s). I suggest you also take a look at the SSCCE page (←click).

Winston
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additionally please also post your stack trace.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Joshi wrote:Kindly look into it and please tell me what the error is..

You already told us what the error is - an array out of bounds exception.

The stack trace will tell you what line it happened on. Look at that line to see what array is being accessed. you can always put in System.out.println() statements to print out what index you are trying to access and how many elements are in the array.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!