2017-05-07 55 views
0

但在另一個類的設置方法,當我得到的值是存在,, 所以我有3個類的第一類是getter setter類,第二個是類填充集和最後一個類是獲得方法吸氣,,但它會空值...爲什麼當我在另一個班級打電話時,getter方法變爲空?

 public class loginAdminn { 

     String Username, Password; 

     public String getUsername() { 
      return Username; 
     } 

     public void setUsername(String Username) { 
      this.Username = Username; 
     } 

     public String getPassword() { 
      return Password; 
     } 

     public void setPassword(String Password) { 
      this.Password = Password; 
     } 

    } 
    // method to fill the set on another class 
    public void in(){ 
       loginAdminn p = new loginAdminn(); 
       String user = Username.getText(); 
       String pass = Password.getPassword().toString(); 
       p.setUsername(user); 
       p.setPassword(pass); 

       // new NewMain().run(); 

       tes.jalankan(); 
      } 
// class method getter (null) 

public void jalankan() { 

     loginAdminn br = new loginAdminn(); 

     String kueri = "Select Username, password from Admin"; 

     int x = 0; 

     try { 
      ps = conn.prepareStatement(kueri); 
      ResultSet rs = ps.executeQuery(); 

      while (rs.next()) { 

       if (br.getUsername().equalsIgnoreCase(rs.getString("Username"))) { 
        if (br.getPassword().equalsIgnoreCase(rs.getString("Password"))) { 
         JOptionPane.showMessageDialog(null, "Masuk Berhasil"); 
         x = 1; 
         break; 
        } 
       } 
      } 

      if (x == 1) { 
       HomeAdmin b = new HomeAdmin(); 
       b.setVisible(true); 

      } else { 
       JOptionPane.showMessageDialog(null, "Masuk Gagal"); 

       DaftarAplikasi da = new DaftarAplikasi(); 
       da.setVisible(true); 

      } 

      ps.close(); 
     } catch (SQLException ex) { 
      JOptionPane.showMessageDialog(null, ex.getMessage()); 
     } 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

//the getUsername and getPassword goes null 

回答

0

您getUserName()來了空,因爲你正在使用比較的對象還沒有分配給它的Username值。

public void jalankan() { 

     //creating an object here , br has no username assigned 
     loginAdminn br = new loginAdminn(); 

     String kueri = "Select Username, password from Admin"; 

     int x = 0; 

     try { 
      ps = conn.prepareStatement(kueri); 
      ResultSet rs = ps.executeQuery(); 

      while (rs.next()) { 

       if (br.getUsername().equalsIgnoreCase(rs.getString("Username"))) { 
        if (br.getPassword().equalsIgnoreCase(rs.getString("Password"))) { 
         JOptionPane.showMessageDialog(null, "Masuk Berhasil"); 
         x = 1; 
         break; 
        } 
       } 
      } 

當你讀br.getUserName()時,它假設給你null。

+0

我將br與訪問值進行比較,那麼訪問者的值是多少? –

+0

訪問器方法應該在一個類中聲明嗎?所以只有2班? –

+0

或者我應該輸入參數? –

相關問題