简体   繁体   English

我收到一个nullpointerexception,我找不到原因

[英]I'm getting a nullpointerexception and I can't find why

The program is supposed to make a non-deterministic automaton in the form of a tree holding information of both keys and the next pieces of automaton. 该程序应该以树的形式制作一个不确定的自动机,其中包含两个键和下一个自动机的信息。 The automaton is to find patterns on a text (more comlpicated than that, but that should suffice for my question as I haven't gotten to the next part yet). 自动机是在文本上查找模式(比这更复杂,但这应该足以解决我的问题,因为我还没有到达下一部分)。

I don't understand how but when I do a simple pattern for the automaton to build up ("AA" is my pattern) and I get that the "sgte"(next) is becoming null while the array's length (saved as N [kN]) is not 0. And I cannot find why :( 我不知道如何,但是当我为自动机建立简单的模式(“ AA”是我的模式)时,我发现“ sgte”(下一个)在数组长度(保存为N [ kN])不是0。而且我找不到原因:(

Here's the code: 这是代码:

public class AFND {

    private boolean estado; // true o false dependiendo de si es final o inicial respectivamente.
    private AFND sgte[];  //arreglo con todos los posibles estados siguientes
    private int N;  //cantidad de posibles estados siguientes
    private char key[]; //key[i] es el caracter con el que se accede a sgte[i] { '-' = e  }
    private int q; //denominador de estado
    private String alfa = "aaabcdefghijklmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZZZ"; //para ahorrarnos errores revisamos indexOf desde la posicion 2 donde sea necesario

    public AFND() {
        estado = true;
        sgte = null;
        N = 0;
        key = null;
        q = 0;
    }

    public AFND(int q) {
        estado = true;
        sgte = null;
        N = 0;
        key = null;
        this.q = q;
    }

    public AFND(String s) {
        if (check(s) == false) {
            U.println("Patrón Invalido.");
            System.exit(0);
        }
        AFND k = Construccion(s, 0);
        estado = k.estado;
        int i = 0;
        sgte = new AFND[k.N];
        key = new char[k.N];
        while (i < k.N) {
            sgte[i] = k.sgte[i];
            key[i] = k.key[i];
            i++;
        }
        N = k.N;
        q = k.q;
    }

    public AFND Construccion(String s, int l) {
        if (s.length() == 0) {
            return new AFND();
        }
        AFND k = new AFND(l);
        k.estado = false;
        k.q = l;
        if (s.charAt(0) == '[') {
            AFND sgte[] = new AFND[5];
            char key[] = new char[5];
            String h = s.substring(1, s.indexOf(']'));
            int i = 0;
            int j = 0;
            int x;
            String L[] = new String[5];
            while (i < 5) {
                L[i] = "";
                while (j < h.length()) {
                    x = alfa.substring(2).indexOf(h.charAt(j));
                    L[i] += alfa.charAt(x + i - 2);
                    j++;
                }
                j = 0;
                L[i] += s.substring(s.indexOf(']') + 1);
                sgte[i] = Construccion(L[i].substring(1), l);
                l++;
                key[i] = L[i].charAt(0);
                i++;
            }
            k.N = 5;
        } else {
            AFND sgte[] = new AFND[1];
            char key[] = new char[1];
            key[0] = s.charAt(0);
            if (s.length() > 1) {
                sgte[0] = Construccion(s.substring(1), l);
            } else {
                sgte[0] = new AFND(l);
            }
            k.N = 1;
            l++;
        }
        int o = 0;
        k.sgte = new AFND[k.N];
        k.key = new char[k.N];
        while (o < k.N) {
            k.sgte[o] = sgte[o];
            k.key[o] = key[o];
            o++;
        }
        return k;
    }

    public boolean estado() {
        return estado;
    }

    public AFND[] sgte() {
        return sgte;
    }

    public int ancho() {
        return N;
    }

    public char[] key() {
        return key;
    }

    public int num() {
        return q;
    }

    public boolean check(String s) {
        int i = 0;
        int j = 0;
        while (i < s.length()) {
            if (j == 0) {
                if (s.charAt(i) == '[') {
                    j = 1;
                } else if (s.charAt(i) == ']') {
                    return false;
                } else if (!Character.isLetter(s.charAt(i))) {
                    return false;
                }
            } else {
                if (s.charAt(i) == ']') {
                    j = 0;
                } else if (s.charAt(i) == '[') {
                    return false;
                } else if (!Character.isLetter(s.charAt(i))) {
                    return false;
                }
            }
            i++;
        }
        return true;
    }
}

And here's the running program: 这是正在运行的程序:

import java.io.IOException;

public class Tarea2 {

static public void main(String[] args) throws IOException{
    String m=U.readLine("Ingresar Patrón: ");
    AFND patron=new AFND(m);
    U.println("AFND Patron Desplazado: ");
    U.println("");
    U.println("<!--Deus ex Machina-->");
    U.println("<structure>");
    U.println("<type>");
    U.println("fa");
    U.println("</type>");
    U.println("<automaton>");
    imprimirEstados(patron);
    imprimirTransiciones(patron);
    U.println("</automaton>");
    U.println("</structure>");
}

static public void imprimirEstados(AFND m){
    U.println("<state id="+m.num()+" name=q"+m.num()+">");
    U.println("<x>");
    U.println("0.0");
    U.println("</x>");
    U.println("<y>");
    U.println("0.0");
    U.println("</y>");
    U.println("</state>");
    int i=0;
    if(m.ancho()!=0){
        AFND[] s=m.sgte();
        while(i<m.ancho()){
            imprimirEstados(s[i]);
            i++;
            }
        }
    }

static public void imprimirTransiciones(AFND m){
    if(m.ancho()!=0){
        int i=0;
        while(i<m.ancho()){
            U.println("<transition>");
            U.println("<from>");
            U.println(m.num());
            U.println("</from>");
            U.println("<to>");
            U.println(m.sgte()[i].num());
            U.println("</to>");
            U.println("<read>");
            U.println(m.key()[i]);
            U.println("</read>");
            imprimirTransiciones(m.sgte()[i]);
            i++;
        }
    }
}

}

Please help :( 请帮忙 :(

Here's the exception: 这是例外:

Exception in thread "main" java.lang.NullPointerException
at tarea2cs.AFND.Construccion(AFND.java:104)
at tarea2cs.AFND.Construccion(AFND.java:95)
at tarea2cs.AFND.<init>(AFND.java:48)
at tarea2cs.Tarea2.main(Tarea2.java:9)

The 104 is this part: 104是这一部分:

        while(o<k.N){ 
        k.sgte[o]=sgte[o];    <=
        k.key[o]=key[o];
        o++;
    }

I could just add the "if(sgte!=null)" but that would not solve the issue that it's becoming a null when it shouldnt :( 我可以只添加“ if(sgte!= null)”,但这不能解决当它不应该:(

I think it is a shadowing problem. 我认为这是一个阴影问题。 You have an instance variable sgte but in a couple of places you declare local variables with the same name; 您有一个实例变量sgte但是在几个地方用相同的名称声明了局部变量。 eg 例如

   AFND sgte[] = new AFND[5];

This looks like a mistake ... and my guess it should be: 这看起来像是个错误...我想应该是:

   sgte = new AFND[5];

(You make the same mistake in at least one other place.) (您在至少另一个地方犯了同样的错误。)


I should also comment that the code as written has a serious maintainability issue. 我还应该评论说,编写的代码存在严重的可维护性问题。 Your pervasive use of one letter variable names and abbreviations (like sgte and AFND ) without any explanatory comments will make it hard for someone else to figure out what this application is about, let alone how it is supposed to work. 您普遍使用一个字母的变量名和缩写(如sgteAFND )而没有任何解释性注释,这将使其他人很难弄清此应用程序的用途,更不用说它应该如何工作了。

You've created an array but you've not created objects. 您已经创建了数组,但尚未创建对象。

 sgte = new AFND[k.N];
 for(int i=0;i<k.N;i++){
   sgte[i]=new AFND();
 }

you only created sgte array but never initialized it. 您仅创建了sgte数组,但从未对其进行初始化。

private AFND sgte[];  //arreglo con todos los posibles estados siguientes


   public AFND(int q) {
    estado = true;
    sgte = null;
    N = 0;
    key = null;
    this.q = q;
}

and you are trying to get the element at 0 index here 并且您正在尝试将元素的索引设置为0

   while (o < k.N) {
        k.sgte[o] = sgte[o];   //NPE
        k.key[o] = key[o];
        o++;
    }

and you are also creating a local variable with the same name(its not an issue anyway). 并且您还在创建一个具有相同名称的局部变量(无论如何这都不是问题)。 but, to diffrentiate use this.sgte for instance variable. 但是,要进行区分,请使用this.sgte作为实例变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不知道为什么我会收到NullPointerException错误 - Not sure why I'm getting a NullPointerException error 我看不到为什么我得到了NullPointerException - I'm unable to see why I'm getting a NullPointerException 我不知道为什么我得到StackOverFlowError - I can't figure out why I'm getting StackOverFlowError 我在defaultWriteObject处收到NotActiveException,我不知道为什么 - I'm getting a NotActiveException at defaultWriteObject and I can't tell why 我无法弄清楚为什么在通过io运行时会收到java.lang.Nullpointerexception错误消息 - I can't work out why I'm getting a java.lang.Nullpointerexception error message when i run through my io 为什么要在扩展jna中的Structure的类构造函数中获取NullPointerException? - Why i m getting the NullPointerException in a class constructor extending Structure in jna? 无法弄清楚为什么我得到了StringIndexOutOfBoundsException - Can't figure out why I'm getting a StringIndexOutOfBoundsException 无法理解为什么我得到一个空指针错误 - Can't understand why i'm getting a null pointer error 我不断收到 NullPointerException,但找不到我犯错的地方 - I keep getting a NullPointerException but can't find where I made the mistake 我想知道为什么会收到java.lang.NullPointerException - I'm wondering why I'm getting a java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM