Reputation: 787
I get an error java.lang.NullPointerException: println needs a message when using Log statement with the following code
for (Application app : mApplications) {
Log.d("LOG", "********");
Log.d("LOG", app.getName());
Log.d("LOG", app.getArtist());
Log.d("LOG", app.getReleaseDate());
}
but in second parameter, If I add another string between quotes the error is gone
for(Application app: mApplications){
Log.d("LOG", "******************");
Log.d("LOG","Name: " +app.getName());
Log.d("LOG","Artist: " +app.getArtist());
Log.d("LOG","ReleaseDate: " +app.getReleaseDate());
}
what is the difference between the two ?
Upvotes: 0
Views: 606
Reputation: 456
First of all if you are using Log.d it is generally use for debugging and this is the correct syntax for Log.d d(String tag, String msg, Throwable tr)
For better understanding See this
In your case First you are using this
for (Application app : mApplications) {
Log.d("LOG", "********");
Log.d("LOG", app.getName());
Log.d("LOG", app.getArtist());
Log.d("LOG", app.getReleaseDate());
}
And generate this error java.lang.NullPointerException: println needs a message
because you can't put message as per Log.d syntax rule.
I hope you understand...
Upvotes: 0
Reputation: 191681
The difference is that you've concatenated a string with the a null variable.
Log
needs a non-null message string.
Alternatively, this also works because it will print "null"
but it doesn't look clean
Log.d("LOG", String.valueOf(app.getName()));
Log.d("LOG", String.valueOf(app.getArtist()));
Log.d("LOG", String.valueOf(app.getReleaseDate()));
Upvotes: 1