Browse Source

fix: exception on Philo when author do not exist

Martin Passard 6 years ago
parent
commit
02ce8a1c55
1 changed files with 9 additions and 2 deletions
  1. 9 2
      src/comportement/Philo.java

+ 9 - 2
src/comportement/Philo.java

@@ -7,6 +7,7 @@ import java.util.Random;
 
 import org.apache.commons.io.FileUtils;
 import org.json.JSONArray;
+import org.json.JSONException;
 import org.json.JSONObject;
 
 import actions.Action;
@@ -83,9 +84,15 @@ public class Philo extends Comportement {
 		int nbrCit = array.length();
 		int choix = random.nextInt(nbrCit);
 		JSONObject quote  = array.getJSONObject(choix);
-		String author = quote.getString("author");
+		final String UNKNOWN = "Inconnu";
+		String author="";
+		try {
+			author = quote.getString("author");
+		}catch(JSONException e) {
+			author = UNKNOWN;
+		}
 		if(author == null) {
-			author = "Inconnu";
+			author = UNKNOWN;
 		}
 		return "\""+quote.getString("quote")+"\" par : "+author;
 	}