[英]How to get a JTextField to display text from a file
我正在为计算机类制作一个简单的“脚本解释器”,并且我想让它当前在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);
}
}
按钮管理器类(在其中初始化文件输入):
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();
}
}
这是错误:
InputReader.FileInput.ReadFile(FileInput.java:30)处FileReader.FileInput.process(FileInput.java:40)处的线程“ AWT-EventQueue-0” java.lang.NullPointerException处于Input.ButtonManager.actionPerformed(ButtonManager。)处javax.swing.AbstractButton.fireActionPerformed(未知源)处的java:84)javax.swing.DefaultButtonModel.fireActionPerformed(未知源)处的javax.swing.AbstractButton $ Handler.actionPerformed(未知源)处...
如果有人可以告诉我我在哪里搞砸了,或者有更好的方法做到这一点,那将很棒。 提前致谢。
编辑-我不是在问什么是空指针,而是在问我的handler.getInterp()方法为什么返回null。 我在哪里忘记初始化它。
java.lang.NullPointerException在
FileReader.FileInput.process(FileInput.java:40)在
您尚未在初始化FileReader.FileInput
类并调用process(String)
方法的地方提供代码,但您的handler
引用可能已初始化为null
值或handler.getInterp()
返回null
值
handler
.getInterp()
编辑:
第二次检查
更新ButtonManager
构造函数代码以检查是否具有非null处理程序
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空值,则对其进行更正。
也许这会工作
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." ) ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.