-1

i'm building some program. apache guacamole (VNC tool) makes log in console. (it can be tweak for make log in file, but i won't) so, my program reads log in console and determine which user(id) accessed(connected) or leave(disconnected).

but it gives me "java lang nullpointer exception at line 19". so here is my code.

import java.util.*;
import java.lang.*;
import java.io.*;

class id
{
    public static void main (String[] args) throws java.lang.Exception
    {
    String connect    = "connect";
    String disconnect = "disconnect";
    String id;
    int strindex, endindex; //mark start, end index

    while(true){

        String phrase = System.console().readLine();
        if(phrase.equals(""))
            continue;

        if(phrase.contains(disconnect)){
            if( (strindex=phrase.indexOf("- User ")) > -1 )
            strindex = strindex + 8;        
        endindex = phrase.indexOf('"', strindex);
        id = phrase.substring(strindex, endindex);

        System.out.println(id);
        System.out.println("disco\n");
        //dockerfile_logout+pause;
        }//islogout

        else if(phrase.contains(connect)){
            if( (strindex=phrase.indexOf("- User ")) > -1 )
            strindex = strindex + 8;        
        endindex = phrase.indexOf('"', strindex);
        id = phrase.substring(strindex, endindex);

        System.out.println(id);
        System.out.println("connected\n");
        //dockerfile_login+start!   
    }//islogin

    }//while
}

}

HyunsooKim
  • 103
  • 1
  • 1
  • 5

1 Answers1

2

Invocations to System.console() might return null, if not invoked within an interactive shell.

If you're running this from an IDE, String phrase = System.console().readLine(); will throw NullPointerException.

See API here and here.

Mena
  • 47,782
  • 11
  • 87
  • 106