Log.d doesn't print out message - android

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 ?

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()));

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...

Related

firebase crash reporting does not logs normal logs without expection

I am using firebase crash in my cordova application and I want to capture all logs ( Log.e ) in my application as follows.
Login => {"userName" : "jhon" , "networkConnection": "on" ,
"status":"success"}
AddContact => {"userName" : "jhon" , "networkConnection": "on" ,
"status":"success", "contactId" :"3233"}
But I have noticed one thing is that if my app gets crashed then only I am able to see logs without that I can't see the normal logs when I app do not crash.
For example ,
In following code
If I explicitly call FirebaseCrash.report(new Exception(name)); then only FirebaseCrash.logcat(Log.ERROR, name, params + " "+message); appears in firebase crash report.
If I only write FirebaseCrash.report(new Exception(name)); then only FirebaseCrash.logcat(Log.ERROR, name, params + " "+message); the these logs are not coming to the server.
private void logCrash(final CallbackContext callbackContext, final String name, final String params,final String message){
cordova.getThreadPool().execute(new Runnable() {
#Override
public void run() {
FirebaseCrash.logcat(Log.ERROR, name, params + " "+message);
FirebaseCrash.report(new Exception(name));
}
});
callbackContext.success();
}

ParseUser SignUpInBackground issue: Value == Null

I've been trying to use the Parse library to access MongoDB in order to create users or objects for my Android application. The issue is that every time I attempt to SignUpInBackground or SignInInBackground I get this super unspecific error.
"java.lang.NullPointerException: value == null"
Just value == null.. I have no idea which value this refers to but the most frustrating part is that the code I'm using works perfectly in one app and despite copying it over, it always returns this error.
I usually wouldn't bother making a question for my issue but I've been trying to find a solution to this for about a week now and have been unable to find anything related to my issue online.
Hope someone out there has seen this before, as I'm about out of ideas for this one. Thanks in advance to anyone who gets back to me on this :)
The Sign Up Method:
private void signUp(){
String username = "Andy"; //Temp
ParseUser user = new ParseUser();
user.setUsername(username);
user.setEmail(txtUsername.getText().toString());
user.setPassword(txtPassword.getText().toString());
Log.i(TAG,"SignUp method. Parse user = " + user.getUsername() + ", " + user.getEmail());
user.signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
if(e==null){
Log.i(TAG, "Successfully created new user!");
signIn();
} else {
Log.i(TAG, "Failed to create new user. Error: " + e.getMessage());
Toast.makeText(getContext(),"Sign up failed! " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
Error:
I/LogIn: Validation success
I/LogIn: SignUp method. Parse user = Andy, test
I/LogIn: Failed to create new user. Error: java.lang.NullPointerException: value == null
Edit:
While stepping through the parse libraries, I ended up with this error message several times in the log.
I/art: Rejecting re-init on previously-failed class java.lang.Class

ParseQuery simply not running, with no response, not logs written

Why does parse not appear to be doing anything with my query whatsoever. It's almost as if the entire block is ignored..
try {
Log.w("UpdateContacts", "Attempting an update");
List<ParseObject> result = friendshipAndUserQuery.find();
for (ParseObject friendship : result) {
Log.i("UpdateContacts", "Found friendship" + friendship.getObjectId());
UserObject friend = (UserObject) friendship.getParseObject("to");
Log.i("UpdateContacts", "Converted friendship to friend " + friend.getObjectId());
contacts.add(friend);
((MainActivity) mContext).notify("UpdateContacts", friend.getObjectId() + " / " + friend.getUsername(), true);
}
} catch (ParseException e) {
Log.e("ParseException", e.toString());
((MainActivity) mContext).notify("UpdateContacts", e.toString(), true);
}
I get the "attempting an update" Log, but nothing else. I can see from my Parse portal that the query has run, my android simply gets nothing back at all.. not an empty set, nothing. No logs were made..
Solved
Query referenced the incorrect table name (case sensitive) and Parse simply stops all related code, rather than spouting out an error..

How to save Log message to a string in android?

Is there any way that I could directly redirect the log message in Android to a string?
For example, I have a log message in Android.
Log.v(TAG, "First Name" + firstName);
Log.v(TAG, "Last Name" + lastName);
I have a string
String nameMessage = "Please check your credetials\n" ;
I want to concatenate the log to the message.
One way is to concatenate manually.
I am looking If we could redirect the log to message.
I'm not sure if I am understanding your question right, but technically you could just create a function that appends the log message and then actually logs it?
Example:
private StringBuilder errorMessage;
public void log(String tag, String message) {
errorMessage.append(message).append(System.getProperty("line.separator"));
Log.v(tag, message);
}
public void getErrorMessage() {
return errorMessage.toString();
}
And then, you'd just replace all your calls to Log#v with a call to #log(tag, message).

What is common way to log exception if message is not a concern

I was wondering, what is the common way to log exception, if message is not my concern.
// exp is Exception.
Log.e(TAG, "", exp);
or
// exp is Exception.
Log.e(TAG, null, exp);
You can use Log.getStackTraceString if you want to avoid the extra message line in your log:
Log.e(TAG, Log.getStackTraceString(exp));
You should really write a message that is contextual and would make the log message explicit.
Something like:
Log.e(TAG, "Null Pointer when parsing my internet response. Is the web service down?", exp);
but if you really don't want to worry about it, you could wrap the Log class with your own class
public class MyLog {
private static final String TAG = "MyApp";
public static void e(Exception e){
Log.e(TAG, "", e);
}
}
Then use it like this:
MyLog.e(exp);
If you insist on going against best practices, at least use the exception message:
Log.e(TAG, exp.getMessage(), exp);
To do anything less is irresponsible.

Categories

Resources