gpt4 book ai didi

java - JAX-RS POST NullPointerException

转载 作者:行者123 更新时间:2023-11-29 16:26:12 24 4
gpt4 key购买 nike

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)

这是使用 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;
}
}

这是 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;
}
}

我希望结果能够成功发布到数据库,但我却收到了 NullPointerException。

编辑:我现在将代码更改为英文,因此应该更容易阅读。

最佳答案

我怀疑下面的行抛出 NullPointerException。我没有看到 preparedstatementDatabase 中的任何位置被初始化并且它为 null。

this.preparedstatement.setString(1, date);

关于java - JAX-RS POST NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54218785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com