1

我正在开发一个向导页面。在该页面中,我将文件转换为字符串并将其显示在文本框中

但我想以我尝试使用以下代码的 java 格式显示文本

import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;
    public class FormatterTest {
        public static void main(String[] args) {
            String code = pagehandlerContent; //the file which im getting and saving it in string
            CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);
            TextEdit textEdit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, code, 0, code.length(), 0, null);
            IDocument doc = new Document(code);
            try {
                textEdit.apply(doc);
                System.out.println(doc.get());
            } catch (MalformedTreeException e) {
                e.printStackTrace();
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        }
    }

但我在 textEdit.apply(doc) 行收到空指针异常

4

1 回答 1

0

您的 CodeFormatter 为空

CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);

注意:我不能写评论

于 2013-09-04T07:10:02.620 回答