2

For some reasons i get null pointer errors in the code down. I'm working with a book of examples and trying to make smth mine, but seems like theres a change in java or mistake in book. I'm making a Chatbot(sort of AI) and can't get it working in java. When i run the program it lets me only write Hi and then it replays. When i try to give him another word like engi he acts like i always say hi. If i restart and write first engi, then the error pops up, null pointer for some reason. Can anyone help?

import java.io.IOException;
import java.util.Scanner;

import org.apache.http.client.ClientProtocolException;

import com.google.gson.*;

public class engibot 
{
    JsonObject context;

    public static void main (String[] args) {
        engibot c= new engibot();
        Scanner scanner= new Scanner(System.in);
        String userUtterance;

        do {
            System.out.print("User:");
            userUtterance=scanner.nextLine();
            //end conversation
            if (userUtterance.equals("QUIT")) {break;}

            JsonObject userInput=new JsonObject();
            userInput.add("userUtterance", new JsonPrimitive(userUtterance));
            JsonObject botOutput = c.process(userInput);
            String botUtterance = "";
            if(botOutput !=null && botOutput.has("botUtterance")) {
                botUtterance = botOutput.get("botUtterance").getAsString();
            }
            System.out.println("Bot:" + botUtterance);
            }while(true);

        scanner.close();
        }
    public engibot() {
        context =new JsonObject();
        }
    public JsonObject process(JsonObject userInput) {
    //step 1: process user input

        JsonObject userAction = processUserInput(userInput);

    //step 2: update context
        updateContext(userAction);

    //step 3: identify bot intent
        identifyBotIntent();

    //step 4: structure output
        JsonObject out = getBotOutput();

    return out;
    }
public JsonObject processUserInput(JsonObject userInput) {
    String userUtterance = null;
    JsonObject userAction = new JsonObject();

    //default case
    userAction.add("userIntent", new JsonPrimitive(""));

    if(userInput.has("userUtterance")) {
        userUtterance= userInput.get("userUtterance").getAsString();
        userUtterance=userUtterance.replaceAll("%2C", ",");
    }
    if (userUtterance.matches("(hi)|(hi | hello)( there)?")) {
        userAction.add("userIntent", new JsonPrimitive("greet"));
    }
    else if (userUtterance.matches("(thanks)|(thank you)")) {
        userAction.add("userIntent", new JsonPrimitive("thank"));
    }
    else if(userUtterance.matches("(engi) | (engagment)")) {
        userAction.add("userIntent", new JsonPrimitive("request_engi"));
    }
    else {

        //contextual processing

        String currentTask = context.get("currentTask").getAsString();
        String botIntent = context.get("botIntent").getAsString();
        if(currentTask.equals("requestEngi") && botIntent.equals("requestUserName")) {
            userAction.add("userIntent", new JsonPrimitive("inform_name"));
            userAction.add("userName", new JsonPrimitive(userUtterance));
        }
    }

    return userAction;
}
public void updateContext(JsonObject userAction) {
    //copy userIntent
    context.add("userIntent", userAction.get("userIntent"));

    String userIntent = context.get("userIntent").getAsString();
    if(userIntent.equals("greet")) {
        context.add("currentTask", new JsonPrimitive("greetUser"));
    }else if (userIntent.equals("request_engi")) {
        context.add("currentTask", new JsonPrimitive("requestEngi"));
    }else if (userIntent.equals("infrom_city")) {
        String userName = userAction.get("userName").getAsString();
        Engi userInfo = DB.getUserInfo(userName);
        if(!userInfo.get("userName").isJsonNull()) {
            context.add("placeOfWeather", userInfo.get("cityCode"));
            context.add("placeName", userInfo.get("cityName"));
        }
    }else if (userIntent.equals("thank")) {
        context.add("currenTask", new JsonPrimitive("thankUser"));
    }
}
public void identifyBotIntent() {
    String currentTask = context.get("currentTask").getAsString();
    if(currentTask.equals("greetUser")) {
        context.add("botIntent", new JsonPrimitive("greetUser"));
    }else if(currentTask.equals("thankUser")) {
        context.add("botIntent", new JsonPrimitive("thankUser"));
    }else if (currentTask.equals("requestEngi")) {
        if (context.get("userName").getAsString().equals("unknown")) {
            context.add("botIntent", new JsonPrimitive("requestUserName"));
        }
        else {
            Integer time = -1;
            if (context.get("").getAsString().equals("current")) {
                time=0;
            }
            Engi userReport =null;
            userReport = DB.getUserInfo(
             context.get("userName").getAsString());
            if(userReport !=null) {
                context.add("userReport", new JsonPrimitive("userReport"));
                context.add("botIntent", new JsonPrimitive("informUser"));
            }
        }
    }else {
        context.add("botIntent", null);
    }
}
public JsonObject getBotOutput() {
    JsonObject out=new JsonObject();
    String botIntent = context.get("botIntent").getAsString();
    String botUtterance="";
    if(botIntent.equals("greetUser")) {
        botUtterance= "Hi there! I am EngiMan, your engagement bot! " + "What would you like to do? Engage someone or something else?";
        }else if(botIntent.equals("thankUser")) {
            botUtterance="Thanks for talking to me! Have a great day!!";
        }else if (botIntent.equals("requestName")) {
            botUtterance="Ok. What's his name?";
        }else if(botIntent.equals("informUser")) {
            String userDescription= getUserDescription(context.get("userName").getAsString());
            String engiIndex= getEngiIndex();
            botUtterance = "Ok. "+"Engagment index of "+userDescription+" is"+engiIndex+".";
        }
    out.add("botIntent", context.get("botIntent"));
    out.add("botUtterance", new JsonPrimitive(botUtterance));
    return out;
}
private String getEngiIndex() {
    return context.get("engiIndex").getAsString();
}
private String getUserDescription(String userName) {
    if (userName.equals("current")) {
        return "current";
    }
    return null;
}
}






    import com.google.gson.JsonElement;

public class Engi {
private String Name;
private String LastName;
private int Age;
private double EngiIndex;
private String Firm;
private String Team;

public JsonElement get(String string) {
    // TODO Auto-generated method stub
    return null;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getLastName() {
    return LastName;
}

public void setLastName(String lasteName) {
    LastName = lasteName;
}

public int getAge() {
    return Age;
}

public void setAge(int age) {
    Age = age;
}

public double getEngiIndex() {
    return EngiIndex;
}

public void setEngiIndex(double engiIndex) {
    EngiIndex = engiIndex;
}

public String getFirm() {
    return Firm;
}

public void setFirm(String firm) {
    Firm = firm;
}

public String getTeam() {
    return Team;
}

public void setTeam(String team) {
    Team = team;
}
}
ShaggyZG
  • 39
  • 7
  • You are closing the scanner in your do-while loop. While that may not be the nly problem it certainly will lead to errors when you try to make an inut in the second iteration of the loop. – OH GOD SPIDERS Sep 25 '18 at 09:10
  • mistake while copying, u see there's one outside the loop also. That's not the problem – ShaggyZG Sep 25 '18 at 09:11
  • What do you mean "Mistake while copying"? If the code you posted here is not the code that causes problems then it will be very hard to help. – OH GOD SPIDERS Sep 25 '18 at 09:13
  • Yeah i was editing my code in my editor, not here -.- – ShaggyZG Sep 25 '18 at 09:19
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Sep 25 '18 at 16:32
  • i tried to separate the code into two, but it keeps it all in one. because public class engi goes into antoher file. But still, just copy the code and run it, then u'll get the same error as I did. If anynone can help would be cool because I'm still suck on that matter – ShaggyZG Sep 27 '18 at 07:48

0 Answers0