I have this NullPointerException runtime exception, so I suspect that it's my getter method in the entity that's not working. But I am not sure, and I don't know how to fix it. Please kindly correct me if you could.
I am now using Java as Backend to accept the frontend request.
A. I looked for the 1. official documents :https://docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html, 2. How to solve java.lang.NullPointerException error? 3. NullPointerException on calling getter method
B. I was expecting to see the printing result of user.getUsername() and opinion.getCollaboratorName(), but I only got the previous one. The 'opinion.getCollaboratorName() is null' is just not clear to me.
- This is my controller:
@RestController
@RequestMapping("/forum"){
 private final JwtDecoder jwtDecoder;
    private final UserServiceImpl userServiceImpl;
    private final OpinionsRepository opinionsRepository;
    @Autowired
    //constructor
    public OpinionsController(JwtDecoder jwtDecoder, UserServiceImpl userServiceImpl, OpinionsRepository opinionsRepository) {
        this.jwtDecoder = jwtDecoder;
        this.userServiceImpl = userServiceImpl;
        this.opinionsRepository = opinionsRepository;
    }
@PutMapping("/opinions")
 public ResponseEntity<?> updateOpinion(**@RequestBody Opinion opinion**, @CookieValue(name = "dashboardToken", required = true) String dashboardToken, HttpServletResponse response) {
        System.out.println("Java 3 at putmapping/opinions to edit individual posts,outside try, {id}" + **opinion.toString()**);
        try {
            System.out.println("Printing out the @RequestBody Opinion");
            **System.out.println(opinion);**
            System.out.println("Java 4 inside try put mapping");
            String username = jwtDecoder.decodeUserInfoFromDashboardToken(dashboardToken);
            System.out.println("email in PutMapping Opinions:" + username);
            System.out.println("OpinionID in PutOpinions id:");
            System.out.println(opinion.getId());
            User user = userServiceImpl.findUserByEmail(email);  //User is an Entity
            **System.out.println("opinion.getCollaboratorName()");
            System.out.println(opinion.getCollaboratorName());**//how is this possible??
            if (!Objects.equals(user.getUsername(), opinion.getCollaboratorName())) {
                return ResponseEntity.status(HttpStatus.FORBIDDEN).body("The Collaborator does not correspond with the author of the article in the Database. Please contact us.");
            }
            response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE,OPTIONS");
            response.addHeader("Access-Control-Allow-Headers", "origin, content-type, accept");
            //response.addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
            //response.addHeader("Access-Control-Allow-Credentials", "true");
            response.addHeader("Access-Control-Expose-Headers", "loginToken");
            response.addHeader("Access-Control-Max-Age", "3600");
            System.out.println("Hey!!!This is the response from putmapping,process almost done:" + response.toString());
            return new ResponseEntity<Opinion>(opinionsRepository.save(opinion), HttpStatus.OK);
        } catch (UserNotFoundException e) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body("The Collaborator does not correspond with the author of the article in the Database. Please contact us.");
        } catch (Exception e) {
            System.out.println("Oops, something went wrong. at getmapping opinions line 55");
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Collections.emptyList());
        }
    } 
}
- This is my Entity called "Opinion"
@Entity
@Table(name="opinion")
public class Opinion {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    @Column(name="collaborator_name",nullable=false)
    private String collaboratorName;
    @Column(nullable=false)
    private String title;
    @Column(nullable=false)
    private String body;
    @Column(nullable=false)
    private String category;
    @Column(name="updated_at")
    @UpdateTimestamp
    private Date updatedAt;
    @Column(name="created_at",nullable=false,updatable=false)
    @CreationTimestamp
    private Date createdAt;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public void setCollaboratorName(String collaboratorName) {
        this.collaboratorName = collaboratorName;
    }
**    public String getCollaboratorName() {
        return collaboratorName;
    }**
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    public Date getCreatedAt() {
        return createdAt;
    }
    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }
    public Date getUpdatedAt() {
        return updatedAt;
    }
    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }
    @Override
    public String toString() {
        return "Opinion{" +
                "id=" + id +
                ", collaboratorName='" + collaboratorName + '\'' +
                ", title='" + title + '\'' +
                ", body='" + body + '\'' +
                ", category='" + category + '\'' +
                ", updatedAt=" + updatedAt +
                ", createdAt=" + createdAt +
                '}';
    }
}
- This is what the Java Console shows:
Java 3 at putmapping/opinions to edit individual posts,outside try, {id}Opinion{id=8, **collaboratorName='null'**, title='Hi there', body='Opps I hope it wrks', category='gender equality', updatedAt=Tue Jul 04 14:19:12 CST 2023, createdAt=Tue Jul 04 14:19:12 CST 2023}
Printing out the @RequestBody Opinion
Opinion{id=8, **collaboratorName='null'**, title='Hi there', body='Opps I hope it wrks', category='gender equality', updatedAt=Tue Jul 04 14:19:12 CST 2023, createdAt=Tue Jul 04 14:19:12 CST 2023}
Opinion{id=8, **collaboratorName='null'**, title='Hi there', body='Opps I hope it wrks', category='gender equality', updatedAt=Tue Jul 04 14:19:12 CST 2023, createdAt=Tue Jul 04 14:19:12 CST 2023}
Java 4 inside try put mapping
Hi This is from decodeUserInfoFromDashboardToken in JwtDecoder, and the Claims are:
{sub=Stasy, opinionsList=[{id=8, **collaboratorName=Stasy**, title=Hi there, body=Opps I hope it wrks, category=gender equality, updatedAt=1688451552464, createdAt=1688451552464}, {id=9, collaboratorName=Stasy, title=Now Tuesday, body=Hi there, TUesday again, category=gender equality, updatedAt=1688451787036, createdAt=1688451787036}, {id=10, collaboratorName=Stasy, title=11th Revolution, body=I hope this time it works, category=gender equality, updatedAt=1688452876627, createdAt=1688452876627}], email=t.hsuanhsieh@gmail.com, password=1128, username=Stasy, iat=1688628751}
**result from getUserInfoFromLoginToken:Stasy
username in PutMapping Opinions:Stasy**
OpinionID in PutOpinions id:
8
Hibernate: select u1_0.userid,u1_0.email,u1_0.password,u1_0.username from users u1_0 where u1_0.email=?
**opinion.getCollaboratorName()
null** 
