0

OK so I have this phonebook I need to do, and its main interface is a JFrame. I put a JTable in it, with firstName, lastName, ID, phone and mobile phone.

The idea is that I want, for example, to add a new Subscriber to the table, and I have an ADD button for this. So when you press the ADD button, it creates an instance of another JFrame, with the corresponding buttons and textfields that allow you to enter the Subscriber's fields (name etc).

The question is, after the Subscriber is created, how do I pass this Subscriber back to the "main JFrame", the one that contained the ADD button? I always get a NullPointerException, even as I've established a getter for the Subscriber.

I even declared a subscriber as static, and then initialized it in the method corresponding to the "OK" button in the ADD JFrame.

It seems to me that the Subscriber lives only in the scope of that method (even though I've declared it as static in the class, and haven't initialized it yet).

Here's some of the code:

public class AdaugaGUI extends javax.swing.JFrame {

    private static Abonat abonat;

        public static Abonat getAbonat() {
            return abonat;
        }

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
    NrTel nrTel = new NrTel(telefonFixFormattedTextField.getText(), telefonMobilFormattedTextField.getText());
    abonat = new Abonat(numeTextField.getText(), prenumeTextField.getText(), cnpFormattedTextField.getText(), nrTel);
    dispose();
}   

So as you can see, I declare "Abonat" at the beginning of the class, and then I initialize its values with those that the user inputs in the textFields, inside the method "okButtonActionPerformed".

Then, in the main JFrame (that created this secondary JFrame that allows you to create a new Subscriber), this is how the method looks (for the ADD button):

    private void btnAdaugaActionPerformed(java.awt.event.ActionEvent evt) {                                          
    AdaugaGUI adaugaGUI = new AdaugaGUI();
    adaugaGUI.setVisible(true);
    Abonat abonatAdaugat = adaugaGUI.getAbonat();
    System.out.println(abonatAdaugat.toString());
}        

I create the adaugaGUI JFrame (the one that allows you to add a Subscriber); then I create a Subscriber and try to use the get method to get the Subscriber that was added by the user.

Am I doing something logically wrong? Is this method being executed "completely" and hence a NullPointerException is thrown, or is it temporarily suspended while the user adds the info in the other JFrame?

This is very confusing.

Robert Ruxandrescu
  • 627
  • 2
  • 10
  • 22
  • 1
    1. See: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636). 2. Use a modal dialog, not a 2nd JFrame. 3. If you're seeing a NullPointerException, then debug it as per is well described in the answers to the canonical NullPointerException question: [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Hovercraft Full Of Eels Dec 08 '16 at 14:55
  • 1
    `"I even declared a subscriber as static"` -- which is a **very** bad thing to do as an attempted solution to this problem. This is not what static is used for. – Hovercraft Full Of Eels Dec 08 '16 at 14:56
  • I know I know, just wanted to see what would happen. – Robert Ruxandrescu Dec 08 '16 at 15:31

0 Answers0