Q:

JAX-RS POST NullPointerException

JAX-RS Post method gives a NullPointerException.
Severe:   java.lang.NullPointerException
    at fi.allu.savukelaskuri.ConsumptionResource.postJson(ConsumptionResource.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

Here is MySQL database connectivity using JDBC:
import java.sql.*;

public class Database {

    protected Connection conn = null;
    protected PreparedStatement preparedstatement = null;
    protected Statement statement = null;
    protected ResultSet resultset = null;

    public boolean openCOnnection() {
        boolean ok = true;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/savukelaskuri?serverTimezone=UTC", "root", "");
        } catch (Exception e) {
            e.printStackTrace();
            ok = false;
        }
        return ok;
    }

    public boolean closeConnection() {
        boolean ok = true;
        try {
            this.conn.close();
        } catch (Exception e) {
            ok = false;
        }
        return ok;
    }
}

Here is REST root resource class and all the methods:
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import org.json.JSONArray;
import org.json.JSONObject;

@Path("consumptions")
public class ConsumptionResource extends Database {

    JSONArray jsonarray = new JSONArray();
    JSONObject jsonobject = null;

    @Context
    private UriInfo context;

    public ConsumptionResource() {
        this.openCOnnection();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String getJson() {
        try {
            statement = conn.createStatement();
            String sql = "SELECT * FROM kulutus";
            resultset = statement.executeQuery(sql);

            while (resultset.next()) {
                jsonobject = new JSONObject();
                jsonobject.put("id", resultset.getInt("id"));
                jsonobject.put("pvm", resultset.getString("pvm"));
                jsonobject.put("klo", resultset.getString("klo"));
                jsonobject.put("kulutus", resultset.getInt("kulutus"));
                jsonarray.put(jsonobject);

            }
            this.closeConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonarray.toString(4);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public boolean postJson(String content) {
        boolean ok = true;
        JSONObject json = new JSONObject(content);
        String date = json.getString("date");
        String time = json.getString("time");
        String consumption = json.getString("consumption");

        try {
            String sql = "INSERT INTO kulutus (pvm, klo, kulutus) VALUES (?,?,?)";
            this.preparedstatement.setString(1, date);
            this.preparedstatement.setString(2, time);
            this.preparedstatement.setString(3, consumption);
            this.closeConnection();
        } catch (Exception e) {
            e.printStackTrace();
            ok = false;
        }

        return ok;
    }

    @DELETE
    public boolean deleteJson() {
        boolean ok = true;
        try {
            statement = conn.createStatement();
            String sql = "DELETE FROM kulutus";
            statement.executeQuery(sql);
            this.closeConnection();
        } catch (Exception e) {
            e.printStackTrace();
            ok = false;
        }
        return ok;
    }

    @GET
    @Path("{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getId(@PathParam("id") String id) {
        JSONArray jsonarray = new JSONArray();
        try {
            String sql = "SELECT * FROM kulutus WHERE id = ?";
            this.preparedstatement = this.conn.prepareStatement(sql);
            this.preparedstatement.setString(1, id);
            this.resultset = this.preparedstatement.executeQuery();
            while (resultset.next()) {
                JSONObject jsonobjekti = new JSONObject();
                jsonobjekti.put("date", resultset.getString("pvm"));
                jsonobjekti.put("time", resultset.getString("klo"));
                jsonobjekti.put("consumption", resultset.getString("kulutus"));
                jsonarray.put(jsonobjekti);
            }
            this.closeConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonarray.getString(4);
    }

    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public boolean putJson(@PathParam("id") String id, String content) {
        boolean ok = true;
        JSONObject json = new JSONObject(content);
        String date = json.getString("pvm");
        String time = json.getString("klo");
        String consumption = json.getString("kulutus");

        try {
            String sql = "UPDATE kulutus SET pvm = ?, klo = ?, kulutus = ? WHERE id = ?";
            this.preparedstatement = conn.prepareStatement(sql);
            this.preparedstatement.setString(1, date);
            this.preparedstatement.setString(2, time);
            this.preparedstatement.setString(3, consumption);
            this.preparedstatement.setString(4, String.valueOf(id));
            this.preparedstatement.execute();
            this.closeConnection();
        } catch (Exception e) {
            e.printStackTrace();
            ok = false;
        }

        return ok;
    }

    @DELETE
    @Path("{id}")
    public boolean deleteId(@PathParam("id") String id) {
        boolean ok = true;
        try {
            String sql = "DELETE FROM kulutus WJERE id = ?";
            this.preparedstatement = conn.prepareStatement(sql);
            this.preparedstatement.setInt(1, Integer.valueOf(id));
        } catch (Exception e) {
            e.printStackTrace();
            ok = false;
        }
        return ok;
    }
}

I'm expecting the result to be a successful post to databse, but instead I am getting a NullPointerException.
EDIT: I changed the code to english now, so it should be easier to read.

A:

I am suspecting below line is throwing NullPointerException. I don't see preparedstatement is intialized anywhere in Database and its null.
this.preparedstatement.setString(1, date);

