Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
CampinCarl
Graphmaster Gerbil
Topic Author
Posts: 1363
Joined: Mon Jul 04, 2005 9:53 pm

Issues That Make Me Feel Like an Idiot in Java

Sat Dec 13, 2008 5:00 pm

EDIT: I fixed this one, please see next post for my latest issue that makes me feel like an idiot in Java
Well, for work I've been writing code to make these certificates of calibration straight from the database files in which we store the test data, and I've been using the Apache POI to do this. I've gotten pretty far, and I've been moving along fairly quickly once I figured things out. However, I ran into this issue early on and skipped it, hoping I could find a fix for it once I was done with the rest of it. The issue is thus: the example code provided to insert an image doesn't work. The sample code can be viewed here. When I compile all my code, the snippet below yields two errors:
1) The method createDrawingPatriarch() is undefined for the type Sheet
2) The method createClientAnchor() is undefined for the type CreationHelper
Which is an obvious lie, because I scoured through the Javadocs and found the methods right where they're supposed to be. Does anyone know why this is happening, and if so how do I fix it?

//Add the logo image:
        //Add picture data to the workbook
        InputStream logo = new FileInputStream("Logo.jpeg");
        byte[] bytes = IOUtils.toByteArray(logo);
        int logoIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
        logo.close();
        CreationHelper helper = wb.getCreationHelper();
        //Create drawing "patriarch" the top level container for all shapes
        HSSFPatriarch logoPat = sheet.createDrawingPatriarch(); //error 1
        //Add a picture shape
        HSSFClientAnchor logoAnchor = helper.createClientAnchor(); //error 2
        //Set top-left corner of the picture, all calls of resize() will operate relative to this corner
        logoAnchor.setCol1((short)0);
        logoAnchor.setRow1(1);
        HSSFPicture logoImg = logoPat.createPicture(logoAnchor, logoIdx);


I tried to comment things as well as possible. edit: I suppose I should fend off the "JFGI" comments with: I've spent about 2 hours wandering around google results trying different things; nothing has worked.
Last edited by CampinCarl on Wed Dec 17, 2008 5:44 pm, edited 2 times in total.
Gigabyte AB350M Gaming-3 | R7 1700X | 2x8 GB Corsair Vengeance DDR4-3200 (@DDR4-2933)| Samsung 960 Evo 1TB SSD | Gigabyte GTX1080 | Win 10 Pro x86-64
 
CampinCarl
Graphmaster Gerbil
Topic Author
Posts: 1363
Joined: Mon Jul 04, 2005 9:53 pm

Re: Insert Image with Apache POI doesn't seem to work

Wed Dec 17, 2008 5:35 pm

Well, I figured that part out earlier today. I had to rewrite it in a totally different manner and use different commands. Go go gadget proper examples!

Anyway, I've run into a new problem. This problem arises while I was trying to modularize (is that even a word?) the code I have set up because once I'm done writing everything we're going to bring it in to the main program. A piece of code I changed was:
CellStyle tableStyle = wb.createCellStyle();
        Font tableFont = wb.createFont();
        tableFont.setFontName("Arial");
        tableFont.setFontHeightInPoints((short)12);
        tableStyle.setFont(tableFont);
        tableStyle.setAlignment(CellStyle.ALIGN_LEFT);
        tableStyle.setBorderRight(CellStyle.BORDER_THIN);
        tableStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
        tableStyle.setBorderLeft(CellStyle.BORDER_THIN);
        tableStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        tableStyle.setBorderTop(CellStyle.BORDER_THIN);
        tableStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        tableStyle.setBorderBottom(CellStyle.BORDER_THIN);
        tableStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
       for(int i = 5; i < 12; i++)
        {
            for(int k = 6; k 8); k++)
            {
                Row tableRow = certSheet.getRow(i);
                Cell tableCell = tableRow.getCell(k);
                tableCell.setCellStyle(tableStyle);
                if(dataTable[i-5][k-6] instanceof Integer)
                {
                    tableCell.setCellValue((Integer)dataTable[i-5][k-6]);
                }
                else if (dataTable[i-5][k-6] instanceof String)
                {
                    tableCell.setCellValue((String)dataTable[i-5][k-6]);
                }
                else if(dataTable[i-5][k-6] instanceof Double)
                {
                   tableCell.setCellValue((Double)dataTable[i-5][k-6]);
                }
            }
        }

which was previously residing in the method make5PtUnampCert() and it worked just fine and dandy.
I brought it out of the method, and made it it's own private method so that it now looks like:
private static void addNPointCalibTable(Sheet certSheet, Workbook wb, Object[][] dataTable)
    {
       CellStyle tableStyle = wb.createCellStyle();
        Font tableFont = wb.createFont();
        tableFont.setFontName("Arial");
        tableFont.setFontHeightInPoints((short)12);
        tableStyle.setFont(tableFont);
        tableStyle.setAlignment(CellStyle.ALIGN_LEFT);
        tableStyle.setBorderRight(CellStyle.BORDER_THIN);
        tableStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
        tableStyle.setBorderLeft(CellStyle.BORDER_THIN);
        tableStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        tableStyle.setBorderTop(CellStyle.BORDER_THIN);
        tableStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        tableStyle.setBorderBottom(CellStyle.BORDER_THIN);
        tableStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
       for(int i = 0; i < Array.getLength(dataTable); i++)
        {
            for(int k = 0; k < Array.getLength(dataTable[i]); k++)
            {
                Row tableRow = certSheet.getRow(i+5);
                Cell tableCell = tableRow.getCell(k+6);
                tableCell.setCellStyle(tableStyle); [1]
                if(dataTable[i][k] instanceof Integer)
                {
                    tableCell.setCellValue((Integer)dataTable[i][k]); [2]
                }
                else if (dataTable[i][k] instanceof String)
                {
                    tableCell.setCellValue((String)dataTable[i][k]);
                }
                else if(dataTable[i][k] instanceof Double)
                {
                   tableCell.setCellValue((Double)dataTable[i][k]);[3]
                }
            }
        }
    }


which errors at [1], [2], and [3] all with the same NullPointerException.
I am completely lost as to why this is happening. FWIW, the call being used in the make5PtUnampCert() method is:
addNPointCalibTable(certSheet, wb, calibDataTable);
where calibDataTable is: Object[][] calibDataTable = {{"Load", "(lbs)", 0.0, 0.0, 0.0, 0.0, 0.0,0.0},{"Output", "mV/V", 0.0, 0.0, 0.0, 0.0, 0.0,0.0}};

Lemme throw out there that if I comment out those lines which throw errors, everything compiles and runs fine, except that the calibration table is a) not outlined and b) only has the string values in it
Gigabyte AB350M Gaming-3 | R7 1700X | 2x8 GB Corsair Vengeance DDR4-3200 (@DDR4-2933)| Samsung 960 Evo 1TB SSD | Gigabyte GTX1080 | Win 10 Pro x86-64
 
arsenhazzard
Gerbil First Class
Posts: 169
Joined: Thu Oct 18, 2007 4:30 pm

Re: Issues That Make Me Feel Like an Idiot in Java

Wed Dec 17, 2008 6:44 pm

For your first problem, you shouldn't need to rewrite anything unless you're using POI 3.2. Just make sure you're using the correct Sheet class (org.​apache.​poi.​ss.​usermodel.Sheet). Everything compiled as is with 3.5b4.

For your second problem, what's your sheet look like? If your sheet stays the same between the two code segments, you're attempting to read cells that may not be defined properly. You're getting rows 5-11, columns 6-7 in the first bit and rows 5-6, columns 6-13 (both 0-indexed) in the second. I'm guessing you want i < dataTable[0].length and k < dataTable.length.
Last edited by arsenhazzard on Wed Dec 17, 2008 7:02 pm, edited 1 time in total.
 
SNM
Emperor Gerbilius I
Posts: 6209
Joined: Fri Dec 30, 2005 10:37 am

Re: Issues That Make Me Feel Like an Idiot in Java

Wed Dec 17, 2008 6:48 pm

I can't be sure, since I am sleep-depped, not familiar with the classes you're using, and not really familiar with database programming. However, it looks to me like your loop bounds are very wrong.
In the first code snippet, you iterate over the certsheet rows 5 to 11 and columns 6 to 8 (I think -- you messed up copying the for loop header unless there's some strange syntax I'm not aware of) and look backwards. In the second example you access the certsheet by iterating over the entire length of the datatable, which is 2x8. That right there means you run the loop more often in the second example, which guarantees you're going past your usable data. Beyond that, in the second loop you tried to compensate for your different loop bounds by looking forward from your counters, but since you're iterating UP TO the last element in the datatable and then looking forward, you're going to go past your bounds.
My assumption is that you don't error out on the (instanceOf string) portion because you only encounter strings in the early part of the datatable where you're accessing memory still in-bounds; whereas the setCellStyle, (instanceOf Integer), and (instanceOf Double) are all called farther into the table. In some languages just trying to access out-of-bounds indices would throw an exception of some kind, but since you're using a getRow() and getCell() method you're probably getting back a null pointer or something to indicate non-existence.
Core i7 920, 3x2GB Corsair DDR3 1600, 80GB X25-M, 1TB WD Caviar Black, MSI X58 Pro-E, Radeon 4890, Cooler Master iGreen 600, Antec P183, opticals
 
CampinCarl
Graphmaster Gerbil
Topic Author
Posts: 1363
Joined: Mon Jul 04, 2005 9:53 pm

Re: Issues That Make Me Feel Like an Idiot in Java

Wed Dec 17, 2008 11:37 pm

arsenhazzard wrote:
For your first problem, you shouldn't need to rewrite anything unless you're using POI 3.2. Just make sure you're using the correct Sheet class (org.​apache.​poi.​ss.​usermodel.Sheet). Everything compiled as is with 3.5b4.

For your second problem, what's your sheet look like? If your sheet stays the same between the two code segments, you're attempting to read cells that may not be defined properly. You're getting rows 5-11, columns 6-7 in the first bit and rows 5-6, columns 6-13 (both 0-indexed) in the second. I'm guessing you want i < dataTable[0].length and k < dataTable.length.


Interesting. I am using 3.5b4.

Yeah, that was a whoopsie on my part. It was putting it 2 rows x 8 columns where I meant it to be 8 rows x 2 columns. I fixed that, but the errors still remain.

Also, SNM that last bit makes sense as to why it would be NullPointerException instead of ArrayIndexOutOfBounds. I should also probably point out that the first snippet was coded only for 5 point calibration tables, however the second was me attempting to make one where I could use it on custom sheets that require, say, 10 or 12 point calibration tables. Hence the change in the structure of the code.
Gigabyte AB350M Gaming-3 | R7 1700X | 2x8 GB Corsair Vengeance DDR4-3200 (@DDR4-2933)| Samsung 960 Evo 1TB SSD | Gigabyte GTX1080 | Win 10 Pro x86-64

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On