0

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.

5
  • 1
    Possible duplicate of What is a Null Pointer Exception, and how do I fix it?
    – dsh
    Mar 9, 2016 at 15:06
  • It's not the same as that. I'm trying to figure out why I'm getting a null pointer. Where did I mess up? I know what a null pointer is, I just don't know why this null pointer is occuring. Mar 9, 2016 at 15:12
  • It seems your field handler is null, or perhaps the return value of one of your chained method calls. That is where the exception's stack trace points you, right?
    – dsh
    Mar 9, 2016 at 15:22
  • You were correct. I was creating the file input object with a handler that was created before the interpreter was full created. I managed to fix it. Mar 9, 2016 at 15:34
  • You can step through your program, in a debugger or in your head, and follow the values as the statements are executed. When are objects created? When are variables assigned to? One useful technique to have your code help show you when and where you have a problem is to check arguments for validity. For example: public FileInput(Handler handler) { if (handler == null) { throw new IllegalArgumentException("handler must not be null"); } this.handler = handler; } This will show you when and where you pass null and will stop your program when it is invalid.
    – dsh
    Mar 9, 2016 at 16:17

2 Answers 2

0

java.lang.NullPointerException at

FileReader.FileInput.process(FileInput.java:40) at

You haven't provided the code where you have initialized the FileReader.FileInput class and invoked the process(String) method but your handler reference probably initialized with null value or handler.getInterp() is returning the null value

handler
.getInterp()

EDIT:

Second check

Update your ButtonManager constructor code to check whether you have non-null handler or not

 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());
 }

If it is sysout null value then correct it.

2
  • My bad, ill fix it in the question Mar 9, 2016 at 15:25
  • Try second check in answer.
    – Mahendra
    Mar 9, 2016 at 15:45
0

Maybe this will work

    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." ) ;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.