diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java index 9cc2389c7..e41867996 100644 --- a/src/java/org/apache/poi/hssf/model/Workbook.java +++ b/src/java/org/apache/poi/hssf/model/Workbook.java @@ -2074,15 +2074,18 @@ public class Workbook implements Model { public PaletteRecord getCustomPalette() { PaletteRecord palette; - Record rec = records.get(records.getPalettepos()); - if (rec instanceof PaletteRecord) - { + int palettePos = records.getPalettepos(); + if (palettePos != -1) { + Record rec = records.get(palettePos); + if (rec instanceof PaletteRecord) { palette = (PaletteRecord) rec; + } else throw new RuntimeException("InternalError: Expected PaletteRecord but got a '"+rec+"'"); } else { palette = createPalette(); - records.add(records.getPalettepos(), palette); + //Add the palette record after the bof which is always the first record + records.add(1, palette); } return palette; } diff --git a/src/java/org/apache/poi/hssf/model/WorkbookRecordList.java b/src/java/org/apache/poi/hssf/model/WorkbookRecordList.java index 0f2fa3254..90eefaefa 100644 --- a/src/java/org/apache/poi/hssf/model/WorkbookRecordList.java +++ b/src/java/org/apache/poi/hssf/model/WorkbookRecordList.java @@ -74,7 +74,7 @@ public class WorkbookRecordList private int namepos = 0; // holds the position of last name record private int supbookpos = 0; // holds the position of sup book private int externsheetPos = 0;// holds the position of the extern sheet - private int palettepos = 0; // hold the position of the palette, if applicable + private int palettepos = -1; // hold the position of the palette, if applicable public void setRecords( List records )