我正在开发一个向导页面。在该页面中,我将文件转换为String并在文本框中显示
但我想以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)行
获取空指针异常答案 0 :(得分:0)
您的CodeFormatter为空
CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);
注意:我不能写评论