Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8636683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:17:22+00:00 2026-06-12T10:17:22+00:00

The program is supposed to make a non-deterministic automaton in the form of a

  • 0

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 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:17:23+00:00Added an answer on June 12, 2026 at 10:17 am

    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; e.g.

       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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am supposed to write a program in JavaScript to find all the anagrams
I am supposed to make a program that only has 2 methods. One method
The program is supposed to take a file, say data.dat, filled with a list
My program is supposed to read a text document with a bunch of numbers
This program is supposed to determine how many units are stored in the value
So my program is supposed to recursively go through a directory and then for
I'm supposed to program in Python, and I've only used Python for 3 weeks.
I have a program that is supposed to interact with a web server and
I have a small program that's supposed to sample some value from a device
I am currently making a program which is supposed to prompt the user to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.