0

Im trying to get jTextField value in another class but always get error null exception. Here is my code :

Class Main :

public class FormTambahDoc extends javax.swing.JFrame {
    Utility utility;

    public FormTambahDoc() {
        initComponents();
        utility = new Utility();
        setButton();
    }

    public  String gettextIdentitasPengguna() {
        return textIdentitasPengguna.getText();
    }

     private void setButton() {
        btnSimpan.addActionListener(new ActionListener() {@Override
        public void actionPerformed(ActionEvent e) { utility.cek();} });
    }

  }

Class another:

public class Utility {

FormTambahDoc formTambahDoc;
//FileJpaController controller;


public  void cek()
{

   String inputText = formTambahDoc.gettextIdentitasPengguna();
   System.out.println(inputText);
   //return `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException`


}

}

What's wrong in this code ?

Guillaume
  • 5,208
  • 10
  • 43
  • 77
navotera
  • 321
  • 1
  • 14
  • Have you intialized your variable `formTambahDoc`? Change the third line to `FormTambahDoc formTambahDoc = new FormTambahDoc();` – Sergiy Medvynskyy May 28 '15 at 07:33
  • im already try it but when im changing textIdentitasPengguna value on the GUI the output is still the default value so that not what i expected – navotera May 28 '15 at 07:39
  • Can you add some more code. From the code snippet it is not clear what is relation between 2 classes. – Anton Krosnev May 28 '15 at 07:54
  • this is the complete code hope u can help @AntonK. http://pastebin.com/UkvVSTpK – navotera May 28 '15 at 08:00
  • when the code is running the compiler become hang, the error is something like stackoverflow. sorry that what i got, i forget to copy that error here @SergiyMedvynskyy – navotera May 28 '15 at 08:17

2 Answers2

1

You should create instance of FormTambahDoc before you ca use it: FormTambahDoc formTambahDoc = new FormTambahDoc(); or get the instance from somewhere. Otherwise formTambahDoc will always be null. Check again your AWT tutorial.

Anton Krosnev
  • 3,499
  • 1
  • 18
  • 35
  • im already try it but when im changing _textIdentitasPengguna_ value on the GUI the output is still the default value – navotera May 28 '15 at 07:37
0

Try following

public class FormTambahDoc extends javax.swing.JFrame {
    Utility utility;

    public FormTambahDoc() {
        initComponents();
        utility = new Utility(this);
        setButton();
    }

    public  String gettextIdentitasPengguna() {
        return textIdentitasPengguna.getText();
    }

     private void setButton() {
        btnSimpan.addActionListener(new ActionListener() {@Override
        public void actionPerformed(ActionEvent e) { utility.cek();} });
    }

}

public class Utility {

    FormTambahDoc formTambahDoc;
    //FileJpaController controller;
    public Utility(FormTambahDoc aForm) {
        formTambahDoc = aForm;
    }

    public  void cek()
    {

       String inputText = formTambahDoc.gettextIdentitasPengguna();
       System.out.println(inputText);
       //return `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException`


    }

}
Sergiy Medvynskyy
  • 10,540
  • 1
  • 26
  • 43
  • Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - constructor Utility in class sharesystem.digital_file.controller.Utility cannot be applied to given types; required: sharesystem.digital_file.view.FormTambahDoc found: no arguments reason: actual and formal argument lists differ in length – navotera May 29 '15 at 01:19