-1

I'm having trouble with calling a get method from one class and implementing it in a seperate actionlistner class. Here is the code for my get method which gets the text from a JTextField.

public String getTitleTextField() {
    return this.TitleTextField.getText();
}

The button that calls the listner class is below:

this.AddButton = new JButton("Add");
AddButton.setBounds(20, 161, 89, 23);
AddButton.addActionListener(new Listener());
add(AddButton);

Here is the code for my listner class: `

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Listener implements ActionListener{

    FilmPanel view;
    SaveData save;
    Object[] row = new Object[1];

    public void actionPerformed(ActionEvent arg0) {
        row[0] = view.getTitleTextField();
        view.model.addRow(row);
        try {
            save.saveTable();
        } catch (Exception e) {
            e.printStackTrace();
        }
    };
}

The error comes up as

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at assignment.listener.actionPerformed(listener.java:13)

which points to this row[0] = view.getTitleTextField();

Thanks for any help :)

EDIT:

Trying to figure out what's wrong its not a NULL exception

PrimosK
  • 13,848
  • 10
  • 60
  • 78
  • 3
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – OH GOD SPIDERS Oct 13 '17 at 08:34
  • How is that a duplicate? – user123123123 Oct 13 '17 at 08:36
  • 1
    When is`view` set? `FilmPanel view;` means `view` will get the value `null`, which explains the `NullPointerException` – Stefan Oct 13 '17 at 08:37
  • 2
    @user123123123 how is that not a duplicate? – Stultuske Oct 13 '17 at 08:38
  • @Stefan FIlmPanel is the Class name for where the get methods are and the GUI is – user123123123 Oct 13 '17 at 08:38
  • `FilmPanel` is the class. You have to make sure your `view` object is set so it "points" to the object that is your current instance of `FilmPanel`. Maybe a bit confusing since you're only working with one `FilmPanel` object, but image you had 10 windows/views next to each other. How would the `view` in the listener know which of the 10 objects to listen to? – Stefan Oct 13 '17 at 08:43
  • @Stefan how would I overcome this? – user123123123 Oct 13 '17 at 08:44
  • Pretty simple actually. Just check whether `view.getTitleTextField();´ is already initialized or not. – Alexander Heim Oct 13 '17 at 08:44

1 Answers1

1

Add a reference to this when you create the Listener:

new Listener(this); // instead of new Listener() in your code

Add a constructor in the Listener:

public Listener(FilmPanel view) {
    super();
    this.view = view;
}
Stefan
  • 2,395
  • 4
  • 15
  • 32
  • I'm happy to help :) – Stefan Oct 13 '17 at 08:52
  • 1
    Just a note referring to the original question. There is also a field `SaveData save` which isn't initialised just like `FilmView view` wasn't. However, your exception handling will hide this error. But perhaps your aware of this already and the reason the exception handling is there is because it still needs to be implemented. – d.j.brown Oct 13 '17 at 09:24