I try lots of thing to fix this error but I can't succeed.
I developed a simple project using spring framework and swing library.
I created my UI using swing to be easy.
I didnt use web dependency because I dont need it.
My main aim is that create an database and business logic using spring framework and pull data to fill tables, text fields in GUI which are created.
However, @Autowired
doesn't work all around to project.I checked many times for true implementation but I didn't find any error. Maybe it is a side affect of swing or any other thing but I don't know. Can you check one more time to solve problem?
When runtime reaches to securityService.doctorLogin(username,password)
in controller class, it gives NullPointerException
.
include lines /Controller.java
import org.springframework.beans.factory.annotation.Autowired;
@Transactional(propagation = Propagation.NEVER)
public class Controller {
public static Controller controller;
@Autowired
SecurityServiceInterface securityService;
public static Controller getController() {
if(controller==null) {
controller=new Controller();
}
return controller;
}
public boolean validateLogin(String username,String password,String user) {
switch(user) {
case "Doctor":
securityService.doctorLogin(username, password);
break;
case "Patient":
securityService.patientLogin(username,password);
case "Nurse":
return securityService.nurseLogin(username, password);
case "Relative":
return securityService.relativeLogin(username, password);
}
return false;
}
}
include lines /ServiceImpl.java
import org.springframework.beans.factory.annotation.Autowired;
@Service("SecurityServiceInterface")
@Transactional(propagation = Propagation.REQUIRED)
public class ServiceImpl implements SecurityServiceInterface {
@Autowired
private DoctorRepository doctorRepo;
@Autowired
private NurseRepository nurseRepo;
@Autowired
private PatientRepository patientRepo;
@Autowired
private RelativeRepository relativeRepo;
@Autowired
private DoctorPatientRepository doctorPatientRepo;
@Override
public boolean doctorLogin(String userName,String password){
try {
Doctor doctor=doctorRepo.findDoctor(userName);
if(doctor.getPassword()==password) {
return true;
}
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public boolean patientLogin(String userName,String password) {
System.out.println("hereee");
Patient patient=patientRepo.findPatient(userName);
if(patient!=null) {
if(patient.getPassword()==Encryption.sha256Encrypt(password)) {
return true;}
return false;
}
return false;
}
@Override
public boolean nurseLogin(String userName,String password) {
Nurse nurse=nurseRepo.findNurse(userName);
if(nurse!=null) {
if(nurse.getPassword()==Encryption.sha256Encrypt(password)) {
return true;}
return false;
}
return false;
}
include lines/main.java
@SpringBootApplication
public class NetworkSecurity1Application {
private static final Logger logger =
LoggerFactory.getLogger(NetworkSecurity1Application.class);
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(NetworkSecurity1Application.class);
builder.headless(false).run(args);
}
@Bean
public CommandLineRunner setup(PatientRepository patientRepo,DoctorRepository doctorRepo) {
return (args) -> {
logger.info("Preloading " + patientRepo.save(new Patient("samet", "calis","12.12.2010")));
doctorRepo.save(new Doctor("ali","veli","sa"));
logger.info("The sample event data has been generated");
try {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringContext.class);
LoginView loginView=(LoginView) context.getBean("loginView");
loginView.setVisible(true);
}catch(Exception e) {
e.printStackTrace();
}
};
}
}