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

Recommended Answers

All 6 Replies

line 142 reads: if(ft.id==ip.t.id)
so print every one of those values (including intermediates like ip.t) immediately before that line to see which is (are) null.

well, it's telling you on which line you are using a variable that you haven't instantiated yet.
add a print right before it, so you can check which var it is, or add default values to your variables, that should help you out.

i have a question
is it necessary to initialize objects of another classes ,in case these objects hold their own fields.

i ve a class 'transition' with a field 'id' and in another class 'outputarc' i ve object of transition as a field . i supply value to this field(transition) by assigning it to a class 'transition' object.the question is before assigning should i initialize the field 'id' of transition class. tell me if its confusing. thanking you..

class transition
{
int id;
}
class outputarc
{
transition t=new transition();
void somefunction()
{
//assigning value for t
transition s=new transition();
t=s;
//do i need to initialize id of transition class
}
}

If the field is an Object then yes (unless null is a valid value for it). But if it's a primitive then no, because they all have default values (0 or false).

Looking at all the info you have provided so far I would suspect that ip is null for some value of i

thankyou the error is gone now!! but is it possible to redirect in the program (java)
when an exception occured... thankyou once again

...is it possible to redirect in the program (java) when an exception occured...

Sorry, I don't understand the question. Can you expand/re-phrase it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.