I'm working on making a simple "Script Interpreter" for a computer class and I want to have it currently display the contents of a file in a JTextField but I keep getting a null pointer exception. Here is the code:
MainClass (it is run by a launcher class with the main method)
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;
}
}
My handler:
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;
}
}
My FileInput class(The class that reads and outputs the contents of the file):
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);
}
}
The Button Manager Class(where the file input is initialized):
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();
}
}
Here is the error:
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) ...
If someone could tell me where I messed up, or a better way to do this that would be great. Thanks in advance.
edit-I'm not asking what a null pointer is, I'm asking why my handler.getInterp() method is returning null. Where did I forget to initialize it.