I'm trying to update the users table using the update operation but it somehow sends me this error in the log and doesn't let me put JSON format in it.
java.lang.NullPointerException: Cannot invoke "com.common.db.domain.Student.getId()" because "student" is nulldata insert unsuccessful
And in Postman it looks like this:
How do I need to change the code to remove the null pointer exception here is my other code:
My StudentResource
@Path("/updatestudent")
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public Student updateStudent(Student student)
{
return studentService.updateStudent(student);
}
My StudentService
public Student updateStudent(Student student) {
String insert = "update users set username=?, password=?, fullname=?,email=? where user_id=?";
Connection connection = null;
try{
connection = new MysqlDbConnectionService().getConnection();
PreparedStatement ps= connection.prepareStatement(insert);
ps.setString(1,student.getId());
ps.setString(2,student.getUsername());
ps.setString(3,student.getPassword());
ps.setString(4,student.getFullName());
ps.setString(5,student.getEmail());
ps.executeUpdate();
}catch(Exception e)
{
System.out.println(e + "data insert unsuccessful");
}
return student;
}
Student Model:
package com.common.db.domain;
import com.google.gson.annotations.SerializedName;
public class Student {
@SerializedName("id")
private String id;
@SerializedName("username")
private String username;
@SerializedName("password")
private String password;
@SerializedName("fullname")
private String fullName;
@SerializedName("email")
private String email;
public Student()
{
}
public Student(String id, String username, String password, String fullName, String email)
{
super();
this.id=id;
this.username = username;
this.password = password;
this.fullName = fullName;
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Is the problem here in the code or am I missing something different.