0

[enter image description here][1] this is my code where i got java.ioNotSerializabale Exception when i run below query i got this exception

java.io.NotSerializableException

my pojo class is here public class ProjectHelper implements Serializable {

private String name;
private String description;
private String company;
private String category;
private List<UserRegister> usersRegisterList;

private String startDate;

And this is my DaoImpl method where i got error "java.io.NotSerializableException"

@Override
public GenericModal getProjects(User email) throws SQLException {

    String sql = "select p.project_name, p.project_description, p.company,                      p.start_date, p.end_date, " +
            "p.category f.task, f.milestones, f.billing, f.time_log, f.messages, f.features_id, f.files, u.firstName" +
            "u.lastname, u.email" +
            " from project as p" +
            "join userprojectrelation as upr" +
            "on p.project_id=upr.project_id " +
            "join users as u" +
            "on u.UserId=upr.user_id" +
            "join features as f " +
            "on f.project_id=p.project_id" +
            "where u.email=?";
    int status = 0;
    GenericModal genericModal=new GenericModal();

    try {
        List<ProjectHelper> projectHelper=jdbcTemplate.query(sql,new Object[]{email},new ProjectHelperMapper());
        if (projectHelper.isEmpty()) {
            return null;
        } else {
            genericModal.setObject(projectHelper);
        }
       // return genericModal;
    } catch (Exception ex) {
        genericModal.setException(ex.getMessage());
    }

    return genericModal;
}
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452

1 Answers1

0

It's probably UserRegister that isn't Serializable. If you want to serialize a class, all of it's components (fields) must also be serializable, unless you implement custom serialization methods or mark the fields as transient.

Jeremy Gurr
  • 1,483
  • 6
  • 10
  • I already made ProjectHelper class serilizable which cover all pojos – Udaib Khan Dec 13 '16 at 19:35
  • Also my guess. Also the List interface is not serializable, but many implementations are, however this is not an issue as serialization is only checked runtime. – Klaus Groenbaek Dec 13 '16 at 19:37
  • 1
    @UdaibKhan it is not enough to make ProjectHelper Serializable, every type it references (that is not transient) must be serializable as well. – Klaus Groenbaek Dec 13 '16 at 19:38
  • @KlausGroenbaek i made all its refernces serializable but error is same – Udaib Khan Dec 13 '16 at 19:44
  • Like I said in the answer, you can't just mark the class as Serializable. You must also make all components serializable, or manually do the serialization yourself. Otherwise, how is the auto-serialization code supposed to know how to serialize UserRegister? – Jeremy Gurr Dec 13 '16 at 19:45
  • We need to see the code. How did you make it serializable? – Jeremy Gurr Dec 13 '16 at 19:45
  • public class ProjectHelper implements Serializable { private String name; private String description; private String company; private String category; private List usersRegisterList; private String startDate; private String endDate; private String task; private String mileStones; private String billing; private String files; private String timeLog; private String message; } – Udaib Khan Dec 13 '16 at 19:48
  • sorry this is my first time i am posting questions in stackover flow so i making mistakes while posting – Udaib Khan Dec 13 '16 at 19:50
  • That's only ProjectHelper. That's not the problem. The problem is in your UserRegister class. That's the code we need to see. – Jeremy Gurr Dec 13 '16 at 19:50
  • public class UserRegister implements Serializable { private int userId; private String firstName; private String lastName; private String email; private String regPassword; private String companyName; private String siteAddress; private String phoneNumber; private String role; private String message; public UserRegister(){} public String getRole() {return role;} public void setRole(String role) { this.role = role; } public int getUserId() { return userId; } – Udaib Khan Dec 13 '16 at 19:53
  • @JeremyGurr the above code is my UserRegister modal – Udaib Khan Dec 13 '16 at 19:55
  • Hmm... that should be fine since it only consists of Strings, and an int. – Jeremy Gurr Dec 13 '16 at 19:55
  • Then the next potential trouble spot would be what exactly the class of projectHelper is. It's some kind of List. If you can log that or debug it, and tell us the exact class, it might lead to a solution. – Jeremy Gurr Dec 13 '16 at 19:56
  • @JeremyGurr sorry i did'nt get it what you are saying? plz explain it – Udaib Khan Dec 13 '16 at 19:59
  • 1
    why is it serializable in the first place? it looks like ProjectHelper holds data from a database. Serialization is typically used for transport and sometimes persistence. If they are already in the DB, it's probably not persistence, if it is transport I recommend a human readable format like JSON, rather than Java's binary serialization. – Klaus Groenbaek Dec 13 '16 at 20:03
  • @KlausGroenbaek ProojectHelper is getting data from bootstrap modal and then passing data to multiple pojos for futher persistence process and also ProjectHelper is used here for holding data from DB – Udaib Khan Dec 13 '16 at 20:09
  • In this line of code: genericModal.setObject(projectHelper); we need to know the exact class of projectHelper. Is it an ArrayList, or something else? If it's something that's not serializable, then that's your problem. – Jeremy Gurr Dec 13 '16 at 20:17
  • @JeremyGurr GenericModal is simple class used for handling exception and holding other related data i am sharing "GenericModal" class below ..... public class GenericModal{ private String exception; private List list; private Object object; private int status; //getters and setters – Udaib Khan Dec 13 '16 at 20:22
  • i m returning json object form spring controllerr i have web service which is returning all projects to user . @ResponseBody @RequestMapping(value = "/projects", method = RequestMethod.GET, produces = "application/json") public ProjectHelper getProjects(Model model, HttpSession session) throws SQLException { User user = (User) session.getAttribute("LOGGEDIN_USER"); GenericModal genericModal = projectService.getProjects(user); ProjectHelper projectHelper = (ProjectHelper) genericModal.getObject(); return projectHelper; } – Udaib Khan Dec 13 '16 at 20:30