I have a controller where I validate a user and retrieve a list of files(they're file names) from a database. The database has two tables one is users and the other is files. It validates the user properly but wouldn't even go for the method called for fileDAO. I know that the variable fileDAO is empty but should it not be executed just like userDAO whis is null too?
in the line " User person = userDAO.findByname(username);" the variable userDAO is also null, Is it not? But this one calls the method properly and validates the user with that username exists and brings back the user.But in the same way, I am trying to create a variable file and send fileDAO to bring the file that belongs to the matricula/ID given.
What I did not understand is, why does it not show any error in userDAO but in fileDAO yes?
It shows me java.lang.NullPointerException
error. Can anyone help me, please?
private UserDAO userDAO;
private FileDAO fileDAO;
@RequestMapping("/home")
public String validate(@RequestParam("username") String username,
@RequestParam("password") String password, Model model) {
User person = userDAO.findByname(username);
File userfiles = null;
if(person.getPassword().toString().equals(password)) {
String PersonName = person.getPersonname();
model.addAttribute("personname", PersonName);
//
System.out.println("Here the file will be printed and the matricula is");
Long matricula = Long.parseLong(username);
//Userfiles = (List<File>) FileDAO.findByMatricula(matricula);
userfiles = fileDAO.findByMatricula(matricula);
//System.out.println(FileDAO.findByMatricula(matricula));
model.addAttribute("Userfiles", userfiles);
return "Home_e65";
} else {
return "e45_login";
}
}
My FileDAO class is like this:
package com.anjan.udharan.repository;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import com.anjan.udharan.model.File;
@Repository
public class FileDAO {
private class FileMapper implements RowMapper<File> {
public File mapRow(ResultSet row, int rowNum) throws SQLException {
File file = new File();
file.setID(row.getLong("file_id"));
file.setFile_name(row.getString("file_name"));
file.setMatricula(row.getLong("id"));
file.setFile_initial_date(row.getString("file_initial_date"));
file.setFile_final_date(row.getString("file_final_date"));
return file;
}
}
@Autowired
private JdbcTemplate jdbcTemplate;
public File findByMatricula(Long id) {
String sql = "select * from files where id=?";
File file = jdbcTemplate.queryForObject(sql, new
Object[]{id}, new FileMapper());
return file;
}
public List<File> findAll() {
String sql = "select * from files";
List<File> userList = jdbcTemplate.query(sql,
ParameterizedBeanPropertyRowMapper.newInstance(File.class));
return userList;
}
}
And my File.java is like this:
package com.anjan.udharan.model;
public class File {
private Long file_id=null;
private Long id=null;
private String file_name=null;
private String file_initial_date=null;
private String file_final_date=null;
public File(){
}
public File(String file_name, Long id, String file_initial_date, String file_final_date){
this.id = id ;
this.file_name = file_name;
this.file_initial_date = file_initial_date;
this.file_final_date = file_final_date;
}
public Long getID() {
return file_id;
}
public void setID(Long matricula) {
this.file_id = matricula;
}
public Long getMatricula() {
return id;
}
public void setMatricula(Long matricula) {
this.id = matricula;
}
public String getFile_name() {
return file_name;
}
public void setFile_name(String file_name) {
this.file_name = file_name;
}
public String getFile_initial_date() {
return file_initial_date;
}
public void setFile_initial_date(String file_initial_date) {
this.file_initial_date = file_initial_date;
}
public String getFile_final_date() {
return file_final_date;
}
public void setFile_final_date(String file_final_date) {
this.file_final_date = file_final_date;
}
}
My Configuracion class is:
package com.anjan.udharan.config;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
@Configuration
@EnableWebMvc
@ComponentScan
(basePackages = {"com.anjan.udharan.*"})
public class AppConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver jspViewResolver(){
InternalResourceViewResolver resolver = new
InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/");
registry.addResourceHandler("/img/**").addResourceLocations("/resources/img/"); // equivale al dir src/main/webapp/resources/img en la vista de files
registry.addResourceHandler("/js/**").addResourceLocations("/resources/js/");
registry.addResourceHandler("/fonts/**").addResourceLocations("/resources/fonts/");
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/alumnos");
dataSource.setUsername("root");
dataSource.setPassword("");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}