I am new to Java and I am trying to implement a login system and a user profile conform to MVC - DAO. I would like to enable the controller trough the method addUserDatatoView() to retrieve the user credentials from DAO, in order to add them as strings in a new JPanel (view). Anyway, I am not sure that my way to proceed is correct. First of all, I am getting all the time the NullPointerException, event though the DAO-level is getting correctly the data from the database:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "model.User.getUserName()" because "this.user" is null
at controller.LoginController.addUserDatatoView(LoginController.java:75)
at controller.LoginController.showHome(LoginController.java:65)
at controller.LoginController$LoginListener.actionPerformed(LoginController.java:44)
How can I actually retrieve the data from DAO passing through the Model (User class)? What would be the best way to deploy to data as strings from the controller to the view?
I am quite confused about the communication between the different classes and what is the correct procedure, in order not to contravene MVC-DAO.
I am not asking you to solve the problem, but to get an hint in order to get the right direction.
DAO-Implementation:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import controller.HomeController;
import ds.OracleDsSingleton;
import model.Event;
import model.User;
import view.HomeView;
import view.LoginView;
import view.ProfileView;
public class DaoImpl implements DAO {
LoginView view;
ProfileView profView;
ResultSet rs;
public DaoImpl(LoginView view, ProfileView profView) {
this.view = view;
this.profView = profView;
}
@Override
public ArrayList<User> getUserLogIn (String userName, String userPass) throws SQLException {
OracleDsSingleton ora = OracleDsSingleton.getInstance();
boolean controlRecords = false;
try {
//ArrayList type User
ArrayList<User> user = new ArrayList<User>();
Connection con = ora.getConnection();
Statement stmt = con.createStatement();
String addQuery = "SELECT * FROM UserList";
ResultSet rs = stmt.executeQuery(addQuery);
while (rs.next()) {
userName = rs.getString("userName");
userPass = rs.getString("userPass");
if (userName.equals(view.getUserNameTextField().getText().toString())
&& (userPass.equals(view.getUserPassTextField().getText().toString()))) {
{
controlRecords = true;
User u = new User(userName, userPass);
user.add(u);
for(User us : user) {
System.out.println("Directly from DAOImp: " + us);
}
return user;
}
}
else {
continue;
}
}
if (!controlRecords) {
JOptionPane.showMessageDialog(null, "Not successfully logged in!");
};
if (con != null)
con.close();
if (stmt != null)
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
Class User:
public class User {
String userName;
String userPass;
public User(String userName, String userPass) {
this.userName = userName;
this.userPass = userPass;
}
public User() throws SQLException {
}
public String getUserName() {
return userName;
}
public String getUserPass() {
return userPass;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String toString() {
return userName + userPass;
}
}
Controller
package controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JComponent;
import controller.LoginController.LoginBACKListener;
import dao.DAO;
import dao.DaoImpl;
import model.User;
import view.HomeView;
import view.LoginView;
import view.ProfileView;
import view.StartView;
public class LoginController{
private User user;
private LoginView view;
private ProfileView profView;
public LoginController(User user, LoginView view) {
this.user = user;
this.view = view;
addListener();
}
private void addListener() {
this.view.setLoginListener(new LoginListener());
}
class LoginListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String name = view.getUserNameTextField().getText();
String pass = view.getUserPassTextField().getText();
DAO myDAO = new DaoImpl(view, profView);
try {
//when method from DAOImpl get filled, proceed to Home
if(myDAO.getUserLogIn(name, pass) != null) {
showHome();
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
public void showHome() {
HomeView home = new HomeView();
home.setVisible(true);
HomeController h = new HomeController(home);
try {
addUserDatatoView();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ArrayList<User> addUserDatatoView() throws SQLException {
DAO myDAO = new DaoImpl(view, profView);
ArrayList<User> userCredentials = myDAO.getUserLogIn(user.getUserName(), user.getUserPass());
for(User us : userCredentials) {
System.out.println("Directly from Controller: " + us);
}
return userCredentials;
}
}
It is hard to tell from the partial code in the question, but from what I see, a User
object can be constructed only after a successful login. so the constructor should change to:
public LoginController(LoginView view) {
this.view = view;
addListener();
}
And
class LoginListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String name = view.getUserNameTextField().getText();
String pass = view.getUserPassTextField().getText();
DAO myDAO = new DaoImpl(view, profView);
try {
user = myDAO.getUserLogIn(name, pass);//change getUserLogIn to return a single User, or null
//when method from DAOImpl get filled, proceed to Home
if(user != null) {
showHome();
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}