![](/img/trans.png)
[英]My object cannot be initialized because of the “else if” loop for some reason
[英]An inbox JSON object cannot be initialized?
我正在嘗試用Java創建JSON收件箱對象。
public class GetContacts extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
try{
String list = req.getParameter("list");
String profileId = "27";
//Accessing driver from the JAR file
Class.forName ("com.mysql.jdbc.Driver").newInstance();
//Connect to Clockie's database
Connection con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/cdatabase", "root", "root");
//Here we create our query
String sql =
"SELECT * " +
"FROM messages " +
"WHERE profileId = ?";
PreparedStatement statement = con.prepareStatement(sql);
statement.setString(1, profileId);
ResultSet result = statement.executeQuery();
JSONArray messages = new JSONArray();
while(result.next()){
JSONObject message = new JSONObject();
message.put("to", result.getString("to"));
message.put("from", result.getString("from"));
message.put("message", result.getString("message"));
message.put("profileId", profileId);
messages.put(message);
}
System.out.println(messages.toString());
resp.setContentType("text/javascript");
PrintWriter out = resp.getWriter();
String output = req.getParameter("callback") + "(" + messages.toString() + ");";
out.write(output);
out.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
但是我收到以下錯誤消息:
本地變量消息尚未初始化
我做錯了什么?
我使用的JSON類是http://www.json.org/java/中的原始類
順便說一句,我正在使用Eclipse作為我的IDE
好吧,我想我找到了問題所在。 當我嘗試導出Jar文件時,添加的JSON庫出現錯誤。 錯誤出現在JSONArray類中。 如何在Eclipse中將JSON庫導出到jar文件?
這是我做的並得到一些警告(警告在JSONArray類中):
Ok created a new project and right click -> export -> choose Jar Files (under java folder) -> marked both .classpath and .project in the right rectangle -> I selected a random destination folder with the filname org.json.jar. When I did this i got some warnings
沒關系,問題的位置。 我試圖代替定義變量的消息變量messagesss
作為JSONArray OBJ。 我仍然收到與message
not messagessss
相同的錯誤。 但是我該如何糾正這個問題?
java類位於名為messages的程序包下,這是問題嗎?
如果clean + build無法幫助您刷新項目或重新啟動IDE。 我將此代碼復制到Eclipse中,沒有任何問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.