2016-11-18 105 views
-3

我的setter测试值是传递的参数。当我尝试在getter方法中获取它时,它将返回null。我无法弄清楚我的生活。这是我的完整代码。我认为这可能不属于方法问题。为什么我的getter在setter返回它应该返回的值时返回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()); 
+1

您可能是以错误的顺序或不同的实例调用它。 – SLaks

+0

yep - 如图所示的代码没有看到问题,所以问题出现在您未显示的代码中;-) –

+0

请显示[mcve]。 –

回答

2

我认为你是第一,然后二传手调用getter方法。

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

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; 
} 
3
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.getLoggedEmail()得到你实例化一个新的MainFrame多数民众赞成。

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