Em có một thư viện tiện ích (Bài tập mẫu):
package helper;
public class shareHelper {
/**
* Ảnh biểu tượng của ứng dụng, xuất hiện trên mọi cửa sổ
*/
public static final Image APP_ICON;
static{
// Tải biểu tượng ứng dụng
String file = "";
APP_ICON = new ImageIcon(shareHelper.class.getResource(file)).getImage();
}
/**
* Sao chép file logo chuyên đề vào thư mục logo
* @param file là đối tượng file ảnh
* @return chép được hay không
*/
public static boolean saveLogo(File file){
File dir = new File("logos");
// Tạo thư mục nếu chưa tồn tại
if(!dir.exists()){
dir.mkdirs();
}
File newFile = new File(dir, file.getName());
try {
// Copy vào thư mục logos (đè nếu đã tồn tại)
Path source = Paths.get(file.getAbsolutePath());
Path destination = Paths.get(newFile.getAbsolutePath());
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
return true;
}
catch (Exception ex) {
return false;
}
}
/**
* Đọc hình ảnh logo chuyên đề
* @param fileName là tên file logo
* @return ảnh đọc được
*/
public static ImageIcon readLogo(String fileName){
File path = new File("logos", fileName);
return new ImageIcon(path.getAbsolutePath());
}
/**
* Đối tượng này chứa thông tin người sử dụng sau khi đăng nhập
*/
public static NhanVien USER;
/**
* Xóa thông tin của người sử dụng khi có yêu cầu đăng xuất
*/
public static void logoff() {
shareHelper.USER = null;
}
/**
* Kiểm tra xem đăng nhập hay chưa
* @return đăng nhập hay chưa
*/
public static boolean authenticated() {
return shareHelper.USER != null;
}
}
Khi em gọi hàm đăng nhập thì bị lỗi ngay chỗ: shareHelper.USER = nhanVien;
.
Mọi người giúp em sửa lỗi với ạ.
void login(){
String manv = txtMaNV.getText();
String matKhau = new String(txtMatKhau.getPassword());
try {
NhanVien nhanVien = dao.findByld(manv);
if(nhanVien != null){
String matKhau2 = nhanVien.getMatKhau();
if(matKhau.equals(matKhau2))
{
shareHelper.USER=nhanVien;
DialogHelper.alert(this, "Đăng nhập thành công !");
this.dispose();
}
else{
DialogHelper.alert(this, "Sai mật khẩu!");
}
}
else{
DialogHelper.alert(this, "Sai tên đăng nhập!");
}
}
catch (Exception e) {
DialogHelper.alert(this, "Lỗi truy vấn dữ liệu!");
}
}