从zoho报告数据库中读取空值时处理NullPointerException

时间:2016-02-15 14:34:03

标签: java json nullpointerexception

Chatlet Created
Chatlet Thrown
Inside reply
5684dc24617149a601dea913	Yes, Please let me in.
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.teamchat.client.sdk.impl.TeamchatAPIImpl.invokeMethod(Unknown Source)
	at com.teamchat.client.sdk.impl.TeamchatAPIImpl.onMsg(Unknown Source)
	at com.teamchat.client.sdk.events.EventStreamConnector.onMsg(Unknown Source)
	at com.teamchat.client.sdk.impl.SimpleEventsExecutor.notifyCallbackMessage(Unknown Source)
	at com.teamchat.client.sdk.events.EventStreamImpl.handleLine(Unknown Source)
	at com.teamchat.client.sdk.events.EventStreamImpl.fetchEventStream(Unknown Source)
	at com.teamchat.client.sdk.events.EventStreamImpl.fetchOnceAndReturnInterruptedStatus(Unknown Source)
	at com.teamchat.client.sdk.events.EventStreamConnector.getEvents(Unknown Source)
	at com.teamchat.client.sdk.events.EventStreamConnector.mainloop(Unknown Source)
	at com.teamchat.client.sdk.events.EventStreamConnector.access$000(Unknown Source)
	at com.teamchat.client.sdk.events.EventStreamConnector$1.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
	at frontEnd.sendRequest.onReply(sendRequest.java:75)
	... 16 more

我目前正在处理任务,我需要从数据库单元格中读取空值。但是当我尝试这样做时,我的代码会抛出NullPointerException。我尝试使用try catch块处理它,但没有任何帮助我。

public String getCellData(String sql_query) throws Exception	{
		System.out.println("Inside getCellData");
		String cell_data = "";
		try{
			JSONArray queried_data = (JSONArray)db_work.retrieveQueryRows(db_work.retrieveData(sql_query).toString());
			for (int i = 0; i < queried_data.size(); i++) {
				JSONArray queried_row=(JSONArray) queried_data.get(i);
				cell_data = queried_row.get(0).toString();
			}
					
		}
		catch(NullPointerException e)
		{
			cell_data="empty";
			return cell_data;
		}
		return cell_data;
	}
if(response.equals("Yes, Please let me in.")){
			String gender = fetch_details.getCellData("select * from Group_Join where Room='"+room_id+"'");
			System.out.println(gender);
			String goal = fetch_details.getCellData("select * from Group_Join where Room='"+room_id+"'");
			System.out.println(goal);
			api.performPostInCurrentRoom(new TextChatlet("Sure, thanks. We will add you to the support group."));
			if (goal.equals("empty")) {
				if (gender.equals("empty")) {
					System.out.println("Gender and Goal Both Empty");
					PrimaryChatlet gender_goal_chatlet=new PrimaryChatlet();
					gender_goal_chatlet.alias("GenderGoal");
					Form gender_goal_form=api.objects().form();
					gender_goal_form.addField(api.objects().select().name("Goal").label("Goal").addOption("Weight Loss").addOption("Weight Gain").addOption("Muscle Tone"));
					gender_goal_form.addField(api.objects().select().name("Gender").label("Gender").addOption("Male").addOption("Female"));
					gender_goal_chatlet.setReplyScreen(gender_goal_form);
					api.performPostInCurrentRoom(gender_goal_chatlet);
				}

1 个答案:

答案 0 :(得分:0)

不建议捕获运行时异常(NullPointerException是)。

你为什么不首先试图避免它?

类似的东西:

if (queried_row.get(0).toString() != null)
  cell_data = queried_row.get(0).toString();
else
  cell_data = "empty";

cell_data = (queried_row.get(0).toString() != null) ? queried_row.get(0).toString() : "empty";