I have a problem with getting an input from class A to class B. I already created a method which gets the value but when I call it in class b it returns empty.
These are my codes:
CLASS A

public class log extends javax.swing.JFrame {

    public String name;
  

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
             System.out.println("1"+getuser());
        try {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/devweb", "root", "123456");
            PreparedStatement statement = con.prepareStatement("select User,pwd from account");
            ResultSet rs = statement.executeQuery();
             name = user.getText();
            String pwd = jTextField2.getText();
      
            while (rs.next()) {
                if (rs.getString("User").equals(name) && rs.getString("pwd").equals(pwd)) {
                    user.getText();
                    result now = new result();
                    getuser();
                    now.res();
                    now.jTextField1.setText(name);
                     
                     System.out.println("2"+getuser());
                     

                }


            }




        } catch (Exception e) {
            System.out.println(e.getMessage());
        }




    }                                        
 public   String getuser() {
       
      
        return this.user.getText();
        
    }

CLASS B(I'm gonna post the button straight away since that this is the only thing that this class contains)

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       
        log qwe=new log();
        qwe.getuser();
       
        jTextField1.setText(qwe.user.getText());
        System.out.println("3"+qwe.getuser());
      
        System.out.println("5"+qwe.user.getText());

Line 3 you create new log, then ask that to getUser, so of course its empty.
Instead of a new log class, B needs a reference to the real log instance that you created and interacted with. How that happens depends on the code where log and B are created, which I don't see here.

the user variable needs to be an instantiated variable under the class, and either needs to be given a value there or in a default constructor. Infact it shouldn't even compile because I don't see where user is ever instantiated (Including jTextField2 as well).

I'll assume all the variables are textfields.

public class log extends javax.swing.JFrame {

    public JTextField user = new JTextField("", 10);
    public JTextField jTextField2 = new JTextField("", 10);
    public String name;
  
    /* Default constructor */
    public log()
    {
       user.setText("Some stuff!");
       jTextField2.setText("Some other stuff!");
    }

Now at least the getuser method will return something...
In this case, it will return "Some stuff!"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.