Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In that first line, you declare the variables <code>state0</code>, <code>state1</code>, <code>curr_state</code>, <code>init_state</code> and <code>temp</code> as being variables of type <code>DFA_State</code>. However, that only declares them, they are not yet initialized. The next few lines are all okay. Second line creates a state without anything in it and assigns it to <code>state0</code>, so does the third line for <code>state1</code>. Fourth line overwrites your previous <code>state0</code> assignment with a new <code>DFA_State</code> that has actual contents. Fifth line creates a <code>DFA_State</code> as a copy of <code>state0</code> and assigns it to <code>init_state</code>.</p> <p>Assuming there's nothing in between this and the first line of your second code block, now you'll get a problem. You're assigning <code>temp</code> with a new <code>DFA_State</code> that uses a copy-constructor with an argument relying on <code>curr_state</code>. But at that point, that variable hasn't been initialized yet. Just because it was declared doesn't mean it has somehow already been structured in memory. When you call <code>nextState</code> on it, there's simply no variable to resolve this to. Don't expect to get something like a pointer that will eventually point to a part of what you put in <code>curr_state</code>.</p> <p>I'm just guessing, but from your code style I'd say you have a background in C or C++. Look into the differences between those languages and Java. If possible, I'd also advise you to make your <code>DFA_State</code> class immutable, since this is more reliable and will avoid mistakes. That means getting rid of the no-args constructor. Here's a reworking of it (not actually compiled, might contain errors):</p> <pre><code>package foundations.of.computing; /** * * @author Kayotic */ class DFA_State { private final String state; private final DFA_State on_0; private final DFA_State on_1; private final boolean isAccepting; //private DFA_State dummy; public DFA_State(DFA_State arg) { //this(arg.is_accepting(), arg.on0(), arg.on1()); state = arg.get_name(); isAccepting = arg.is_accepting(); on_0 = arg.on0(); on_1 = arg.on1(); } public DFA_State(String name, Boolean accepting, DFA_State on0, DFA_State on1) { state = name; isAccepting = accepting; on_0 = on0; on_1 = on1; } public String get_name(){ return state; } public Boolean is_accepting() { return isAccepting; } public DFA_State on0() { return on_0; } public DFA_State on1() { return on_1; } public DFA_State nextState(char i) { if (i == '0') { return on0(); } else if (i == '1') { return on1(); } else { System.out.println("Error with input"); return null; } } } </code></pre> <p>Even if you can't make the instance variables final, it's best to at least make them private, since you already have methods for getting them.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload