Store cell values to Object The 2019 Stack Overflow Developer Survey Results Are InHow can I read numeric strings in Excel cells as string (not numbers)?How to get the formula cell value(data) using apache poiHow to get the Cell value of A1(Cell Address) using apache poi 3.6POI - How do I set cell value to Date and apply default Excel date format?Apache POI : How to format numeric cell valuesIdentify objects in seleniumCannot get a text value from a numeric cell “Poi”How to get value of checkbox in capybara?How can I update values in the Blank Cells in the Excel sheet using HSSF using Java?java code reading excel cell value which is (91.58666666666% rounded off and mentioned as 91.58) as zero, using PoI
Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?
What was the last CPU that did not have the x87 floating-point unit built in?
Button changing its text & action. Good or terrible?
Keeping a retro style to sci-fi spaceships?
If my opponent casts Ultimate Price on my Phantasmal Bear, can I save it by casting Snap or Curfew?
Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?
What is the motivation for a law requiring 2 parties to consent for recording a conversation
Why not take a picture of a closer black hole?
What is the most efficient way to store a numeric range?
Mathematics of imaging the black hole
Loose spokes after only a few rides
Is there a way to generate a uniformly distributed point on a sphere from a fixed amount of random real numbers?
Why isn't the circumferential light around the M87 black hole's event horizon symmetric?
Will it cause any balance problems to have PCs level up and gain the benefits of a long rest mid-fight?
What does Linus Torvalds mean when he says that Git "never ever" tracks a file?
Is it safe to harvest rainwater that fell on solar panels?
Is an up-to-date browser secure on an out-of-date OS?
If climate change impact can be observed in nature, has that had any effect on rural, i.e. farming community, perception of the scientific consensus?
How to add class in ko template in magento2
What is this sharp, curved notch on my knife for?
What can I do if neighbor is blocking my solar panels intentionally
What is preventing me from simply constructing a hash that's lower than the current target?
How come people say “Would of”?
Why can't devices on different VLANs, but on the same subnet, communicate?
Store cell values to Object
The 2019 Stack Overflow Developer Survey Results Are InHow can I read numeric strings in Excel cells as string (not numbers)?How to get the formula cell value(data) using apache poiHow to get the Cell value of A1(Cell Address) using apache poi 3.6POI - How do I set cell value to Date and apply default Excel date format?Apache POI : How to format numeric cell valuesIdentify objects in seleniumCannot get a text value from a numeric cell “Poi”How to get value of checkbox in capybara?How can I update values in the Blank Cells in the Excel sheet using HSSF using Java?java code reading excel cell value which is (91.58666666666% rounded off and mentioned as 91.58) as zero, using PoI
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm currently new to TestNG using java. I'm trying to read the values from an excel using poi apache 4.0
public static void read2dRowExcelFile2(String filePath) throws IOException
try
FileInputStream fis = new FileInputStream(new File(filePath));
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheet("PerLocation");
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
int arrNo1 = 0;
for (int i = 1; i <= sheet.getLastRowNum(); i++)
Row row = sheet.getRow(i);
int arrNo2 = 0;
for (int j = 0; j < row.getLastCellNum(); j++)
String cellValue = row.getCell(j).getStringCellValue();
System.out.println(acellValue);
//tableArr[arrNo1][arrNo2] = cellValue;
System.out.println("test");
arrNo2++;
arrNo1++;
wb.close();
catch (Exception e)
e.printStackTrace();
Code above displays the values on the console. My goal is to store those values to an Object. Something like [London, Blue,Tokyo,Yellow,Manila,Red] so I can pass them to a dataProvider
If I run the code above, it displays :
London
BLue
Tokyo
Yellow
Manila
Red
But If i uncomment this line :
//tableArr[arrNo1][arrNo2] = cellValue;
The output is only :
London
03-08-19 : After I enabled stacktrace, it says : java.lang.NullPointerException
which pertains to this code :
tableArr[arrNo1][arrNo2] = cellValue;
automation apache-poi qa
add a comment |
I'm currently new to TestNG using java. I'm trying to read the values from an excel using poi apache 4.0
public static void read2dRowExcelFile2(String filePath) throws IOException
try
FileInputStream fis = new FileInputStream(new File(filePath));
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheet("PerLocation");
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
int arrNo1 = 0;
for (int i = 1; i <= sheet.getLastRowNum(); i++)
Row row = sheet.getRow(i);
int arrNo2 = 0;
for (int j = 0; j < row.getLastCellNum(); j++)
String cellValue = row.getCell(j).getStringCellValue();
System.out.println(acellValue);
//tableArr[arrNo1][arrNo2] = cellValue;
System.out.println("test");
arrNo2++;
arrNo1++;
wb.close();
catch (Exception e)
e.printStackTrace();
Code above displays the values on the console. My goal is to store those values to an Object. Something like [London, Blue,Tokyo,Yellow,Manila,Red] so I can pass them to a dataProvider
If I run the code above, it displays :
London
BLue
Tokyo
Yellow
Manila
Red
But If i uncomment this line :
//tableArr[arrNo1][arrNo2] = cellValue;
The output is only :
London
03-08-19 : After I enabled stacktrace, it says : java.lang.NullPointerException
which pertains to this code :
tableArr[arrNo1][arrNo2] = cellValue;
automation apache-poi qa
1
There is an exception thrown which you are silently ignoring. Pute.printStackTrace();
into yourcatch
block and you will see.
– Axel Richter
Mar 8 at 12:23
Hi. Thanks @AxelRichter. I didn't notice that, thanks for this tip.. if you don't mind? May I ask if i'm doing the right thing on storing values to an Object :code
tableArr[arrNo1][arrNo2] = cellValue;code
after i enable the stacktrace, its says java.lang.NullPointerException pertaining to this line. I tried printing out my variables, and it has values. sorry to bother.
– Beef
Mar 8 at 13:08
AfterObject[][] tableArr = new String[sheet.getLastRowNum() + 1][];
what do you think arre the dimensions of yourObject[][] tableArr
?
– Axel Richter
Mar 8 at 13:51
@AxelRichter. I think I got it now, the null refers to the second dimension of the object. Thanks!
– Beef
Mar 8 at 13:57
Correct. And because of the static behavior of array dimensions, you should better using a Collection instead. AList<List<String>> tableList
for example.
– Axel Richter
Mar 8 at 14:08
add a comment |
I'm currently new to TestNG using java. I'm trying to read the values from an excel using poi apache 4.0
public static void read2dRowExcelFile2(String filePath) throws IOException
try
FileInputStream fis = new FileInputStream(new File(filePath));
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheet("PerLocation");
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
int arrNo1 = 0;
for (int i = 1; i <= sheet.getLastRowNum(); i++)
Row row = sheet.getRow(i);
int arrNo2 = 0;
for (int j = 0; j < row.getLastCellNum(); j++)
String cellValue = row.getCell(j).getStringCellValue();
System.out.println(acellValue);
//tableArr[arrNo1][arrNo2] = cellValue;
System.out.println("test");
arrNo2++;
arrNo1++;
wb.close();
catch (Exception e)
e.printStackTrace();
Code above displays the values on the console. My goal is to store those values to an Object. Something like [London, Blue,Tokyo,Yellow,Manila,Red] so I can pass them to a dataProvider
If I run the code above, it displays :
London
BLue
Tokyo
Yellow
Manila
Red
But If i uncomment this line :
//tableArr[arrNo1][arrNo2] = cellValue;
The output is only :
London
03-08-19 : After I enabled stacktrace, it says : java.lang.NullPointerException
which pertains to this code :
tableArr[arrNo1][arrNo2] = cellValue;
automation apache-poi qa
I'm currently new to TestNG using java. I'm trying to read the values from an excel using poi apache 4.0
public static void read2dRowExcelFile2(String filePath) throws IOException
try
FileInputStream fis = new FileInputStream(new File(filePath));
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheet("PerLocation");
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
int arrNo1 = 0;
for (int i = 1; i <= sheet.getLastRowNum(); i++)
Row row = sheet.getRow(i);
int arrNo2 = 0;
for (int j = 0; j < row.getLastCellNum(); j++)
String cellValue = row.getCell(j).getStringCellValue();
System.out.println(acellValue);
//tableArr[arrNo1][arrNo2] = cellValue;
System.out.println("test");
arrNo2++;
arrNo1++;
wb.close();
catch (Exception e)
e.printStackTrace();
Code above displays the values on the console. My goal is to store those values to an Object. Something like [London, Blue,Tokyo,Yellow,Manila,Red] so I can pass them to a dataProvider
If I run the code above, it displays :
London
BLue
Tokyo
Yellow
Manila
Red
But If i uncomment this line :
//tableArr[arrNo1][arrNo2] = cellValue;
The output is only :
London
03-08-19 : After I enabled stacktrace, it says : java.lang.NullPointerException
which pertains to this code :
tableArr[arrNo1][arrNo2] = cellValue;
automation apache-poi qa
automation apache-poi qa
edited Mar 10 at 13:01
Krishnan Mahadevan
9,09831744
9,09831744
asked Mar 8 at 11:11
BeefBeef
62
62
1
There is an exception thrown which you are silently ignoring. Pute.printStackTrace();
into yourcatch
block and you will see.
– Axel Richter
Mar 8 at 12:23
Hi. Thanks @AxelRichter. I didn't notice that, thanks for this tip.. if you don't mind? May I ask if i'm doing the right thing on storing values to an Object :code
tableArr[arrNo1][arrNo2] = cellValue;code
after i enable the stacktrace, its says java.lang.NullPointerException pertaining to this line. I tried printing out my variables, and it has values. sorry to bother.
– Beef
Mar 8 at 13:08
AfterObject[][] tableArr = new String[sheet.getLastRowNum() + 1][];
what do you think arre the dimensions of yourObject[][] tableArr
?
– Axel Richter
Mar 8 at 13:51
@AxelRichter. I think I got it now, the null refers to the second dimension of the object. Thanks!
– Beef
Mar 8 at 13:57
Correct. And because of the static behavior of array dimensions, you should better using a Collection instead. AList<List<String>> tableList
for example.
– Axel Richter
Mar 8 at 14:08
add a comment |
1
There is an exception thrown which you are silently ignoring. Pute.printStackTrace();
into yourcatch
block and you will see.
– Axel Richter
Mar 8 at 12:23
Hi. Thanks @AxelRichter. I didn't notice that, thanks for this tip.. if you don't mind? May I ask if i'm doing the right thing on storing values to an Object :code
tableArr[arrNo1][arrNo2] = cellValue;code
after i enable the stacktrace, its says java.lang.NullPointerException pertaining to this line. I tried printing out my variables, and it has values. sorry to bother.
– Beef
Mar 8 at 13:08
AfterObject[][] tableArr = new String[sheet.getLastRowNum() + 1][];
what do you think arre the dimensions of yourObject[][] tableArr
?
– Axel Richter
Mar 8 at 13:51
@AxelRichter. I think I got it now, the null refers to the second dimension of the object. Thanks!
– Beef
Mar 8 at 13:57
Correct. And because of the static behavior of array dimensions, you should better using a Collection instead. AList<List<String>> tableList
for example.
– Axel Richter
Mar 8 at 14:08
1
1
There is an exception thrown which you are silently ignoring. Put
e.printStackTrace();
into your catch
block and you will see.– Axel Richter
Mar 8 at 12:23
There is an exception thrown which you are silently ignoring. Put
e.printStackTrace();
into your catch
block and you will see.– Axel Richter
Mar 8 at 12:23
Hi. Thanks @AxelRichter. I didn't notice that, thanks for this tip.. if you don't mind? May I ask if i'm doing the right thing on storing values to an Object :
code
tableArr[arrNo1][arrNo2] = cellValue;code
after i enable the stacktrace, its says java.lang.NullPointerException pertaining to this line. I tried printing out my variables, and it has values. sorry to bother.– Beef
Mar 8 at 13:08
Hi. Thanks @AxelRichter. I didn't notice that, thanks for this tip.. if you don't mind? May I ask if i'm doing the right thing on storing values to an Object :
code
tableArr[arrNo1][arrNo2] = cellValue;code
after i enable the stacktrace, its says java.lang.NullPointerException pertaining to this line. I tried printing out my variables, and it has values. sorry to bother.– Beef
Mar 8 at 13:08
After
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
what do you think arre the dimensions of your Object[][] tableArr
?– Axel Richter
Mar 8 at 13:51
After
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
what do you think arre the dimensions of your Object[][] tableArr
?– Axel Richter
Mar 8 at 13:51
@AxelRichter. I think I got it now, the null refers to the second dimension of the object. Thanks!
– Beef
Mar 8 at 13:57
@AxelRichter. I think I got it now, the null refers to the second dimension of the object. Thanks!
– Beef
Mar 8 at 13:57
Correct. And because of the static behavior of array dimensions, you should better using a Collection instead. A
List<List<String>> tableList
for example.– Axel Richter
Mar 8 at 14:08
Correct. And because of the static behavior of array dimensions, you should better using a Collection instead. A
List<List<String>> tableList
for example.– Axel Richter
Mar 8 at 14:08
add a comment |
1 Answer
1
active
oldest
votes
From your code,
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
At the time of initialization of array, your are setting size of first dimension but not the second . You need to initialize array for second dimension before you access it.
Refer below example:
public static void main(String[] args)
//read rows size
int numOfRows = 3;
String[][] tableArr = new String[numOfRows][];
for (int row = 0; row <3; row++)
//read columns size
int numOfColsInRow = 3;
tableArr[row]=new String[numOfColsInRow];
for (int col = 0; col < 3; col++)
String cellValue = "cell-" + row+""+col;//read cell value
tableArr[row][col] = cellValue;
for(String[] row: tableArr)
System.out.println(Arrays.toString(row));
Running above code with generate expected output:
[cell-00, cell-01, cell-02]
[cell-10, cell-11, cell-12]
[cell-20, cell-21, cell-22]
To reproduce your problem you can try commenting line which initialize array for second dimension in the code and you will see Exception in thread "main" java.lang.NullPointerException
.
//tableArr[row]=new String[numOfColsInRow];
To avoid all such issues you also can check if any exiting TestNG data-provider extension satisfies your need.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55061999%2fstore-cell-values-to-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From your code,
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
At the time of initialization of array, your are setting size of first dimension but not the second . You need to initialize array for second dimension before you access it.
Refer below example:
public static void main(String[] args)
//read rows size
int numOfRows = 3;
String[][] tableArr = new String[numOfRows][];
for (int row = 0; row <3; row++)
//read columns size
int numOfColsInRow = 3;
tableArr[row]=new String[numOfColsInRow];
for (int col = 0; col < 3; col++)
String cellValue = "cell-" + row+""+col;//read cell value
tableArr[row][col] = cellValue;
for(String[] row: tableArr)
System.out.println(Arrays.toString(row));
Running above code with generate expected output:
[cell-00, cell-01, cell-02]
[cell-10, cell-11, cell-12]
[cell-20, cell-21, cell-22]
To reproduce your problem you can try commenting line which initialize array for second dimension in the code and you will see Exception in thread "main" java.lang.NullPointerException
.
//tableArr[row]=new String[numOfColsInRow];
To avoid all such issues you also can check if any exiting TestNG data-provider extension satisfies your need.
add a comment |
From your code,
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
At the time of initialization of array, your are setting size of first dimension but not the second . You need to initialize array for second dimension before you access it.
Refer below example:
public static void main(String[] args)
//read rows size
int numOfRows = 3;
String[][] tableArr = new String[numOfRows][];
for (int row = 0; row <3; row++)
//read columns size
int numOfColsInRow = 3;
tableArr[row]=new String[numOfColsInRow];
for (int col = 0; col < 3; col++)
String cellValue = "cell-" + row+""+col;//read cell value
tableArr[row][col] = cellValue;
for(String[] row: tableArr)
System.out.println(Arrays.toString(row));
Running above code with generate expected output:
[cell-00, cell-01, cell-02]
[cell-10, cell-11, cell-12]
[cell-20, cell-21, cell-22]
To reproduce your problem you can try commenting line which initialize array for second dimension in the code and you will see Exception in thread "main" java.lang.NullPointerException
.
//tableArr[row]=new String[numOfColsInRow];
To avoid all such issues you also can check if any exiting TestNG data-provider extension satisfies your need.
add a comment |
From your code,
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
At the time of initialization of array, your are setting size of first dimension but not the second . You need to initialize array for second dimension before you access it.
Refer below example:
public static void main(String[] args)
//read rows size
int numOfRows = 3;
String[][] tableArr = new String[numOfRows][];
for (int row = 0; row <3; row++)
//read columns size
int numOfColsInRow = 3;
tableArr[row]=new String[numOfColsInRow];
for (int col = 0; col < 3; col++)
String cellValue = "cell-" + row+""+col;//read cell value
tableArr[row][col] = cellValue;
for(String[] row: tableArr)
System.out.println(Arrays.toString(row));
Running above code with generate expected output:
[cell-00, cell-01, cell-02]
[cell-10, cell-11, cell-12]
[cell-20, cell-21, cell-22]
To reproduce your problem you can try commenting line which initialize array for second dimension in the code and you will see Exception in thread "main" java.lang.NullPointerException
.
//tableArr[row]=new String[numOfColsInRow];
To avoid all such issues you also can check if any exiting TestNG data-provider extension satisfies your need.
From your code,
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
At the time of initialization of array, your are setting size of first dimension but not the second . You need to initialize array for second dimension before you access it.
Refer below example:
public static void main(String[] args)
//read rows size
int numOfRows = 3;
String[][] tableArr = new String[numOfRows][];
for (int row = 0; row <3; row++)
//read columns size
int numOfColsInRow = 3;
tableArr[row]=new String[numOfColsInRow];
for (int col = 0; col < 3; col++)
String cellValue = "cell-" + row+""+col;//read cell value
tableArr[row][col] = cellValue;
for(String[] row: tableArr)
System.out.println(Arrays.toString(row));
Running above code with generate expected output:
[cell-00, cell-01, cell-02]
[cell-10, cell-11, cell-12]
[cell-20, cell-21, cell-22]
To reproduce your problem you can try commenting line which initialize array for second dimension in the code and you will see Exception in thread "main" java.lang.NullPointerException
.
//tableArr[row]=new String[numOfColsInRow];
To avoid all such issues you also can check if any exiting TestNG data-provider extension satisfies your need.
answered Mar 11 at 20:20
user861594user861594
3,07132034
3,07132034
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55061999%2fstore-cell-values-to-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
There is an exception thrown which you are silently ignoring. Put
e.printStackTrace();
into yourcatch
block and you will see.– Axel Richter
Mar 8 at 12:23
Hi. Thanks @AxelRichter. I didn't notice that, thanks for this tip.. if you don't mind? May I ask if i'm doing the right thing on storing values to an Object :
code
tableArr[arrNo1][arrNo2] = cellValue;code
after i enable the stacktrace, its says java.lang.NullPointerException pertaining to this line. I tried printing out my variables, and it has values. sorry to bother.– Beef
Mar 8 at 13:08
After
Object[][] tableArr = new String[sheet.getLastRowNum() + 1][];
what do you think arre the dimensions of yourObject[][] tableArr
?– Axel Richter
Mar 8 at 13:51
@AxelRichter. I think I got it now, the null refers to the second dimension of the object. Thanks!
– Beef
Mar 8 at 13:57
Correct. And because of the static behavior of array dimensions, you should better using a Collection instead. A
List<List<String>> tableList
for example.– Axel Richter
Mar 8 at 14:08