1

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?!
}
KYHSGeekCode
  • 1,068
  • 2
  • 12
  • 30
  • Could you explain me how to improve my question or some reasons after downvoting? Thanks for your helps. – KYHSGeekCode Mar 24 '18 at 16:12
  • 3
    I suggest that you learn some debugging skills. These are among the most important skills to develop as an aspiring computer programming. To help you, read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for some tips. – Code-Apprentice Mar 25 '18 at 04:10
  • 2
    @Code-Apprentice Thanks I 'll read that article. – KYHSGeekCode Mar 25 '18 at 04:12
  • 2
    Printing "Unknown command" for NullPointerException is not a good idea. You should print "Unknown command" when you know the command is unknown. For NPE print the stack trace at least. – Piro Mar 25 '18 at 04:22
  • Error message says that method.invoke(cmdimpl, (Object) words); threw NPE. Purely. – KYHSGeekCode Mar 25 '18 at 05:42

2 Answers2

2

It will return a String[] with one element, which is the original string.

You can test it by:

String word="Hello";
String [] words=word.split(" ");
if (words == null) {
  System.out.print("NULL");
} else {
  System.out.println(words.length);
  System.out.println(words[0]);
}
mike
  • 373
  • 3
  • 14
  • Thank you mike and your example works fine. However I made some mistakes on deciding the title of my question. I clarified my title. Anyway +1 because it gave me partial solution – KYHSGeekCode Mar 25 '18 at 04:11
  • 2
    When catching an exception, normally we will printout the stack trace for diagnosis , you can do that by e.printStackTrace(), it will give you more information e.g which line got null pointer exception – mike Mar 25 '18 at 04:40
1

Thank you so much everybody! My actual problem derived from

 String.split("\n");

. When I changed it to

 String.split("\\r?\\n");

According to this question Split Java String by New Line,

The problem was gone.

Again thank you for your answers and comments :)

KYHSGeekCode
  • 1,068
  • 2
  • 12
  • 30