I’m doing a project for studying. There is an agent with 2 variables that take the value True or False using the randomTrue() function.
The construction of the project does not reveal errors. At startup, it throws an error when executing a discrete event:NullPointerException. Not a single agent was entered into the system. On source 0.
I tried to change agents, changed randomTrue() to randomFalse(), with no results.
What are the options?
NullPointerException
java.lang.NullPointerException
at com.anylogic.engine.Utilities.randomTrue(Unknown Source)
at bank.Document.<init>(Document.java:78)
at bank.Main._doc_enter_newEntity_xjal(Main.java:978)
at bank.Main.access$0(Main.java:975)
at bank.Main$1.newEntity(Main.java:312)
at com.anylogic.libraries.processmodeling.Source.inject(Unknown Source)
at com.anylogic.libraries.processmodeling.Source.executeActionOf(Unknown Source)
at com.anylogic.engine.EventTimeout.execute(Unknown Source)
at com.anylogic.engine.Engine.i(Unknown Source)
at com.anylogic.engine.Engine.if(Unknown Source)
at com.anylogic.engine.Engine.e(Unknown Source)
at com.anylogic.engine.Engine$n.run(Unknown Source)
package bank;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Currency;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.Stack;
import java.util.Timer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.awt.Color;
import java.awt.Font;
import com.anylogic.engine.connectivity.ResultSet;
import com.anylogic.engine.connectivity.Statement;
import com.anylogic.engine.elements.*;
import com.anylogic.engine.markup.Network;
import com.anylogic.engine.Position;
import com.anylogic.engine.markup.PedFlowStatistics;
import com.anylogic.engine.markup.DensityMap;
import static java.lang.Math.*;
import static com.anylogic.engine.UtilitiesArray.*;
import static com.anylogic.engine.UtilitiesCollection.*;
import static com.anylogic.engine.presentation.UtilitiesColor.*;
import static com.anylogic.engine.HyperArray.*;
import com.anylogic.engine.*;
import com.anylogic.engine.analysis.*;
import com.anylogic.engine.connectivity.*;
import com.anylogic.engine.database.*;
import com.anylogic.engine.gis.*;
import com.anylogic.engine.markup.*;
import com.anylogic.engine.routing.*;
import com.anylogic.engine.presentation.*;
import com.anylogic.engine.gui.*;
import com.anylogic.libraries.modules.markup_descriptors.*;
import com.anylogic.libraries.processmodeling.*;
import com.anylogic.libraries.material_handling.*;
import java.awt.geom.Arc2D;
public class Document extends Agent
{
// Parameters
// Plain Variables
public final
boolean
is_simple =
randomTrue(0.6)
;
public final
boolean
has_critical_errors =
randomTrue(0.1)
;
@AnyLogicInternalCodegenAPI
private static Map<String, IElementDescriptor> elementDesciptors_xjal = createElementDescriptors( Document.class );
@AnyLogicInternalCodegenAPI
@Override
public Map<String, IElementDescriptor> getElementDesciptors() {
return elementDesciptors_xjal;
}
@AnyLogicCustomProposalPriority(type = AnyLogicCustomProposalPriority.Type.STATIC_ELEMENT)
public static final Scale scale = new Scale( 10.0 );
@Override
public Scale getScale() {
return scale;
}
/** Internal constant, shouldn't be accessed by user */
@AnyLogicInternalCodegenAPI
protected static final int _STATECHART_COUNT_xjal = 0;
// View areas
public ViewArea _origin_VA = new ViewArea( this, "[Origin]", 0, 0, 1500.0, 600.0 );
@Override
@AnyLogicInternalCodegenAPI
public int getViewAreas(Map<String, ViewArea> _output) {
if ( _output != null ) {
_output.put( "_origin_VA", this._origin_VA );
}
return 1 + super.getViewAreas( _output );
}
@AnyLogicInternalCodegenAPI
protected static final Pair<String, Color>[] _person_customColors_xjal = new Pair[] {
new Pair<String, Color>( "Material__3__Surf", null ),
new Pair<String, Color>( "Material__5__Surf", null ),
new Pair<String, Color>( "Material__6__Surf", null ),
new Pair<String, Color>( "Material__2__Surf", null ),
new Pair<String, Color>( "Material__4__Surf", null ),
new Pair<String, Color>( "Material__1__Surf", null ),
};
@AnyLogicInternalCodegenAPI
protected static final int _person = 1;
/** Internal constant, shouldn't be accessed by user */
@AnyLogicInternalCodegenAPI
protected static final int _SHAPE_NEXT_ID_xjal = 2;
@AnyLogicInternalCodegenAPI
public boolean isPublicPresentationDefined() {
return true;
}
@AnyLogicInternalCodegenAPI
public boolean isEmbeddedAgentPresentationVisible( Agent _a ) {
return super.isEmbeddedAgentPresentationVisible( _a );
}
@AnyLogicInternalCodegenAPI
private void _initialize_level_xjal() {
level.addAll(person);
}
protected Shape3DObject person;
protected com.anylogic.engine.markup.Level level;
private com.anylogic.engine.markup.Level[] _getLevels_xjal;
@Override
public com.anylogic.engine.markup.Level[] getLevels() {
return _getLevels_xjal;
}
@AnyLogicInternalCodegenAPI
private void _createPersistentElementsBP0_xjal() {
person = new Shape3DObject(
Document.this, SHAPE_DRAW_2D3D, true, 0.0, 0.0, 0.0, 0.0,
1.0, true, "/bank/",
"3d/person.dae", OBJECT_3D_YZX_AXIS_ORDER, OBJECT_3D_INTERNAL_LIGHTING_OFF, false, -5.0, -4.0,
7.0, 7.0, null, _person_customColors_xjal );
}
@AnyLogicInternalCodegenAPI
private void _createPersistentElementsAP0_xjal() {
}
@AnyLogicInternalCodegenAPI
private void _createPersistentElementsBS0_xjal() {
}
// Static initialization of persistent elements
private void instantiatePersistentElements_xjal() {
level = new com.anylogic.engine.markup.Level(this, "level", SHAPE_DRAW_2D3D, 0.0, true, true);
_getLevels_xjal = new com.anylogic.engine.markup.Level[] {
level };
_createPersistentElementsBP0_xjal();
}
protected ShapeTopLevelPresentationGroup presentation;
protected ShapeModelElementsGroup icon;
@Override
@AnyLogicInternalCodegenAPI
public ShapeTopLevelPresentationGroup getPresentationShape() {
return presentation;
}
@Override
@AnyLogicInternalCodegenAPI
public ShapeModelElementsGroup getModelElementsShape() {
return icon;
}
/**
* Constructor
*/
public Document( Engine engine, Agent owner, AgentList<? extends Document> ownerPopulation ) {
super( engine, owner, ownerPopulation );
instantiateBaseStructureThis_xjal();
}
@AnyLogicInternalCodegenAPI
public void onOwnerChanged_xjal() {
super.onOwnerChanged_xjal();
setupReferences_xjal();
}
@AnyLogicInternalCodegenAPI
public void instantiateBaseStructure_xjal() {
super.instantiateBaseStructure_xjal();
instantiateBaseStructureThis_xjal();
}
@AnyLogicInternalCodegenAPI
private void instantiateBaseStructureThis_xjal() {
instantiatePersistentElements_xjal();
setupReferences_xjal();
}
@AnyLogicInternalCodegenAPI
private void setupReferences_xjal() {
}
/**
* Simple constructor. Please add created agent to some population by calling goToPopulation() function
*/
public Document() {
}
@Override
@AnyLogicInternalCodegenAPI
public void doCreate() {
super.doCreate();
// Assigning initial values for plain variables
setupPlainVariables_Document_xjal();
// Dynamic initialization of persistent elements
_createPersistentElementsAP0_xjal();
_initialize_level_xjal();
level.initialize();
presentation = new ShapeTopLevelPresentationGroup( Document.this, true, 0, 0, 0, 0 , level );
icon = new ShapeModelElementsGroup( Document.this, getElementProperty( "bank.Document.icon", IElementDescriptor.MODEL_ELEMENT_DESCRIPTORS ) );
icon.setIconOffsets( 0.0, 0.0 );
// Port connectors with non-replicated objects
// Creating replicated embedded objects
setupInitialConditions_xjal( Document.class );
// Dynamic initialization of persistent elements
_createPersistentElementsBS0_xjal();
}
@Override
@AnyLogicInternalCodegenAPI
public void doStart() {
super.doStart();
}
/**
* Assigning initial values for plain variables<br>
* <em>This method isn't designed to be called by user and may be removed in future releases.</em>
*/
@AnyLogicInternalCodegenAPI
public void setupPlainVariables_xjal() {
setupPlainVariables_Document_xjal();
}
/**
* Assigning initial values for plain variables<br>
* <em>This method isn't designed to be called by user and may be removed in future releases.</em>
*/
@AnyLogicInternalCodegenAPI
private void setupPlainVariables_Document_xjal() {
}
// User API -----------------------------------------------------
@AnyLogicInternalCodegenAPI
public static LinkToAgentAnimationSettings _connections_commonAnimationSettings_xjal = new LinkToAgentAnimationSettingsImpl( false, black, 1.0, LINE_STYLE_SOLID, ARROW_NONE, 0.0 );
public LinkToAgentCollection<Agent, Agent> connections = new LinkToAgentStandardImpl<Agent, Agent>(this, _connections_commonAnimationSettings_xjal);
@Override
public LinkToAgentCollection<? extends Agent, ? extends Agent> getLinkToAgentStandard_xjal() {
return connections;
}
@AnyLogicInternalCodegenAPI
public void drawLinksToAgents(boolean _underAgents_xjal, LinkToAgentAnimator _animator_xjal) {
super.drawLinksToAgents(_underAgents_xjal, _animator_xjal);
if ( _underAgents_xjal ) {
_animator_xjal.drawLink( this, connections, true, true );
}
}
public AgentList<? extends Document> getPopulation() {
return (AgentList<? extends Document>) super.getPopulation();
}
public List<? extends Document> agentsInRange( double distance ) {
return (List<? extends Document>) super.agentsInRange( distance );
}
}
[run model] : https://i.stack.imgur.com/8vGA4.jpg
Я делаю проект для учебы. Существует агент с двумя переменными, которые принимают значение True или False с помощью функции randomTrue (). Построение проекта ошибок не выявляет. При запуске выдает ошибку при выполнении дискретного события: NullPointerException. В систему не было введено ни одного агента. На источнике 0. Я попытался сменить агентов, изменил randomTrue () на randomFalse (), но безрезультатно. Какие есть варианты?
NullPointerException
java.lang.NullPointerException
at com.anylogic.engine.Utilities.randomTrue(Unknown Source)
at bank.Document.<init>(Document.java:78)
at bank.Main._doc_enter_newEntity_xjal(Main.java:978)
at bank.Main.access$0(Main.java:975)
at bank.Main$1.newEntity(Main.java:312)
at com.anylogic.libraries.processmodeling.Source.inject(Unknown Source)
at com.anylogic.libraries.processmodeling.Source.executeActionOf(Unknown Source)
at com.anylogic.engine.EventTimeout.execute(Unknown Source)
at com.anylogic.engine.Engine.i(Unknown Source)
at com.anylogic.engine.Engine.if(Unknown Source)
at com.anylogic.engine.Engine.e(Unknown Source)
at com.anylogic.engine.Engine$n.run(Unknown Source)
package bank;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Currency;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.Stack;
import java.util.Timer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.awt.Color;
import java.awt.Font;
import com.anylogic.engine.connectivity.ResultSet;
import com.anylogic.engine.connectivity.Statement;
import com.anylogic.engine.elements.*;
import com.anylogic.engine.markup.Network;
import com.anylogic.engine.Position;
import com.anylogic.engine.markup.PedFlowStatistics;
import com.anylogic.engine.markup.DensityMap;
import static java.lang.Math.*;
import static com.anylogic.engine.UtilitiesArray.*;
import static com.anylogic.engine.UtilitiesCollection.*;
import static com.anylogic.engine.presentation.UtilitiesColor.*;
import static com.anylogic.engine.HyperArray.*;
import com.anylogic.engine.*;
import com.anylogic.engine.analysis.*;
import com.anylogic.engine.connectivity.*;
import com.anylogic.engine.database.*;
import com.anylogic.engine.gis.*;
import com.anylogic.engine.markup.*;
import com.anylogic.engine.routing.*;
import com.anylogic.engine.presentation.*;
import com.anylogic.engine.gui.*;
import com.anylogic.libraries.modules.markup_descriptors.*;
import com.anylogic.libraries.processmodeling.*;
import com.anylogic.libraries.material_handling.*;
import java.awt.geom.Arc2D;
public class Document extends Agent
{
// Parameters
// Plain Variables
public final
boolean
is_simple =
randomTrue(0.6)
;
public final
boolean
has_critical_errors =
randomTrue(0.1)
;
@AnyLogicInternalCodegenAPI
private static Map<String, IElementDescriptor> elementDesciptors_xjal = createElementDescriptors( Document.class );
@AnyLogicInternalCodegenAPI
@Override
public Map<String, IElementDescriptor> getElementDesciptors() {
return elementDesciptors_xjal;
}
@AnyLogicCustomProposalPriority(type = AnyLogicCustomProposalPriority.Type.STATIC_ELEMENT)
public static final Scale scale = new Scale( 10.0 );
@Override
public Scale getScale() {
return scale;
}
/** Internal constant, shouldn't be accessed by user */
@AnyLogicInternalCodegenAPI
protected static final int _STATECHART_COUNT_xjal = 0;
// View areas
public ViewArea _origin_VA = new ViewArea( this, "[Origin]", 0, 0, 1500.0, 600.0 );
@Override
@AnyLogicInternalCodegenAPI
public int getViewAreas(Map<String, ViewArea> _output) {
if ( _output != null ) {
_output.put( "_origin_VA", this._origin_VA );
}
return 1 + super.getViewAreas( _output );
}
@AnyLogicInternalCodegenAPI
protected static final Pair<String, Color>[] _person_customColors_xjal = new Pair[] {
new Pair<String, Color>( "Material__3__Surf", null ),
new Pair<String, Color>( "Material__5__Surf", null ),
new Pair<String, Color>( "Material__6__Surf", null ),
new Pair<String, Color>( "Material__2__Surf", null ),
new Pair<String, Color>( "Material__4__Surf", null ),
new Pair<String, Color>( "Material__1__Surf", null ),
};
@AnyLogicInternalCodegenAPI
protected static final int _person = 1;
/** Internal constant, shouldn't be accessed by user */
@AnyLogicInternalCodegenAPI
protected static final int _SHAPE_NEXT_ID_xjal = 2;
@AnyLogicInternalCodegenAPI
public boolean isPublicPresentationDefined() {
return true;
}
@AnyLogicInternalCodegenAPI
public boolean isEmbeddedAgentPresentationVisible( Agent _a ) {
return super.isEmbeddedAgentPresentationVisible( _a );
}
@AnyLogicInternalCodegenAPI
private void _initialize_level_xjal() {
level.addAll(person);
}
protected Shape3DObject person;
protected com.anylogic.engine.markup.Level level;
private com.anylogic.engine.markup.Level[] _getLevels_xjal;
@Override
public com.anylogic.engine.markup.Level[] getLevels() {
return _getLevels_xjal;
}
@AnyLogicInternalCodegenAPI
private void _createPersistentElementsBP0_xjal() {
person = new Shape3DObject(
Document.this, SHAPE_DRAW_2D3D, true, 0.0, 0.0, 0.0, 0.0,
1.0, true, "/bank/",
"3d/person.dae", OBJECT_3D_YZX_AXIS_ORDER, OBJECT_3D_INTERNAL_LIGHTING_OFF, false, -5.0, -4.0,
7.0, 7.0, null, _person_customColors_xjal );
}
@AnyLogicInternalCodegenAPI
private void _createPersistentElementsAP0_xjal() {
}
@AnyLogicInternalCodegenAPI
private void _createPersistentElementsBS0_xjal() {
}
// Static initialization of persistent elements
private void instantiatePersistentElements_xjal() {
level = new com.anylogic.engine.markup.Level(this, "level", SHAPE_DRAW_2D3D, 0.0, true, true);
_getLevels_xjal = new com.anylogic.engine.markup.Level[] {
level };
_createPersistentElementsBP0_xjal();
}
protected ShapeTopLevelPresentationGroup presentation;
protected ShapeModelElementsGroup icon;
@Override
@AnyLogicInternalCodegenAPI
public ShapeTopLevelPresentationGroup getPresentationShape() {
return presentation;
}
@Override
@AnyLogicInternalCodegenAPI
public ShapeModelElementsGroup getModelElementsShape() {
return icon;
}
/**
* Constructor
*/
public Document( Engine engine, Agent owner, AgentList<? extends Document> ownerPopulation ) {
super( engine, owner, ownerPopulation );
instantiateBaseStructureThis_xjal();
}
@AnyLogicInternalCodegenAPI
public void onOwnerChanged_xjal() {
super.onOwnerChanged_xjal();
setupReferences_xjal();
}
@AnyLogicInternalCodegenAPI
public void instantiateBaseStructure_xjal() {
super.instantiateBaseStructure_xjal();
instantiateBaseStructureThis_xjal();
}
@AnyLogicInternalCodegenAPI
private void instantiateBaseStructureThis_xjal() {
instantiatePersistentElements_xjal();
setupReferences_xjal();
}
@AnyLogicInternalCodegenAPI
private void setupReferences_xjal() {
}
/**
* Simple constructor. Please add created agent to some population by calling goToPopulation() function
*/
public Document() {
}
@Override
@AnyLogicInternalCodegenAPI
public void doCreate() {
super.doCreate();
// Assigning initial values for plain variables
setupPlainVariables_Document_xjal();
// Dynamic initialization of persistent elements
_createPersistentElementsAP0_xjal();
_initialize_level_xjal();
level.initialize();
presentation = new ShapeTopLevelPresentationGroup( Document.this, true, 0, 0, 0, 0 , level );
icon = new ShapeModelElementsGroup( Document.this, getElementProperty( "bank.Document.icon", IElementDescriptor.MODEL_ELEMENT_DESCRIPTORS ) );
icon.setIconOffsets( 0.0, 0.0 );
// Port connectors with non-replicated objects
// Creating replicated embedded objects
setupInitialConditions_xjal( Document.class );
// Dynamic initialization of persistent elements
_createPersistentElementsBS0_xjal();
}
@Override
@AnyLogicInternalCodegenAPI
public void doStart() {
super.doStart();
}
/**
* Assigning initial values for plain variables<br>
* <em>This method isn't designed to be called by user and may be removed in future releases.</em>
*/
@AnyLogicInternalCodegenAPI
public void setupPlainVariables_xjal() {
setupPlainVariables_Document_xjal();
}
/**
* Assigning initial values for plain variables<br>
* <em>This method isn't designed to be called by user and may be removed in future releases.</em>
*/
@AnyLogicInternalCodegenAPI
private void setupPlainVariables_Document_xjal() {
}
// User API -----------------------------------------------------
@AnyLogicInternalCodegenAPI
public static LinkToAgentAnimationSettings _connections_commonAnimationSettings_xjal = new LinkToAgentAnimationSettingsImpl( false, black, 1.0, LINE_STYLE_SOLID, ARROW_NONE, 0.0 );
public LinkToAgentCollection<Agent, Agent> connections = new LinkToAgentStandardImpl<Agent, Agent>(this, _connections_commonAnimationSettings_xjal);
@Override
public LinkToAgentCollection<? extends Agent, ? extends Agent> getLinkToAgentStandard_xjal() {
return connections;
}
@AnyLogicInternalCodegenAPI
public void drawLinksToAgents(boolean _underAgents_xjal, LinkToAgentAnimator _animator_xjal) {
super.drawLinksToAgents(_underAgents_xjal, _animator_xjal);
if ( _underAgents_xjal ) {
_animator_xjal.drawLink( this, connections, true, true );
}
}
public AgentList<? extends Document> getPopulation() {
return (AgentList<? extends Document>) super.getPopulation();
}
public List<? extends Document> agentsInRange( double distance ) {
return (List<? extends Document>) super.agentsInRange( distance );
}
}
[модель запуска]: https://i.stack.imgur.com/8vGA4.jpg
I have been trying to fix a NullPointerException for awhile now and I am completely stumped. It is occurring in a ‘seize’ block, but I cannot determine what exactly is causing it.
I have a feeling it could be the code I have inputted into the seize block due to the fact that I am a beginner.
I am trying to seize another agent and send it to different parts of the world via a parameter within the ‘order’ agent that contains a value from 1 to 8.
On seize unit I have the following code:
if (agent.missionLocation <= 1); KC30resource1.moveTo(main.AsiaRegion.randomPointInside());
{
if (agent.missionLocation <= 2); KC30resource1.moveTo(main.AfricaRegion.randomPointInside());
}
{
if (agent.missionLocation <= 3); KC30resource1.moveTo(main.MiddleeastRegion.randomPointInside());
}
{
if (agent.missionLocation <= 4); KC30resource1.moveTo(main.EuropeRegion.randomPointInside());
}
{
if (agent.missionLocation <= 5); KC30resource1.moveTo(main.NAmericaRegion.randomPointInside());
}
{
if (agent.missionLocation <= 6); KC30resource1.moveTo(main.SAmericaRegion.randomPointInside());
}
{
if (agent.missionLocation <= 7); KC30resource3.moveTo(main.AustraliaNzRegion.randomPointInside());
}
{
if (agent.missionLocation <= 8); KC30resource2.moveTo(main.PacificRegion.randomPointInside());
}
Could this be the reason why it is happening?
Exception during discrete event execution:
NullPointerException
java.lang.NullPointerException
at com.anylogic.libraries.processmodeling.Seize.a(Unknown Source)
at com.anylogic.libraries.processmodeling.Seize.a(Unknown Source)
at com.anylogic.libraries.processmodeling.Seize.a(Unknown Source)
at com.anylogic.libraries.processmodeling.Seize.a(Unknown Source)
at com.anylogic.libraries.processmodeling.Seize.a(Unknown Source)
at com.anylogic.libraries.processmodeling.Seize.a(Unknown Source)
at com.anylogic.libraries.processmodeling.Seize.a(Unknown Source)
at com.anylogic.libraries.processmodeling.Seize$16.a(Unknown Source)
at com.anylogic.libraries.processmodeling.Seize$16.action(Unknown Source)
at com.anylogic.libraries.processmodeling.AsynchronousExecutor_xjal.executeActionOf(Unknown Source)
at com.anylogic.engine.EventTimeout.execute(Unknown Source)
at com.anylogic.engine.Engine.f(Unknown Source)
at com.anylogic.engine.Engine.gj(Unknown Source)
at com.anylogic.engine.Engine.e(Unknown Source)
at com.anylogic.engine.Engine$m.run(Unknown Source)
Expected results are that the unit will fly to the GISregion specified in the code. Each ‘MoveTo’ block for each region is connect to the Seize block.
Thank you in advance.
I’m doing a project for studying. There is an agent with 2 variables that take the value True or False using the randomTrue() function.
The construction of the project does not reveal errors. At startup, it throws an error when executing a discrete event:NullPointerException. Not a single agent was entered into the system. On source 0.
I tried to change agents, changed randomTrue() to randomFalse(), with no results.
What are the options?
NullPointerException
java.lang.NullPointerException
at com.anylogic.engine.Utilities.randomTrue(Unknown Source)
at bank.Document.<init>(Document.java:78)
at bank.Main._doc_enter_newEntity_xjal(Main.java:978)
at bank.Main.access$0(Main.java:975)
at bank.Main$1.newEntity(Main.java:312)
at com.anylogic.libraries.processmodeling.Source.inject(Unknown Source)
at com.anylogic.libraries.processmodeling.Source.executeActionOf(Unknown Source)
at com.anylogic.engine.EventTimeout.execute(Unknown Source)
at com.anylogic.engine.Engine.i(Unknown Source)
at com.anylogic.engine.Engine.if(Unknown Source)
at com.anylogic.engine.Engine.e(Unknown Source)
at com.anylogic.engine.Engine$n.run(Unknown Source)
package bank;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Currency;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.Stack;
import java.util.Timer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.awt.Color;
import java.awt.Font;
import com.anylogic.engine.connectivity.ResultSet;
import com.anylogic.engine.connectivity.Statement;
import com.anylogic.engine.elements.*;
import com.anylogic.engine.markup.Network;
import com.anylogic.engine.Position;
import com.anylogic.engine.markup.PedFlowStatistics;
import com.anylogic.engine.markup.DensityMap;
import static java.lang.Math.*;
import static com.anylogic.engine.UtilitiesArray.*;
import static com.anylogic.engine.UtilitiesCollection.*;
import static com.anylogic.engine.presentation.UtilitiesColor.*;
import static com.anylogic.engine.HyperArray.*;
import com.anylogic.engine.*;
import com.anylogic.engine.analysis.*;
import com.anylogic.engine.connectivity.*;
import com.anylogic.engine.database.*;
import com.anylogic.engine.gis.*;
import com.anylogic.engine.markup.*;
import com.anylogic.engine.routing.*;
import com.anylogic.engine.presentation.*;
import com.anylogic.engine.gui.*;
import com.anylogic.libraries.modules.markup_descriptors.*;
import com.anylogic.libraries.processmodeling.*;
import com.anylogic.libraries.material_handling.*;
import java.awt.geom.Arc2D;
public class Document extends Agent
{
// Parameters
// Plain Variables
public final
boolean
is_simple =
randomTrue(0.6)
;
public final
boolean
has_critical_errors =
randomTrue(0.1)
;
@AnyLogicInternalCodegenAPI
private static Map<String, IElementDescriptor> elementDesciptors_xjal = createElementDescriptors( Document.class );
@AnyLogicInternalCodegenAPI
@Override
public Map<String, IElementDescriptor> getElementDesciptors() {
return elementDesciptors_xjal;
}
@AnyLogicCustomProposalPriority(type = AnyLogicCustomProposalPriority.Type.STATIC_ELEMENT)
public static final Scale scale = new Scale( 10.0 );
@Override
public Scale getScale() {
return scale;
}
/** Internal constant, shouldn't be accessed by user */
@AnyLogicInternalCodegenAPI
protected static final int _STATECHART_COUNT_xjal = 0;
// View areas
public ViewArea _origin_VA = new ViewArea( this, "[Origin]", 0, 0, 1500.0, 600.0 );
@Override
@AnyLogicInternalCodegenAPI
public int getViewAreas(Map<String, ViewArea> _output) {
if ( _output != null ) {
_output.put( "_origin_VA", this._origin_VA );
}
return 1 + super.getViewAreas( _output );
}
@AnyLogicInternalCodegenAPI
protected static final Pair<String, Color>[] _person_customColors_xjal = new Pair[] {
new Pair<String, Color>( "Material__3__Surf", null ),
new Pair<String, Color>( "Material__5__Surf", null ),
new Pair<String, Color>( "Material__6__Surf", null ),
new Pair<String, Color>( "Material__2__Surf", null ),
new Pair<String, Color>( "Material__4__Surf", null ),
new Pair<String, Color>( "Material__1__Surf", null ),
};
@AnyLogicInternalCodegenAPI
protected static final int _person = 1;
/** Internal constant, shouldn't be accessed by user */
@AnyLogicInternalCodegenAPI
protected static final int _SHAPE_NEXT_ID_xjal = 2;
@AnyLogicInternalCodegenAPI
public boolean isPublicPresentationDefined() {
return true;
}
@AnyLogicInternalCodegenAPI
public boolean isEmbeddedAgentPresentationVisible( Agent _a ) {
return super.isEmbeddedAgentPresentationVisible( _a );
}
@AnyLogicInternalCodegenAPI
private void _initialize_level_xjal() {
level.addAll(person);
}
protected Shape3DObject person;
protected com.anylogic.engine.markup.Level level;
private com.anylogic.engine.markup.Level[] _getLevels_xjal;
@Override
public com.anylogic.engine.markup.Level[] getLevels() {
return _getLevels_xjal;
}
@AnyLogicInternalCodegenAPI
private void _createPersistentElementsBP0_xjal() {
person = new Shape3DObject(
Document.this, SHAPE_DRAW_2D3D, true, 0.0, 0.0, 0.0, 0.0,
1.0, true, "/bank/",
"3d/person.dae", OBJECT_3D_YZX_AXIS_ORDER, OBJECT_3D_INTERNAL_LIGHTING_OFF, false, -5.0, -4.0,
7.0, 7.0, null, _person_customColors_xjal );
}
@AnyLogicInternalCodegenAPI
private void _createPersistentElementsAP0_xjal() {
}
@AnyLogicInternalCodegenAPI
private void _createPersistentElementsBS0_xjal() {
}
// Static initialization of persistent elements
private void instantiatePersistentElements_xjal() {
level = new com.anylogic.engine.markup.Level(this, "level", SHAPE_DRAW_2D3D, 0.0, true, true);
_getLevels_xjal = new com.anylogic.engine.markup.Level[] {
level };
_createPersistentElementsBP0_xjal();
}
protected ShapeTopLevelPresentationGroup presentation;
protected ShapeModelElementsGroup icon;
@Override
@AnyLogicInternalCodegenAPI
public ShapeTopLevelPresentationGroup getPresentationShape() {
return presentation;
}
@Override
@AnyLogicInternalCodegenAPI
public ShapeModelElementsGroup getModelElementsShape() {
return icon;
}
/**
* Constructor
*/
public Document( Engine engine, Agent owner, AgentList<? extends Document> ownerPopulation ) {
super( engine, owner, ownerPopulation );
instantiateBaseStructureThis_xjal();
}
@AnyLogicInternalCodegenAPI
public void onOwnerChanged_xjal() {
super.onOwnerChanged_xjal();
setupReferences_xjal();
}
@AnyLogicInternalCodegenAPI
public void instantiateBaseStructure_xjal() {
super.instantiateBaseStructure_xjal();
instantiateBaseStructureThis_xjal();
}
@AnyLogicInternalCodegenAPI
private void instantiateBaseStructureThis_xjal() {
instantiatePersistentElements_xjal();
setupReferences_xjal();
}
@AnyLogicInternalCodegenAPI
private void setupReferences_xjal() {
}
/**
* Simple constructor. Please add created agent to some population by calling goToPopulation() function
*/
public Document() {
}
@Override
@AnyLogicInternalCodegenAPI
public void doCreate() {
super.doCreate();
// Assigning initial values for plain variables
setupPlainVariables_Document_xjal();
// Dynamic initialization of persistent elements
_createPersistentElementsAP0_xjal();
_initialize_level_xjal();
level.initialize();
presentation = new ShapeTopLevelPresentationGroup( Document.this, true, 0, 0, 0, 0 , level );
icon = new ShapeModelElementsGroup( Document.this, getElementProperty( "bank.Document.icon", IElementDescriptor.MODEL_ELEMENT_DESCRIPTORS ) );
icon.setIconOffsets( 0.0, 0.0 );
// Port connectors with non-replicated objects
// Creating replicated embedded objects
setupInitialConditions_xjal( Document.class );
// Dynamic initialization of persistent elements
_createPersistentElementsBS0_xjal();
}
@Override
@AnyLogicInternalCodegenAPI
public void doStart() {
super.doStart();
}
/**
* Assigning initial values for plain variables<br>
* <em>This method isn't designed to be called by user and may be removed in future releases.</em>
*/
@AnyLogicInternalCodegenAPI
public void setupPlainVariables_xjal() {
setupPlainVariables_Document_xjal();
}
/**
* Assigning initial values for plain variables<br>
* <em>This method isn't designed to be called by user and may be removed in future releases.</em>
*/
@AnyLogicInternalCodegenAPI
private void setupPlainVariables_Document_xjal() {
}
// User API -----------------------------------------------------
@AnyLogicInternalCodegenAPI
public static LinkToAgentAnimationSettings _connections_commonAnimationSettings_xjal = new LinkToAgentAnimationSettingsImpl( false, black, 1.0, LINE_STYLE_SOLID, ARROW_NONE, 0.0 );
public LinkToAgentCollection<Agent, Agent> connections = new LinkToAgentStandardImpl<Agent, Agent>(this, _connections_commonAnimationSettings_xjal);
@Override
public LinkToAgentCollection<? extends Agent, ? extends Agent> getLinkToAgentStandard_xjal() {
return connections;
}
@AnyLogicInternalCodegenAPI
public void drawLinksToAgents(boolean _underAgents_xjal, LinkToAgentAnimator _animator_xjal) {
super.drawLinksToAgents(_underAgents_xjal, _animator_xjal);
if ( _underAgents_xjal ) {
_animator_xjal.drawLink( this, connections, true, true );
}
}
public AgentList<? extends Document> getPopulation() {
return (AgentList<? extends Document>) super.getPopulation();
}
public List<? extends Document> agentsInRange( double distance ) {
return (List<? extends Document>) super.agentsInRange( distance );
}
}
[run model] : https://i.stack.imgur.com/8vGA4.jpg
Problem Description:
I am trying to model peer influence for churn situations. Agents will send messages to their peers depending on different conditions. I have also created a List called MessagesReceived to store received messages.(The number of these messages may be >1).
Inside the Agent’s Connections>Communications>OnMessageReceived , I have added this.Messages_Recieved.add(msg);
which should add the received message to the list.
Now I get a NullPointerException Error on onReceive method of my agent.
Solution – 1
The system works fine and processes the message but when it tries to add the new message to the list, it encounters the NullPointerException as the list does not exist yet. Declaring a List as variable is not enough. We need to initiate it too:
List<String> myList = new LinkedList<String>();
I have been able to initiate this variable inside Agent>Properties>Agent Actions>On StartUp :
Messages_Recieved = new LinkedList<String>();
Also note that we cannot initiate a List in java using List<String> myList = new List<String>();
List is generic and you can create different collections (ordered/unordered) depending on the class you are using:
See here for more information. I have used LinkedList in this example.
Solution – 2
first, you have a collections object in the agents palette that you can use instead of this, you can check the collection class options to select LinkedList
but if you insist in using a variable to define the list, you need to initialize it like this::