import java.util.*;
public class PetrinetSim
{
public int noofplaces=0;
public int nooftransitions=0;
public inputarc[] ip=new inputarc[50];
public outputarc[] op=new outputarc[30];
ArrayList<place> pl= new ArrayList<place>();
public Scanner in=new Scanner(System.in);
place[] p;
transition[] t;
void createplaces()
{
int i;
p=new place[noofplaces];
for(i=0;i<noofplaces;i++)
{
p[i]=new place();
p[i].token=0;
p[i].id=i;
}
}
void createtransitions()
{
int i,n,m;
t= new transition[nooftransitions];
for(i=0;i<nooftransitions;i++)
{
t[i]=new transition();
t[i].id=i;
}
}
void createarcs()
{
int i=0,n=0,m=0,s=0;
System.out.println("Available Transitions");
for(i=0;i<nooftransitions;i++)
System.out.println("T"+t[i].id);
System.out.println("========================================================");
System.out.println("Available Places");
for(i=0;i<noofplaces;i++)
System.out.println("P"+p[i].id);
System.out.println("========================================================");
i=0;
System.out.println("Enter the Place that holds Token");
s=in.nextInt();
p[s].token=1;
pl.add(p[s]);
System.out.println("Enter the Input Transitions");
while(true)
{
System.out.println("Enter the place Id:");
n=in.nextInt();
ip[i]=new inputarc();
ip[i].p=new place();
ip[i].t=new transition();
ip[i].p=p[n];
System.out.println("Enter the Transition Id:");
m=in.nextInt();
ip[i].t=t[m];
System.out.println("Enter another Transition:1.Yes 2.Exit");
m=in.nextInt();
if(m==2)
break;
i++;
}
System.out.println("Enter the Output Transitions");
i=0;
while(true)
{
System.out.println("Enter the Transition Id:");
m=in.nextInt();
op[i]=new outputarc();
op[i].t=new transition();
op[i].p=new place();
op[i].t=t[m];
System.out.println("Enter the place Id:");
n=in.nextInt();
op[i].p=p[n];
System.out.println("Enter another Transition:1.Yes 2.Exit");
m=in.nextInt();
if(m==2)
break;
i++;
}
}
void Simulate()
{
int pf=0,i=0;
transition ft=new transition();
while(true)
{
/***
* Checking whether all places in the ArrayList have Tokens.
*/
for(place ps: pl)
{
if(!hastoken(ps))
{
System.out.println("Missing Token!!!Further transition not possible");
System.exit(0);
}
}
/**
* Choosing a place with lowest index in ArrayList (pl) to check for transitions.
*/
place f=pl.get(0);
/**
* Searching input transitions for this place
*/
for(inputarc sp: ip)
{
if(f.id==sp.p.id)
{
ft=sp.t;
pf=1;
break;
}
}
if(pf!=1)
{
System.out.println("Final Place Reached , Place: P["+ f.id+ "]" );
System.exit(0);
}
else
{
/**
* found transition with place 'f' from input arcs.
*/
/**
* Checking whether same transition is done by other places
*/
pf=0;
transition te=new transition();
for(i=0;i<ip.length;i++)
{
//System.out.println("Print"+te.id);
if(ft.id==ip[i].t.id)
{
if(!hastoken(ip[i].p))
{
System.out.println("Missing Token!!!Further transition not possible");
System.exit(0);
}
}
}
/**
* The execution of following lines shows that all places input to that transition have token
*/
/**
* Removing tokens from places and Clearing ArrayList
*/
for(place pt:pl)
{
pt.token=0;
}
pl.clear();
for(outputarc oa:op)
{
if(ft.id==oa.t.id)
{
oa.p.token=1;
pl.add(oa.p);
}
}
}
}
}
public boolean hastoken( place s)
{
if(s.token==1)
return true;
else
return false;
}
public static void main(String a[])
{
Scanner in=new Scanner(System.in);
PetrinetSim ps=new PetrinetSim();
System.out.println("Enter the no of Places:");
ps.noofplaces=in.nextInt();
ps.createplaces();
System.out.println("Enter the no of Transitions:");
ps.nooftransitions=in.nextInt();
ps.createtransitions();
ps.createarcs();
ps.Simulate();
}
}
Exception in thread "main" java.lang.NullPointerException
at PetrinetSim.Simulate(PetrinetSim.java:142)
at PetrinetSim.main(PetrinetSim.java:195)
Java Result: 1
i tried almost all the available solutions to solve this ,but couldnt solve it yet.
help me out to solve this .. thanking