How to add blank lines to the end of a Document?

I'm working on a text editor and it needs to copy some text from one JTextArea to another at the same line, but when one adds a line I can't copy it over with the code I'm using because the document doesnt have enough Elements. My code is
        addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent ce) {
                JTextComponent comp = (JTextComponent)ce.getSource();
                int pos = comp.getCaretPosition();
                Element root = comp.getDocument().getDefaultRootElement();
                lineNum = root.getElementIndex(pos);
                Element line = root.getElement(lineNum);
                try {
                    String text = comp.getDocument().getText(line.getStartOffset(), line.getEndOffset() - line.getStartOffset());
                    CodeDoc document = (CodeDoc) other.getDocument();
                    text = ((CodeView) comp).hasAssm ? CodeView.fromAssm(text) : CodeView.toAssm(text);
                    document.replace(document.getDefaultRootElement().getElement(lineNum).getStartOffset(), document.getDefaultRootElement().getElement(lineNum).getEndOffset(), text, null);
                } catch (BadLocationException e) {
                    throw new RuntimeException(e);
                }
            }
        });
Was this page helpful?