0

I consider this question would be much specific and clearer.

Here is my code which do simple autocompletion operation (sort of).

public class JLineExample {
    public static void main(String[] args) throws IOException {
        String line;
        ConsoleReader console = new ConsoleReader();
        console.setPrompt("> ");

        // Quit
        Completer quit = new StringsCompleter("quit");

        // Group #1
        Completer start = new StringsCompleter("start");
        Completer stop = new StringsCompleter("stop");

        // Group #2
        Completer propetry = new StringsCompleter("property");
        Completer hide = new StringsCompleter("hide");

        // Joint groups
        Completer executionGroup = new ArgumentCompleter(start, stop);
        Completer propertyGroup = new ArgumentCompleter(propetry, hide);
        Completer jointGroups = new ArgumentCompleter(executionGroup, propertyGroup);

        // Aggregator
        Completer aggregated = new AggregateCompleter(quit, jointGroups);

        console.addCompleter(aggregated);
        while ((line = console.readLine("> ")) != null) {
            if ("quit".equals(line.trim())) exit(1);
        }
    }
}

The problem is combining completers in one group. I get

Exception in thread "main" java.lang.NullPointerException
        at jline.console.completer.ArgumentCompleter$AbstractArgumentDelimiter.delimit(ArgumentCompleter.java:283)
        at jline.console.completer.ArgumentCompleter.complete(ArgumentCompleter.java:116)
        at jline.console.completer.ArgumentCompleter.complete(ArgumentCompleter.java:152)
        at jline.console.completer.AggregateCompleter$Completion.complete(AggregateCompleter.java:121)
        at jline.console.completer.AggregateCompleter.complete(AggregateCompleter.java:80)
        at jline.console.ConsoleReader.complete(ConsoleReader.java:3261)
        at jline.console.ConsoleReader.readLine(ConsoleReader.java:2621)
        at jline.console.ConsoleReader.readLine(ConsoleReader.java:2269)
        at com.test.jline.JLineExample.main(JLineExample.java:52)

when press a TAB key.

Any ideas how can I fix this behavoir?

Alex
  • 2,091
  • 6
  • 31
  • 49

1 Answers1

1

you didn't set the completers into console completer this is why you are getting null pointer exception.

either to do:

Vector<String> wc = new Vector<String>();

wc.add("start");
wc.add("stop");
wc.add("load");
wc.add("clear");

console.addCompleter(
    new StringsCompleter(wc)
); 

or you can construct it from external file and load it into StringsCompleter constructor or stream it into it like this :

console.addCompleter(
    new StringsCompleter(
        IOUtils.readLines(new GZIPInputStream(ConsoleDemo.class.getResourceAsStream("wordlist.txt.gz")))
    )
);

this is just an example taken from https://jeszysblog.wordpress.com/2012/04/14/readline-style-command-line-editing-with-jline/

the words file is loaded by Apache Commons IO library but it can also be done by java I/O since the token's file isn't that huge no problem if fully loaded in memory, otherwise you should split your tokens into separated sets and load each one the time it is required.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49