在两个用户之间创建对话

时间:2015-12-13 19:45:34

标签: java nullpointerexception

所以我的代码出错了,我找不到它的位置,经过这么长时间我才伸出手来。 我正在尝试在之前创建的两个用户之间创建一个对话,这一切都很好。 问题是,当我要创建一个新的对话时,我得到一个NullPointerException。 我认为代码是正确的,我试图以其他方式做到这一点,但我总是得到相同的错误,它不会创建对话我想在两者之间创建对话,然后检索它或检索每个对话一个用户拥有其他

错误如下:

Exception in thread "main" java.lang.NullPointerException

这些行标记在代码中。

主要类

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");
    }

}

会话

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;
    }

}

聊天

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;
}

0 个答案:

没有答案