Home > Enterprise >  Retrieving Values from Getters and Setters
Retrieving Values from Getters and Setters

Time:04-07

I'm having an issue retrieving a values from my getters and setters. I'm setting the value in one class and then trying to retrieve it in another so that it can be used in a label. I haven't done any programming in Java in a while so I feel that there is something fundamental that I am not aware of.

I've written this short code to display the problem I'm having in my main project. Any help will be appreciated, I hope you have a good day.

Form1 Class:

public class Form1 extends JFrame implements ActionListener {

    JButton btn1;
    JTextField tf1;
    
    public Form1() {
        
        btn1 = new JButton("submit");
        btn1.setBounds(75, 90, 100, 40);
        btn1.setFocusable(false);
        btn1.addActionListener(this);
        
        tf1 = new JTextField();
        tf1.setBounds(20, 30, 200, 40);
        btn1.addActionListener(this);
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(250, 180);
        this.setLayout(null);
        this.setResizable(false);
        this.setVisible(true);
        this.add(btn1);
        this.add(tf1);
    }
    public void actionPerformed(ActionEvent e) {
        
        String b = "";
        String v = "";
        String m = "";
        String r = "";
        
        Cars car = new Cars(b, v, m, r);
        
        if(e.getSource()==btn1) {
            
            b = tf1.getText();
            car.SetBmw(b);
            
            new Form2();
            System.out.println(car.GetBmw());
            // Side note, for some reason this will be printed twice in the console.
        }
    }
}

Form2 Class:

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Form2 extends JFrame {

    Cars car = new Cars();
    
    public Form2() {
    
        JLabel label = new JLabel();
        label.setText(car.GetBmw());
        label.setBounds(10, 10, 50, 50);
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(250, 180);
        this.setLayout(null);
        this.setResizable(true);
        this.setVisible(true);
        this.add(label);
    }
}

Cars Class:


public class Cars {

    private String bmw;
    private String vw;
    private String mazda;
    private String renault;
    
        public Cars() {}
        public Cars(String bmw, String vw, String mazda, String renault) {
            
            this.bmw = bmw;
            this.vw = vw;
            this.mazda = mazda;
            this.renault = renault;
        }
        
        // Getters and Setters for BMW
        public String GetBmw() {
            return bmw;
        }
        public void SetBmw(String bmw) {
            this.bmw = bmw;
        }
        
        // Getters and Setters for VW
        public String GetVw() {
            return vw;
        }
        public void SetVw(String vw) {
            this.vw = vw;
        }
        
        // Getters and Setters for Mazda
        public String GetMazda() {
            return mazda;
        }
        public void SetMazda(String mazda) {
            this.mazda = mazda;
        }
        
        // Getters and Setters for Renault
        public String GetRenault() {
            return renault;
        }
        public void SetRenault(String renault) {
            this.renault = renault;
        }
}

CodePudding user response:

You are creating a new instance of Car in Form2 which is empty, and you are using getters on this empty instance. To retrieve data on instance from Form1, you have to pass it to Form2, through constructor for example:

    public void actionPerformed(ActionEvent e) {
        
        String b = "";
        String v = "";
        String m = "";
        String r = "";
        
        Cars car = new Cars(b, v, m, r);
        
        if(e.getSource()==btn1) {
            
            b = tf1.getText();
            car.SetBmw(b);
            
            new Form2(car);
            System.out.println(car.GetBmw());
            // Side note, for some reason this will be printed twice in the console.
        }
    }

public class Form2 extends JFrame {

    
    public Form2(Car car) {
    
        JLabel label = new JLabel();
        label.setText(car.GetBmw());
        label.setBounds(10, 10, 50, 50);
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(250, 180);
        this.setLayout(null);
        this.setResizable(true);
        this.setVisible(true);
        this.add(label);
    }
}

CodePudding user response:

In Form2 you are creating a new Cars instance:

    Cars car = new Cars();

This is a completely different instance to the one created in Form1.actionPerformed, so it will have completely unrelated values. Since you're using the parameterless constructor here, all the fields will be at their default value of null.

It's difficult to tell what your intention is, because this is a meaningless toy example, but my best guess is that you want to pass the created Cars instance into the constructor of Form2.

In Form1.actionPerformed(), pass the created instance (called car) to the constructor:

            new Form2(car);

In Form2, receive the value and assign it to the field:

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Form2 extends JFrame {

    Cars car;
    
    public Form2(Cars car) {
        this.car = car;
    
        ...
    }
}
  • Related