Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It's complaining with the datasheet, It can't find

What I have tried:

<pre>package Testcases;

import Base.Page;
import Listeners.ExtentReport;
import Pages.Actions.BursaryRegister;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;

public class BursaryTest {

    ExtentReport extentReport = new ExtentReport();

    @DataProvider(name = "ReadData")
    public Object[][] ReadData() throws IOException {
        Utilities.DataProvider.ReadData();
        return Utilities.DataProvider.ReadData();
    }

    @Test(dataProvider = "ReadData")
    public void bursaryTest (String Name, String Surname, String Email, String ID, String CellNumber, String Test_Case_Description, String Expected_Results) {

        /*Defining Test Case Scenario*/
        extentReport.start(Test_Case_Description);

        Page.initConfiguration();

        /*Calling the object to access everything*/

        try {
            BursaryRegister bursaryLogin = new BursaryRegister();
            bursaryLogin.login(Name, Surname,Email,ID,CellNumber);
        } catch (Exception e) {
            extentReport.failure("Failed To Login");
            throw new AssertionError("Failed To Login");
        }

        extentReport.success("Logged in successfully ");
        Page.quitBrowser();
    }
}






package Utilities;

import Base.Constants;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;

public class DataProvider {

    public static XSSFWorkbook workbook;
    public static XSSFSheet worksheet;
    public static DataFormatter formatter = new DataFormatter();
    public String Res;
    WriteExcelData.Write obj1 = new WriteExcelData.Write();
    public int DataSet = -1;
    String ColName = "Pass/Fail";
    int col_num;


    @org.testng.annotations.DataProvider
    public static Object[][] ReadData() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(Constants.excelPath); //Excel sheet file location get mentioned here
        workbook = new XSSFWorkbook(fileInputStream); //get my workbook
        worksheet = workbook.getSheet(Constants.DATA_SHEET);// get my sheet from workbook
        XSSFRow Row = worksheet.getRow(0);     //get my Row which start fro
        // m 0

        int RowNum = worksheet.getPhysicalNumberOfRows();// count my number of Rows
        int ColNum = Row.getLastCellNum(); // get last ColNum

        Object Data[][] = new Object[RowNum-1][ColNum]; // pass my  count data in array,
        System.out.println("Number of rows" +RowNum);
        for (int i = Constants.start; i <=Constants.end; i++) //Loop work for Rows,


            try {

                {
                    XSSFRow row = worksheet.getRow(i+1);//remember to change this back to 1

                    for (int j = 0; j < ColNum-1; j++) //Loop work for colNum
                    {
                        if (row == null)
                            Data[i][j] = "";
                        else {
                            XSSFCell cell = row.getCell(j);
                            if (cell == null)
                                Data[i][j] = ""; //if it get Null value it pass no data
                            else {
                                String value = formatter.formatCellValue(cell);
                                Data[i][j] = value; //This formatter get my all values as string i.e integer, float all type data value
                            }

                        }
                    }
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
        return Data;
    }
}
Posted
Updated 20-Aug-20 7:19am
Comments
Patrice T 20-Aug-20 10:43am    
Complete error message also tells you the position of error.
It is an help to find what is wrong.
Richard MacCutchan 20-Aug-20 11:16am    
Which line, what is the complete text of the error message? Please provide proper details of your problem.

The error is pretty explicit:
indexoutofboundsexception : invalid array range: 1 to 1
So somewhere in that code you are using an array that has one element, and trying to access it using array index 1.
Since the array contains one element, the only valid array index is zero.

We can't fix that for you - we don't have any access to your data!
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Adding to OG's response. You are accessing an index which is not there - based on error, second element when there is only one element in array.

Reference: IndexOutOfBoundsException (Java Platform SE 7 )[^]
Quote:
Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.


Now, keeping above in context, to loop through data rows in worksheet, you use some Constants.start and Constants.end but just before that you actually got the number of rows in workbook. Make sure the indexes are in range. Always check the size of your list or array's length before having the logic of accessing them.


For learning on debugging if it helps:
jdb - The Java Debugger[^]
Debugging the Eclipse IDE for Java Developers | The Eclipse Foundation[^]
 
Share this answer
 
v2
Quote:
How to fix java.lang.indexoutofboundsexception : invalid array range: 1 to 1

Complete error message also tells you the position of error.
It is an help to find what is wrong. We can't give you specific help as we can't know what is wrong.
All we can say is that somewhere in code, you try to access an element of an array that do not exist.
Advice: use the debugger to watch your code as it execute and to inspect variable at position of error.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900