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 [k.N]) is not 0. And I cannot find why 🙁
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:
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 🙁
I think it is a shadowing problem. You have an instance variable
sgtebut in a couple of places you declare local variables with the same name; e.g.This looks like a mistake … and my guess it should be:
(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
sgteandAFND) 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.