简体   繁体   English

JAX-RS POST NullPointerException

[英]JAX-RS POST NullPointerException

JAX-RS Post method gives a NullPointerException. JAX-RS Post方法提供了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: 这是使用JDBC的MySQL数据库连接:

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: 这是REST根资源类和所有方法:

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. 我期望结果将是成功发布到数据库,但我却收到了NullPointerException。

EDIT: I changed the code to english now, so it should be easier to read. 编辑:我现在将代码更改为英语,所以它应该更容易阅读。

方法postJson()从不执行准备好的语句。

I am suspecting below line is throwing NullPointerException. 我怀疑下面的行抛出NullPointerException。 I don't see preparedstatement is intialized anywhere in Database and its null. 我不明白preparedstatement在任何地方intialized Database及其零。

this.preparedstatement.setString(1, date);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM