2016-11-18 42 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多數民衆贊成。

您需要在您創建的同一個對象中設置該值。不是你新創建的那個。