Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - Separate Listener Class throws NullPointerException
    primarykey
    data
    text
    <p>Apologize for the long post, but I wanted to be as detailed as possible so there's a better chance people understand what I'm trying to convey:</p> <p>OK, so an overview of this problem is that I'm trying to make a program that simulates a cash register. (This is a "for-fun" project). The way I set it up is:</p> <p>CashRegister: main class that starts the program, serves as the main window for everything.</p> <pre><code>public class CashRegister extends JFrame { ... } </code></pre> <p>Other classes: serve as JPanels that provide the different components of the main CashRegister window. Example:</p> <pre><code>public class NorthPanel extends JPanel { ... } </code></pre> <p>Then in the CashRegister class:</p> <pre><code>add(new NorthPanel(), BorderLayout.NORTH); </code></pre> <p>etc. Basically, I have a JTextField in the NorthPanel class called priceField that holds the value of the price the user enters. I have a separate class (Keypad) that also extends JPanel and serves as the number keypad in the center of the main window. In CashRegister:</p> <pre><code>add(new NorthPanel(), BorderLayout.NORTH); add(new Keypad()); // (default for BorderLayout is CENTER) </code></pre> <p>One of the problems I have run into is that I created one class, EventHandler, to serve as the listener class for the entire program because there are components in each JPanel class (NorthPanel and Keypad, for instance) that need to communicate with each other; the user presses a keypad button, the price field in the NorthPanel needs to know what key in the keypad was pressed.</p> <p>I don't know if the problem comes from the fact that these components are coming from different classes and are therefore referenced differently, or what. All the classes are in the same directory, I'm using NetBeans and it's all part of the same package.</p> <p>In my EventHandler class, I created several different constructors so as to be able to pass in all the components that need to communicate with each other from different classes. For example:</p> <pre><code>public class EventHandler implements ActionListener { private JTextField priceField; private JButton[][] keypad; public EventHandler(JTextField priceField) { this.priceField = priceField; } public EventHandler(JButton[][] keypad) { this.keypad = keypad; } } </code></pre> <p>In my NorthPanel class, I instantiate priceField first, configure it (set the font, etc.) and say:</p> <pre><code>EventHandler e = new EventHandler(priceField); </code></pre> <p>in my attempt to pass the priceField through to my listener class.</p> <p>Then in the Keypad class, I say:</p> <pre><code>EventHandler e = new EventHandler(keypad); for(int i = 0; i &lt; 4; i++) { for(int j = 0; j &lt; 3; j++) { keypad[i][j].addActionListener(e); } } </code></pre> <p>Then in the EventHandler class, having passed through those variables:</p> <pre><code>public void actionPerformed(ActionEvent e) { for(int i = 0; i &lt; 4; i++) { for(int j = 0; j &lt; 3; j++) { if(keypad[i][j] == e.getSource()) { // call a custom method to append the numbers to the text field } } } } </code></pre> <p>It is at this point that I get a NullPointerException. I don't know why, but my guess is that it's because the listener objects are coming from different classes and using different constructors because I have to pass in different objects from each class that need to communicate with each other. I'm not 100% sure though. Is there a way to get around this or am I doing something completely wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload