Skip to content

Commit 0634427

Browse files
committed
Fixes NPE when not specifying language.
1 parent 97ebecb commit 0634427

File tree

1 file changed

+12
-5
lines changed
  • language/analysis/src/main/java/com/google/cloud/language/samples

1 file changed

+12
-5
lines changed

language/analysis/src/main/java/com/google/cloud/language/samples/AnalyzeBeta.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,19 @@ public static void printEntities(PrintStream out, List<Entity> entities) {
131131
* Gets {@link Sentiment} from the string {@code text}.
132132
*/
133133
public Sentiment analyzeSentimentText(String text, String lang) throws IOException {
134-
// NL autodetects the language
135-
136134
// Note: This does not work on App Engine standard.
137-
Document doc = Document.newBuilder()
138-
.setLanguage(lang)
139-
.setContent(text).setType(Type.PLAIN_TEXT).build();
135+
// NL autodetects the language
136+
Document doc;
137+
if (lang != null) {
138+
doc = Document.newBuilder()
139+
.setLanguage(lang)
140+
.setContent(text).setType(Type.PLAIN_TEXT)
141+
.build();
142+
} else {
143+
doc = Document.newBuilder()
144+
.setContent(text).setType(Type.PLAIN_TEXT)
145+
.build();
146+
}
140147
AnalyzeSentimentResponse response = languageApi.analyzeSentiment(doc);
141148
return response.getDocumentSentiment();
142149
}

0 commit comments

Comments
 (0)