'Why getState() and getTransition doesn't work in unit test?
I have the class below, implemented by me. I want to test with Junit4 the methods. The test of get and set methods are okay (I didn't put them here) but when I try to test the methods getState and getTransition the output is :
java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because the return value of "se21_p2.Nba.nba.State.getStateName()" is null
public class NBA {
private final ArrayList<State> acceptStateList;
private final ArrayList<State> stateList;
private final ArrayList<Transition> transitionList;
private State initState;
/**
* Constructor of NBA
*/
public NBA(ArrayList<State> acceptStateList, ArrayList<State> stateList, ArrayList<Transition> transitionList, State initState) {
this.stateList = stateList;
if (!stateList.contains(initState)){
addState(initState);
}
this.acceptStateList = acceptStateList;
for (State state: acceptStateList){
if(!stateList.contains(state))
addState(state);
}
for (State state0: stateList) {
if (state0.getStateName() != null) {
for (State state1 : stateList) {
if (state1.getStateName() != null) {
String s0 = state0.getStateName();
String s1 = state1.getStateName();
if (stateList.contains(state0) && stateList.contains(state1)) {
if (s0.equals(s1))
removeState(state1);
}
}
}
}
}
this.transitionList = transitionList;
for (Transition t:transitionList) {
if(!stateList.contains(t.getStateFrom())){
addState(t.getStateFrom());
}
if (!stateList.contains(t.getStateTo())){
addState(t.getStateTo());
}
}
this.initState= initState;
}
/**
* Default constructor of NBA
*/
public NBA (){
this.stateList=new ArrayList<>();
this.acceptStateList= new ArrayList<>();
this.transitionList= new ArrayList<>();
this.initState= new State();
}
public State getState(String stateName) {
State state = new State();
for (int i = 0; i < stateList.size(); i++) {
if (stateList.get(i).getStateName().equals(stateName)) {
state=stateList.get(i);
}
}
return state;
}
public Transition getTransition(String id) {
Transition t = new Transition();
for (Transition trans : transitionList) {
if (trans.getId().equals(id)) {
t = trans;
}
}
return t;
}
}
State is another class that NBA needs to work. In particular this is the class with the method getStateName(): (there are also other methods for initial and accept but they work)
public class State {
private String stateName;
private boolean initial;
private boolean accept;
/**
* Constructor State1
*
* @param stateName name of state
*
*/
public State(String stateName) {
this.stateName = stateName;
this.initial = false;
this.accept = false;
}
/**
* Constructor State 2
*/
public State(String stateName,,boolean initial,boolean accept) {
this.stateName = stateName;
this.accept = true;
this.initial = true;
}
/*
*Constructor state 3
*/
public State(){}
/**
* Override equals method
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if(o==null) return false;
//if(getClass() != o.getClass()) return false;
if (!(o instanceof State)) return false;
State state = (State) o;
if (stateName==null){
if(state.stateName != null)
return false;
}else if(!stateName.equals(state.stateName))
return false;
return true;
}
//return stateName.equals(state.getStateName());
@Override
public int hashCode() {
return Objects.hash(stateName);
}
/**
* @return stateName name of State
*/
public String getStateName() {
return this.stateName;
}
/**
* set name of state
*/
public void setStateName(String stateName) {
this.stateName = stateName;
}
Is there something wrong? How can I initilize "statename" in the way it is not null?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
