我正致力于制作一个简单的脚本解释器"对于一个计算机类,我想让它当前在JTextField中显示文件的内容,但我一直得到一个空指针异常。 这是代码:
MainClass(它由带有main方法的启动器类运行)
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 Class(初始化文件输入的位置):
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();
}
}
这是错误:
线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException 在FileReader.FileInput.process(FileInput.java:40) 在FileReader.FileInput.ReadFile(FileInput.java:30) 在Input.ButtonManager.actionPerformed(ButtonManager.java:84) 在javax.swing.AbstractButton.fireActionPerformed(未知来源) at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source) 在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源) ...
如果有人能告诉我我搞砸了哪里,或者更好的方法来做到这一点会很棒。提前谢谢。
编辑 - 我不会问空指针是什么,我问为什么我的handler.getInterp()方法返回null。我在哪里忘了初始化它。
答案 0 :(得分:0)
的java.lang.NullPointerException 中的 FileReader.FileInput.process(FileInput.java:40)
您尚未提供已初始化FileReader.FileInput
类并调用process(String)
方法的代码,但您的handler
引用可能已初始化为null
值
或handler.getInterp()
返回null
值
handler
.getInterp()
修改强>
第二次检查
更新您的ButtonManager
构造函数代码以检查您是否具有非空处理程序
public ButtonManager(Interpreter interpreter)
{
this.interpreter = interpreter;
this.display = interpreter.getDisplay();
// DEBUG CODE START
System.out.println("#### Handler : " + interpreter.getHandler());
System.out.println("#### Interp : " + interpreter.getHandler().getInterp());
// DEBUG CODE END
this.fileInput = new FileInput(interpreter.getHandler());
}
如果是sysout null值,则更正它。
答案 1 :(得分:0)
也许这会起作用
try
{
FileReader file = new FileReader( ".txt" ) ;
BufferedReader buffer = new BufferedReader( file );
String line = "" ;
while( ( line = buffer.readLine() ) != null )
{
txt.setText( line ) ;
}
buffer.close() ;
}
catch( IOException e )
{
System.out.println( "A read error has occurred." ) ;
}