当setter返回它应该的内容时,为什么我的getter返回null

时间:2016-11-18 03:37:46

标签: java getter-setter

我的二传手测试值是传递的争论。当我尝试在getter方法中获取它时,它返回null。我无法想象我的生活。

这是我的完整代码。我认为这可能超出了方法问题。

package com.graphics.tyler;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {

private JPanel contentPane;
private JPasswordField txtPassword;
private JTextField txtUsername;
//private String email;
private String test;

public String getLoggedEmail() {
    System.out.println("Get logged email is: " + test);
    return this.test;
}

public void setLoggedEmail(String passedName) {
    System.out.println("Passed argument is: " + passedName);
    this.test = passedName;
    System.out.println("Setter Username is: " + test);
}





/**
 * Create the frame.
 */
public MainFrame() {

    Driver drive = new Driver();

    setTitle("Graphics project");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(300, 30, 800, 600);
    contentPane = new JPanel();
    contentPane.setBackground(Color.LIGHT_GRAY);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblWelcomeToGraphics = new JLabel("Welcome to Graphics program");
    lblWelcomeToGraphics.setFont(new Font("Tahoma", Font.PLAIN, 27));
    lblWelcomeToGraphics.setBounds(208, 11, 360, 33);
    contentPane.add(lblWelcomeToGraphics);

    JButton btnExit = new JButton("Exit");
    btnExit.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            dispose();
        }
    });
    btnExit.setBounds(703, 11, 71, 23);
    contentPane.add(btnExit);

    JLabel lblUsername = new JLabel("Username:");
    lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblUsername.setBounds(60, 199, 97, 25);
    contentPane.add(lblUsername);

    JLabel lblPassword = new JLabel("Password:");
    lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 20));
    lblPassword.setBounds(66, 281, 91, 25);
    contentPane.add(lblPassword);

    txtPassword = new JPasswordField();
    txtPassword.setBounds(159, 284, 193, 20);
    contentPane.add(txtPassword);

    txtUsername = new JTextField();
    txtUsername.setBounds(159, 205, 193, 20);
    contentPane.add(txtUsername);
    txtUsername.setColumns(10);

    JLabel lblDontHaveAn = new JLabel("Dont have an account? Sign Up!");
    lblDontHaveAn.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            UserCreation userCreate = new UserCreation();
            dispose();
            userCreate.setVisible(true);
        }
    });
    lblDontHaveAn.setBounds(601, 536, 193, 14);
    contentPane.add(lblDontHaveAn);

    JButton btnSignIn = new JButton("Sign In!");
    btnSignIn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            String email = txtUsername.getText();
            String password = new String(txtPassword.getPassword());

            int password1 = password.hashCode();
            String password2 = Integer.toHexString(password1);

            if(drive.signIn(email, password2)) {

                System.out.println("Login Sucessful!");
                UserScreen nextScreen = null;
                setLoggedEmail(email);
                try {
                    nextScreen = new UserScreen();
                } catch (Exception e1) {

                    e1.printStackTrace();
                }
                nextScreen.setVisible(true);
                dispose();
            }else{
                JOptionPane.showMessageDialog(null, "Email or password is incorrect");
            }

        }
    });
    btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
    btnSignIn.setBounds(208, 342, 97, 33);
    contentPane.add(btnSignIn);
}
}

这是我在下面调用getter方法的类:

public UserScreen() throws Exception {

    Driver drive = new Driver();
    MainFrame mainFrame = new MainFrame();

    drive.getUserInfo(mainFrame.getLoggedEmail());
    System.out.println("User screen email is: " + mainFrame.getLoggedEmail());

2 个答案:

答案 0 :(得分:3)

 #categorylist{
      margin: auto;
      display: table;
  }
  #categorylist ul {
      text-align: left;
  }

您实例化了一个新的public UserScreen() throws Exception { Driver drive = new Driver(); MainFrame mainFrame = new MainFrame(); // You instantiate new MainFrame drive.getUserInfo(mainFrame.getLoggedEmail()); System.out.println("User screen email is: " + mainFrame.getLoggedEmail()); ,这就是MainFrame null 的原因。

您需要在您创建的同一对象中设置值。不是你新创造的那个。

答案 1 :(得分:2)

我认为你首先调用getter方法,然后调用setter。

见下面的代码示例。这将执行并给出正确的结果。

 String test;

 public static void main(String []args){
    HelloWorld h = new HelloWorld();
    System.out.println("Hello World");
    h.setLoggedEmail("HI");
    h.getLoggedEmail();
 }


public void setLoggedEmail(String passedName) {
    System.out.println("Passed argument is: " + passedName);
    this.test = passedName;
    System.out.println("Username is: " + test);
}


public String getLoggedEmail() {
    System.out.println("Get logged email is: " + test);
    return this.test;
}