13 Replies Latest reply: Nov 3, 2006 4:58 AM by 800322 RSS

    runtime error calling method that returns a string

    807607
      Hi, I have to enter some text using the keyboard and display it as a file name.
      So I have a TextEnter class for entering the text
      public class TextEnter extends JPanel implements ActionListener {
      private JButton clickButton;
      private JTextField textField;
      private JLabel textLabel;
      public String text;
      ......
      In this class I have a public variable = text

      I have another class which creates the file and I need this class to receive the text variable, so in the TextEnter class I wrote a method
      that returns the text:
      public String rettext(){
      return text;
      };

      so in the file that creates the file I call this method
      TextEnter tx;
      String code;
      code=tx.rettext();

      This compiles but I get a runtime error in the line code= tx.rettext();
      I can't see what's wrong..
        • 1. Re: runtime error calling method that returns a string
          masijade
          If the above is truely your code, then you haven't initialised your tx object. What was the error? And, if the above is truely your code, I seriously doubt that it compiled since it should have through a "variable may not have been initialised" error.
          • 2. Re: runtime error calling method that returns a string
            800322
            1) How about posting that error, which is actually a NullPointerException
            2) How about creating an instance of your class, before using it?
            • 3. Re: runtime error calling method that returns a string
              807607
              tx class is like this one, the only difference is that there's a getText() method so in variable text I get the text the user enters and it works fine:

              import javax.swing.*;
              public class JFrameTester {
              public static void main(String[] args) {
              JFrame myFrame = new JFrame("My JFrame");
              myFrame.setSize(250, 250);
              myFrame.setLocation(300,200);
              myFrame.getContentPane().add(BorderLayout.CENTER, new JTextArea(10, 40));
              myFrame.show();

              }

              }

              I don't initialise the tx object, it compiles, I debugged it and I get the text correctly in var text, the problem is that when I call the method that returns that text in the class that creates the file I get a runtime error
              • 4. Re: runtime error calling method that returns a string
                800322
                I get a runtime error
                It's hopeless.
                • 5. Re: runtime error calling method that returns a string
                  807607
                  1)You are right it's a NullPointerException:
                  Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
                  2) I think that I'm creating an instance in this line in the main of the class that creates the file:
                  frame.getContentPane().add(new TextEnter());
                  • 6. Re: runtime error calling method that returns a string
                    800322
                    2) I think that I'm creating an instance in this line
                    "You think"? Why don't you know what your code does? :)
                    in the main of the class that creates the file:
                    frame.getContentPane().add(new TextEnter());
                    I don't see you assigning the attribute tx to that new instance.
                    • 7. Re: runtime error calling method that returns a string
                      807607
                      now I'm certainly sure that it's initialised:
                      private TextEnter tx= new TextEnter();
                      and I don't get the runtime error!!

                      But I'm getting a null value in the codi variable
                      String codi;
                      codi=tx.rettext();

                      that's because the rettext() method is not working:
                      public String rettext(){
                      String in;
                      in=text;
                      System.out.println("text ???????");
                      System.out.println(in);
                      return in;
                      };
                      in is null, why??? the text variable is not null, it's a global variable and there's the text entered by the keyboard
                      • 8. Re: runtime error calling method that returns a string
                        masijade
                        public String rettext(){
                        String in;
                        in=text;
                        System.out.println("text ???????");
                        System.out.println(in);
                        return in;
                        };
                        in is null, why??? the text variable is not null,
                        it's a global variable and there's the text entered
                        by the keyboard
                        What do you mean by Global variable? There is no such thing in Java. There might be an instance or class variable of that name, but we don't know that since you have not included anything about. And even if there is an instance or class variable of that name, that is no guarantee that it has value. You are not testing that in this method. It might help if you posted a bit more code (and this time use code tags).
                        • 9. Re: runtime error calling method that returns a string
                          807607
                          Here is the class for entering text, in the text variable there's the text entered by the keyboard, that works fine, but the rettext() method returns null

                          import javax.swing.*;
                          import java.awt.*;
                          import java.awt.event.*;

                          public class TextEnter extends JPanel implements ActionListener {

                          private JButton clickButton;
                          private JTextField textField;
                          private JLabel textLabel;
                          public String text;

                          public TextExample1() {
                          super(new BorderLayout()); // calling the constructor JPanel(LayoutManager)
                          textField = new JTextField(40); // calling the constructor JTextField(int) : nr of columns
                          add(textField, BorderLayout.NORTH);

                          clickButton = new JButton("Click to save");
                          clickButton.addActionListener(this);

                          add(clickButton, BorderLayout.CENTER);

                          textLabel = new JLabel("Not saved yet...");
                          add(textLabel, BorderLayout.SOUTH);
                          }

                          public void actionPerformed(ActionEvent e) {
                          if (e.getSource() == clickButton) {
                          texto = textField.getText();
                          textLabel.setText(texto);
                          System.out.println("THE TEXT");
                          System.out.println(text);     

                          }
                          else {
                          System.err.println("Error: since this class only listens to events from clickButton,");
                          System.err.println("it is impossible to get here!!");
                          }
                               
                          }

                          public String rettext(){
                          String in;
                          in=text;
                          System.out.println("text???????");
                          System.out.println(in);
                          return in;
                          };


                          public static void main(String[] arg) {
                          }
                          }
                          • 10. Re: runtime error calling method that returns a string
                            masijade
                            Here is the class for entering text, in the text
                            variable there's the text entered by the keyboard,
                            that works fine, but the rettext() method returns
                            null

                            import javax.swing.*;
                            import java.awt.*;
                            import java.awt.event.*;

                            public class TextEnter extends JPanel implements
                            ActionListener {

                            private JButton clickButton;
                            private JTextField textField;
                            private JLabel textLabel;
                            public String text;

                            public TextExample1() {
                            super(new BorderLayout()); // calling the
                            constructor JPanel(LayoutManager)
                            textField = new JTextField(40); // calling the
                            constructor JTextField(int) : nr of columns
                            add(textField, BorderLayout.NORTH);
                            clickButton = new JButton("Click to save");
                            clickButton.addActionListener(this);

                            add(clickButton, BorderLayout.CENTER);

                            textLabel = new JLabel("Not saved yet...");
                            add(textLabel, BorderLayout.SOUTH);
                            }
                            public void actionPerformed(ActionEvent e) {
                            if (e.getSource() == clickButton) {
                            texto = textField.getText();
                            textLabel.setText(texto);
                            System.out.println("THE TEXT");
                            System.out.println(text);     

                            }
                            else {
                            System.err.println("Error: since this class
                            only listens to events from clickButton,");
                            System.err.println("it is impossible to get
                            here!!");
                            }

                            }

                            public String rettext(){
                            String in;
                            in=text;
                            System.out.println("text???????");
                            System.out.println(in);
                            return in;
                            };


                            public static void main(String[] arg) {
                            }
                            As you can see above, text is not being defined. texto, wherever this comes, is being defined, but this is something different. If this is your code, it should not even compile even if only because of the texto variable.
                            • 11. Re: runtime error calling method that returns a string
                              807607
                              The program is not originally in English so I have to translate some parts to post it here and I didin't translate that , it was a mistake copying the code here in the forum, now this is the correct version with the texto variable changed for the text varible, in the original code now I have the text varible too and I'm still getting the in variable with null value...


                              import javax.swing.*;
                              import java.awt.*;
                              import java.awt.event.*;

                              public class TextEnter extends JPanel implements ActionListener {

                              private JButton clickButton;
                              private JTextField textField;
                              private JLabel textLabel;
                              private String text;

                              public TextEnter() {
                              super(new BorderLayout()); // calling the constructor JPanel(LayoutManager)
                              textField = new JTextField(40); // calling the constructor JTextField(int) : nr of columns
                              add(textField, BorderLayout.NORTH);

                              clickButton = new JButton("Click to save");
                              clickButton.addActionListener(this);

                              add(clickButton, BorderLayout.CENTER);

                              textLabel = new JLabel("not saved yet...");
                              add(textLabel, BorderLayout.SOUTH);
                              }

                              public void actionPerformed(ActionEvent e) {
                              if (e.getSource() == clickButton) {
                              text = textField.getText();
                              textLabel.setText(text);
                                        System.out.println("THE TEXT");
                                             System.out.println(text);
                                        
                              }
                              else {
                              System.err.println("Error: since this class only listens to events from clickButton,");
                              System.err.println("it is impossible to get here!!");
                              }
                                   
                              }

                              public String rettext(){
                              String in;
                              in=text;
                              System.out.println("what's here?");
                              System.out.println(in);
                              return in;
                              };




                              public static void main(String[] arg) {
                              }
                              }
                              • 12. Re: runtime error calling method that returns a string
                                masijade
                                Then what about your other class. I can now only assume that one of two things is happening.

                                1) rettext is being called before actionPerformed has been triggered
                                2) actionPerformed is being triggered on one instance, but rettext is being called on a different instance.

                                Post your other class.
                                • 13. Re: runtime error calling method that returns a string
                                  807607
                                  In the other class I create the file and write some values in the file (that works fine) the problem is that the name of the file should be read from the keyboard in variable filename, somewhere in the way I'm loosing variable text from the other class, maybe when I create the tx instance


                                  import javax.swing.*;
                                  import java.awt.*;
                                  import java.awt.event.*;


                                  public class HelloKeyboard implements MouseListener
                                  {
                                  ...............

                                  private TextEnter tx= new TextEnter();


                                  String filename;
                                  filename=tx.rettext();


                                  try { BufferedWriter out = new BufferedWriter(new FileWriter("hello.txt"));
                                       out.write("HELLLLLLLLLLLLOOOOOO" );
                                       out.close(); } catch (IOException e) { }

                                  public static void main(String args[])
                                  {
                                  JFrame frame = new JFrame("Text Ejamp 1");
                                  frame.getContentPane().add(new TextEnter());
                                  frame.setSize(new Dimension(200, 200)); // method JFrame.setSize(java.awt.Dimension)
                                       frame.setLocationRelativeTo(null);
                                       frame.setLocation(900,200);
                                       
                                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                  frame.show();
                                            
                                                           
                                  }
                                  }