0

So I'm getting an error with my code and I can't find where it's at, and after so long I'm reaching out. I'm trying to create a conversation between two users which I have created before, all fine on this part. The problem is when I'm going to create a new conversation, I'm getting a NullPointerException. I think the code is right and I have tried to do it in other ways, but I am always getting the same error and it doesn't create the conversation I want to create the conversation between the two and then retrieve it or retrieve every conversation that one user has with other

The error is the following:

Exception in thread "main" java.lang.NullPointerException

The lines are marked in the code.

Main Class

private static void INC(Scanner in, Chat ch){

    int iduser1=0;
    int iduser2=0;
    int transvalue=0;


    System.out.print("Insert ID User 1:");

    iduser1=in.nextInt();

    if(iduser1==ch.getUserNameID(iduser1)){
        System.out.print("Found" + ch.getUserName(iduser1));
    }
    in.nextLine();

    System.out.print("Insert ID User 2:");
    iduser2=in.nextInt();

    if(iduser2==ch.getUserNameID(iduser2)){
        System.out.print("Found"+ch.getUserName(iduser2));
    }
    in.nextLine();


    do{    
        System.out.print("Factor de Translacao:");
        transvalue=in.nextInt();
        in.nextLine();
        if (transvalue<1 || transvalue>26){
            System.out.println("Factor between 0 and 26");
        }

    }while(transvalue<1 || transvalue>26);

    if(!ch.hasConversation(iduser1)){ /*------------------------Here*/

        ch.addConversation(iduser1,iduser2,transvalue);

        System.out.println("Sucess " +ch.getConversation(iduser1)+ " e " +ch.getConversation(iduser2)); 
    }else{
        System.out.println("Conversation" +ch.getConversation(iduser1)+ " e " + ch.getConversation(iduser2)+ "Already exists");
    }

}

Conversation

public class Conversation {

    private int ids1=0;
    private int ids2=0;
    private int transvalue=0;

    public Conversation(int iduser1, int iduser2, int transvalue){
        this.ids1=iduser1;
        this.ids2=iduser2;
        this.transvalue=transvalue;
    }

    public int getIds1() {
        return ids1;
    }

    public void setIds1(int ids1) {
        this.ids1 = ids1;
    }

    public int getIds2() {
        return ids2;
    }

    public void setIds2(int ids2) {
        this.ids2 = ids2;
    }

    public int getTransvalue() {
        return transvalue;
    }

    public void setTransvalue(int transvalue) {
        this.transvalue = transvalue;
    }

}

Chat

public Chat(){
    counter = 0;
    current=-1;
    users = new Users[MaxUsers]; 
    conversation = new Conversation[MaxConversation];    

}

public void addConversation(int iduser1, int iduser2, int transvalue){
    if(counter == conversation.length)
        resizeConversation();
    conversation[counter++] = new Conversation(iduser1,iduser2,transvalue);
}

public int getConversation(int iduser1){
    return conversation[searchIndexConversation(iduser1)].getIds1() ;
}

public boolean hasConversation(int iduser1){
    return ((searchIndexConversation(iduser1) >= 0 )); /*------------------------Here*/
}

private int searchIndexConversation(int iduser1){
    int i = 0;
    int result= -1;
    boolean found1= false;
    while((i< counter) && (!found1))
        if(conversation[i].getIds1()==iduser1)  /*------------------------Here*/
            found1 = true;
        else i++;

    if(found1) result = i;
        return result;
}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42

0 Answers0