Splitting a string by newlines can cause bothering bugs. Please see my answer.
Original question - XY Problem caused by not being able to see invisible chars in log.
public void DoCommand(String cmd)
{
String [] words=cmd.split(" ");
if (words == null)
{
Log.e(TAG, "Words null");
return;
}
if (words.length == 0)
{
Log.e(TAG, "Words short");
return;
}
String program=words[0].toLowerCase();
Method method=commandmap.get(program);
try
{
method.invoke(cmdimpl, (Object) words);
} catch (IllegalAccessException |
IllegalArgumentException |
InvocationTargetException e)
{
Log.e(TAG, "invocation error", e);
sender.SendError(e);
}
catch (NullPointerException e)
{
Log.e(TAG, "UNknown cmd", e);
sender.SendError("unknown command "+program);
}
...
}
This is a example of a method in cmdimpl.
public void DoHelp(String []args) throws IOException
{
session.sender.Send(typeConverter.GetBatchInfo("",TypeConverter.FILETYPE.TEXTMSG, WorkerSession.commandmap.keySet().toString()));
}
So when I call DoCommand("help")
it says "unknown command." But when I call DoCommand("help a")
it works fine.
Any suspicions/suggestions to solve this problem?
EDIT
I added some codes to print stack trace but it doesn't work.
catch (NullPointerException e)
{
Log.e(TAG, "UNknown cmd"+program+".", e);
sender.SendError("unknown command "+program+"-"+Log.getStackTraceString(e));
}
sender.SendError
says
"unknown command help"
, not
unknown command help-java.lang.NullPointerException: ...
So it seems that there is something wrong in the program
.
EDIT 2
I suddenly came across one idea that CR LF/ CR / LF
issue can be a key point.
public void ExecuteCommands(String cmds)
{
workerThread.AddCommand(cmds.split("\n")); //this \n may be wrong?!
}