Atilla Adrianopolos Atilla Adrianopolos - 29 days ago 7
Android Question

Trying to get string if there are authors, title, publisher, etc

In my book listing app, I'm parsing the JSON to extract the information I need, such as author, publisher, title, etc. For this, I'm using an if statement to get the string if the desired string exists. Below is the method where I'm doing this.

private static List<Books> extractFeatureFromJson(String bookJSON) {

if (TextUtils.isEmpty(bookJSON)) {return null;}

List<Books> books = new ArrayList<>();

try {
JSONObject jsonResponse = new JSONObject(bookJSON);
JSONArray booksArray = jsonResponse.getJSONArray("items");

for (int i = 0; i < booksArray.length(); i++) {
JSONObject currentBook = booksArray.getJSONObject(i);
JSONObject volumeInfo = currentBook.getJSONObject("volumeInfo");
JSONArray authorsArray = volumeInfo.getJSONArray("authors");
String title, publisher, publishedDate, language, pageCount, printType, maturityRating, authors = "";

if(volumeInfo.has("title")){
language = volumeInfo.getString("language");
}

for (int j = 0; j < authorsArray.length(); j++) {
if (volumeInfo.has("authors")){
authors = authorsArray.getString(j);
}
}

if(volumeInfo.has("publisher")){
publisher = volumeInfo.getString("publisher");
}


if(volumeInfo.has("publishedDate")){
publishedDate = volumeInfo.getString("publishedDate");
}

if(volumeInfo.has("language")){
language = volumeInfo.getString("language");
}

if(volumeInfo.has("pageCount")){
pageCount = volumeInfo.getString("pageCount");
}

if(volumeInfo.has("printType")){
printType = volumeInfo.getString("printType");
}

if(volumeInfo.has("maturityRating")){
maturityRating = volumeInfo.getString("maturityRating");
}

Books book = new Books(title, authors, publisher, publishedDate, language, pageCount, printType, maturityRating);
books.add(book);
}


}


However, the parameters in

Books book = new Books(title, authors, publisher, publishedDate, language, pageCount, printType, maturityRating);
books.add(book);


show an error that says variable 'x' might not have been initialized. Anyway I can come across this? I appreciate all help.

This is the JSON Google API:
"https://www.googleapis.com/books/v1/volumes?maxResults=40&q="


And here is the error log:

Error:(145, 40) error: variable title might not have been initialized
Error:(145, 56) error: variable publisher might not have been initialized
Error:(145, 67) error: variable publishedDate might not have been initialized
Error:(145, 82) error: variable language might not have been initialized
Error:(145, 92) error: variable pageCount might not have been initialized
Error:(145, 103) error: variable printType might not have been initialized
Error:(145, 114) error: variable maturityRating might not have been initialized
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

Answer

You declare some strings in your code.

String title, publisher, publishedDate, language, pageCount, printType, maturityRating, authors = "";

But as the error message tells you only the last one is initialized. You have to initialize all variables. Like so:

String title = "", publisher = "", publishedDate = "", language = "", pageCount = "", printType = "", maturityRating = "", authors = "";

Comments