• Post Reply Bookmark Topic Watch Topic
  • New Topic

JXL: How to check empty Excel cell using while loop?  RSS feed

 
Zulfi Khan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have written a java program to read excel sheets. Its reading a cell which I want. I am using a while loop. My while loop should stop when it encounters a null value. However its not doing this. Its crashing when number of rows are beyond 100. I think my sheet has 100 rows.
Some body please guide me why my loop doesn’t stop when it encounters a null value. My code is:



Output is:


Warning:  Polygon Object on sheet "Sheet1" not supported - omitting
Warning:  Polygon Object on sheet "Sheet1" not supported - omitting
2015-SE-001
2015-SE-001
2015-SE-002
2015-SE-003
2015-SE-004
2015-SE-005
2015-SE-006
2015-SE-007
2015-SE-008
2015-SE-009
2015-SE-010
2015-SE-011
2015-SE-012
2015-SE-013
2015-SE-014
2015-SE-015
2015-SE-016
2015-SE-017
2015-SE-018
2015-SE-019
2015-SE-020
2015-SE-021
2015-SE-022
2015-SE-023
2015-SE-024
2015-SE-025
2015-SE-026
2015-SE-027
2015-SE-028
2015-SE-029
2015-SE-030
2015-SE-031
2015-SE-032
2015-SE-033
2015-SE-034
2015-SE-035
2015-SE-036
2015-SE-037
2015-SE-038
2015-SE-039
2015-SE-040
2015-SE-041
2015-SE-042
2015-SE-043
2015-SE-044
2015-SE-045
2015-SE-046
2015-SE-047
2015-SE-048
2015-SE-049
2015-SE-051
2015-SE-130
2015-SE-138








































Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
        at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
        at ReadExcelSheetMain2.readExcel(ReadExcelSheetMain2.java:41)
        at ReadExcelSheetMain2.main(ReadExcelSheetMain2.java:56)




Zulfi.
 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your whole loop it looks like you are grabbing the value before you start your loop, then you check to see if it's null.   Then it looks like you grab the same cell again before interating the row.

I have been able to test it, but if you do your Row++ before the print I think you will get a better result.
 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shouldn't try to respond from my phone.   I mean in your while loop, check where you are grabbing your values.   I think you need to add your row++ before grabbing the next row.

I also meant I haven't been able to test it, but just reading the code I think the iteration is happening in the wrong place.
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you know the number of rows in the sheet then why not use that as one way of limiting the looping, along with a break when you encounter a null cell?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The docs for getContents() say that it returns a String.  It doesn't say anything about going null for an empty cell.  I would test to see if the String is empty:
 
Zulfi Khan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for response and proper reasoning,
I have modified my code using for loop:

but still its not checking the null value:
I am getting following output:
Warning:  Polygon Object on sheet "Sheet1" not supported - omitting
Warning:  Polygon Object on sheet "Sheet1" not supported - omitting
rollNo2015-SE-001
rollNo2015-SE-002
rollNo2015-SE-003
rollNo2015-SE-004
rollNo2015-SE-005
rollNo2015-SE-006
rollNo2015-SE-007
rollNo2015-SE-008
rollNo2015-SE-009
rollNo2015-SE-010
rollNo2015-SE-011
rollNo2015-SE-012
rollNo2015-SE-013
rollNo2015-SE-014
rollNo2015-SE-015
rollNo2015-SE-016
rollNo2015-SE-017
rollNo2015-SE-018
rollNo2015-SE-019
rollNo2015-SE-020
rollNo2015-SE-021
rollNo2015-SE-022
rollNo2015-SE-023
rollNo2015-SE-024
rollNo2015-SE-025
rollNo2015-SE-026
rollNo2015-SE-027
rollNo2015-SE-028
rollNo2015-SE-029
rollNo2015-SE-030
rollNo2015-SE-031
rollNo2015-SE-032
rollNo2015-SE-033
rollNo2015-SE-034
rollNo2015-SE-035
rollNo2015-SE-036
rollNo2015-SE-037
rollNo2015-SE-038
rollNo2015-SE-039
rollNo2015-SE-040
rollNo2015-SE-041
rollNo2015-SE-042
rollNo2015-SE-043
rollNo2015-SE-044
rollNo2015-SE-045
rollNo2015-SE-046
rollNo2015-SE-047
rollNo2015-SE-048
rollNo2015-SE-049
rollNo2015-SE-051
rollNo2015-SE-130
rollNo2015-SE-138
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo
rollNo

Some body please guide me.

Zulfi.
 
Zulfi Khan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I found the code to check empty on stackoverflow:

for ( row = 8; row < totalNoOfRows; row++) {
                   rollNo = sh.getCell(col, row).getContents();
                   boolean isEmpty = rollNo == null || rollNo.trim().length() == 0;
                   if (isEmpty) {
                      break;
                   }
                   System.out.println("rollNo" + rollNo);
                  
                }

My output is correct now:

Warning:  Polygon Object on sheet "Sheet1" not supported - omitting
Warning:  Polygon Object on sheet "Sheet1" not supported - omitting
rollNo2015-SE-001
rollNo2015-SE-002
rollNo2015-SE-003
rollNo2015-SE-004
rollNo2015-SE-005
rollNo2015-SE-006
rollNo2015-SE-007
rollNo2015-SE-008
rollNo2015-SE-009
rollNo2015-SE-010
rollNo2015-SE-011
rollNo2015-SE-012
rollNo2015-SE-013
rollNo2015-SE-014
rollNo2015-SE-015
rollNo2015-SE-016
rollNo2015-SE-017
rollNo2015-SE-018
rollNo2015-SE-019
rollNo2015-SE-020
rollNo2015-SE-021
rollNo2015-SE-022
rollNo2015-SE-023
rollNo2015-SE-024
rollNo2015-SE-025
rollNo2015-SE-026
rollNo2015-SE-027
rollNo2015-SE-028
rollNo2015-SE-029
rollNo2015-SE-030
rollNo2015-SE-031
rollNo2015-SE-032
rollNo2015-SE-033
rollNo2015-SE-034
rollNo2015-SE-035
rollNo2015-SE-036
rollNo2015-SE-037
rollNo2015-SE-038
rollNo2015-SE-039
rollNo2015-SE-040
rollNo2015-SE-041
rollNo2015-SE-042
rollNo2015-SE-043
rollNo2015-SE-044
rollNo2015-SE-045
rollNo2015-SE-046
rollNo2015-SE-047
rollNo2015-SE-048
rollNo2015-SE-049
rollNo2015-SE-051
rollNo2015-SE-130
rollNo2015-SE-138

>

Thanks for your cooperation.

Zulfi.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!