你好我是一名初级java开发人员并且正在尝试学习SWT并制作一个程序但是我无法通过一个程序。程序打开一个文件,读取它并应该在FromData中显示内容但是我得到了一个例外,如果我尝试在控制台中打印它,它工作正常。 任何人都可以帮助我吗?
代码:
public abstract class CommonUtils {
protected Text text1, text2;
protected Font font;
protected StringBuffer sb;
protected Boolean opened;
protected Display display;
public String mptsvnFileDialog(Shell shell, String type) {
FileDialog dialog;
if (type == "OPEN") {
dialog = new FileDialog(shell, SWT.OPEN);
} else {
dialog = new FileDialog(shell, SWT.SAVE);
}
String[] filterNames = new String[] { "MPT_PRODUCT File",
"All Files (*)" };
String[] filterExtensions = new String[] { "*.mpt_product", "*" };
dialog.setFilterNames(filterNames);
dialog.setFilterExtensions(filterExtensions);
String patch = dialog.open();
if (patch != null) {
return patch;
} else {
return "";
}
}
public void mptOpenFile(Shell shell, String fileOpen) {
FileReader openFile = null;
try {
openFile = new FileReader(fileOpen);
} catch (FileNotFoundException e) {
MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR
| SWT.OK);
messageBox.setMessage("Could not open file!");
messageBox.setText("Error");
messageBox.open();
return;
}
BufferedReader fileInput = new BufferedReader(openFile);
String text = null;
sb = new StringBuffer();
int count = 0;
try {
do {
if (text != null)
sb.append(new String(text.getBytes(), "UTF-8"));
if (count != 0)
sb.append('\n');
count++;
} while ((text = fileInput.readLine()) != null);
fileInput.close();
} catch (IOException e1) {
MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR
| SWT.OK);
messageBox.setMessage("Could not write to file!");
messageBox.setText("Error");
messageBox.open();
return;
}
text1.setText(sb.toString());
// opened = true;
// System.out.println(sb);
}
public class MptSvnText extends CommonUtils {
public MptSvnText() {
}
public void mptSvnText(Display display, Shell shell) {
text1 = new Text(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL
| SWT.BORDER);
FormData data1 = new FormData();
data1.top = new FormAttachment(0, 25);
data1.left = new FormAttachment(0, 5);
data1.right = new FormAttachment(50, -5);
data1.bottom = new FormAttachment(100, -50);
text1.setLayoutData(data1);
text2 = new Text(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL
| SWT.BORDER);
FormData data2 = new FormData();
data2.top = new FormAttachment(0, 25);
data2.left = new FormAttachment(50, 5);
data2.right = new FormAttachment(100, -5);
data2.bottom = new FormAttachment(100, -50);
text2.setLayoutData(data2);
text1.addListener(SWT.Activate, event -> {
Point temp = text2.getSelection();
text2.setSelection(temp.x);
});
text2.addListener(SWT.Activate, event -> {
Point temp = text1.getSelection();
text1.setSelection(temp.x);
});
}
public class MptSvnShell {
public MptSvnShell() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FormLayout());
shell.setText("MPT_SVN_Compatibility_Converter");
shell.setSize(820, 600);
MptSvnText text = new MptSvnText();
text.mptSvnText(display, shell);
MptSvnButtons buttons = new MptSvnButtons(shell);
MptSvnToolbar toolbar = new MptSvnToolbar(shell);
MptSvnMenu menu = new MptSvnMenu(display, shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
我得到的例外:
Exception in thread "main" java.lang.NullPointerException
at com.mptsvn.abstractclasses.CommonUtils.mptOpenFile(CommonUtils.java:92)
at com.mptsvn.menu.MptSvnMenu.lambda$0(MptSvnMenu.java:50)
at com.mptsvn.menu.MptSvnMenu$$Lambda$8/1232367853.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4353)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4172)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3761)
at com.mptsvn.menu.MptSvnMenu.<init>(MptSvnMenu.java:121)
at com.mptsvn.shell.MptSvnShell.<init>(MptSvnShell.java:27)
at com.mptsvn.test.Test.main(Test.java:9)