我正在爲計算機類製作一個簡單的「腳本解釋器」,並且我希望當前在JTextField中顯示文件的內容,但我一直在空指針異常。 下面是代碼:如何獲取JTextField以顯示文件中的文本
MainClass(它是由發射器類與主方法運行)
package ClydeInterpreter;
import Input.ButtonManager;
import gfx.Display;
public class Interpreter extends Thread
{
private Handler handler;
private Display display;
private ButtonManager buttonManager;
public Interpreter()
{
System.out.println("Interpreter is created");
buttonManager = new ButtonManager(this);
handler = new Handler(this, buttonManager);
display = new Display(handler);
buttonManager.Update(this);
handler.Update(this);
}
public Handler getHandler()
{
return handler;
}
public void setHandler(Handler handler)
{
this.handler = handler;
}
public Display getDisplay()
{
return display;
}
public void setDisplay(Display display)
{
this.display = display;
}
}
我的處理程序:
package ClydeInterpreter;
import FileReader.FileInput;
import Input.ButtonManager;
public class Handler
{
private Interpreter interp;
private ButtonManager buttonManager;
public Handler(Interpreter interp, ButtonManager buttonManager)
{
this.interp = interp;
this.buttonManager = buttonManager;
}
public Interpreter getInterp()
{
return interp;
}
public void setInterp(Interpreter interp)
{
this.interp = interp;
}
public ButtonManager getButtonManager()
{
return buttonManager;
}
public void setButtonManager(ButtonManager buttonManager)
{
this.buttonManager = buttonManager;
}
public void Update(Interpreter interpreter)
{
this.interp = interpreter;
}
}
我的FileInput類(讀取並輸出文件內容的類):
package FileReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import ClydeInterpreter.Handler;
import ClydeInterpreter.Utils.Utils;
public class FileInput
{
FileInputStream fileReader;
Handler handler;
public FileInput(Handler handler)
{
this.handler = handler;
}
public void ReadFile(File file)
{
try {
BufferedReader in = new BufferedReader(new FileReader(file));
StringBuffer stringBuffer = new StringBuffer();
String str, line;
while ((str = in.readLine()) != null)
//stringBuffer.append("\n");
process(str);
in.close();
} catch (IOException e) {
}
}
private void process(String str)
{
System.out.println(str);
handler
.getInterp()
.getDisplay()
.getConsole()
.setText(str);
}
}
的Button Manager類(如文件輸入初始化):
package Input;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.sound.sampled.*;
import java.io.*;
import java.net.*;
import javax.sound.sampled.AudioSystem;
import javax.swing.JOptionPane;
import ClydeInterpreter.Handler;
import ClydeInterpreter.Interpreter;
import ClydeInterpreter.Utils.Utils;
import FileReader.FileInput;
import gfx.Display;
public class ButtonManager implements ActionListener
{
Handler handler;
static Interpreter interpreter;
static Display display;
static String loc;
static FileInput fileInput;
public ButtonManager(Interpreter interpreter)
{
this.interpreter = interpreter;
this.display = interpreter.getDisplay();
this.fileInput = new FileInput(interpreter.getHandler());
}
@Override
public void actionPerformed(ActionEvent ae)
{
String dest;
if (ae.getActionCommand().matches("open"))
{
FileDialog fd = new FileDialog(display.getF(), "Select Script", FileDialog.LOAD);
fd.setSize(300, 300);
fd.setVisible(true);
String s1 = ".clyde";
/*
fd.setFilenameFilter(new FilenameFilter()
{
@Override
public boolean accept(File dir, String name)
{
return name.endsWith(".txt");
}
});
*/
String sng = fd.getFile();
dest = fd.getDirectory() + fd.getFile();
if (sng.toLowerCase().endsWith(s1))
{
display.getTf().setText(sng);
display.setFile(new File(dest));
//Utils.loadFileAsString(loc);
}
else
{
JOptionPane.showMessageDialog(display.getF(), "Select a valid file format");
}
try
{
display.setFile(new File(dest));
} catch (Exception e)
{
e.printStackTrace();
}
}
else if(ae.getActionCommand().equals("debug"))
{
System.out.println("It works");
}
else if (ae.getActionCommand().equals("run"))
{
fileInput.ReadFile(display.getFile());
}
}
public void Update(Interpreter interpreter)
{
display = interpreter.getDisplay();
}
}
以下是錯誤:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at FileReader.FileInput.process(FileInput.java:40) at FileReader.FileInput.ReadFile(FileInput.java:30) at Input.ButtonManager.actionPerformed(ButtonManager.java:84) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) ...
如果有人能告訴我在哪裏,我搞砸了,或者更好的方式來做到這一點,這將是偉大的。提前致謝。
編輯 -I'm不問什麼是空指針,我問爲什麼我的handler.getInterp()方法返回null。我忘了初始化它在哪裏。
的可能的複製[什麼是空指針異常,以及如何解決?(http://stackoverflow.com/questions/218384/what-is- a-null-pointer-exception-and-how-do-i-fix-it) – dsh
這不是一回事。我試圖找出_why_我得到一個空指針。我在哪裏搞砸了?我知道什麼是空指針,我只是不知道爲什麼這個空指針發生。 –
看來你的字段'handler'是'null',或者你的鏈式方法調用的返回值。那是異常的堆棧跟蹤指向你的地方,對吧? – dsh