Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception: java.lang.ArrayIndexOutOfBoundsException: 4

Status
Not open for further replies.

112055

Programmer
May 13, 2002
61
US
The goal of this program is Process Scheduling, change part of the code of from First_In_First_Out to Shortest_Job_First. I am spinning wheels now and I have made changes many times, I still get different exception errors...need help here..
This is the section of the code that I believe is giving me the hang ups..
public PCB popFront()
{
int time[] = new int[4];
int a[] = new int[4];
PCB p = null;
int shorttime = 0, temptime = 0;

if(vReady.size() == 0)
return null;
else
{
PCB pcb = (PCB) vReady.elementAt(0);
//vReady.remove(0);

for (int i=0; i < vReady.size(); ++i){
p = (PCB) vReady.get(i);
time = p.getHistory();
temptime = time;
if ((temptime < shorttime) || (i == 0)) {
shorttime = temptime;
pcb = p;

}
}

//a = pcb.getHistory();
//Arrays.sort(a);
//p(&quot;array: &quot; + a[0] +&quot; &quot; + a[1] + &quot; &quot; + a[2] + &quot; &quot; + a[3]);

//p(&quot; popFront &quot; + pcb + &quot; new size &quot; + vReady.size()); vReady.remove(pcb);
return pcb;
}

}

********************************************************
This is the error message...

Exception: java.lang.ArrayIndexOutOfBoundsException: 4
java.lang.ArrayIndexOutOfBoundsException: 4
at opsys.sched.cpu.STSchedSJF_popFront(STSchedSJF.java:60)
at opsys.sched.Kernel.runKernel(Kernel.java:114)
at opsys.sched.CPU.clockTick(CPU.java:95)
at opsys.sched.Sim.doSim(Sim.java:82)
at opsys.sched.SimTrend.<init>(SimTrend.java:109)
at opsys.sched.SimSet.<init>(SimSet.java:36)
at opsys.sched.cpu.STSchedSJF.main(STSchedSJF.java:84)

Thanks for the help..
 
Well, without knowing what 'vReady', 'PCB' or 'PCB.getHistory()' is - (unless PCB.getHistory() assigns an integer)
in this part of code :

Code:
            for (int i=0; i < vReady.size(); ++i){
                p = (PCB) vReady.get(i);
                time = p.getHistory();
                temptime = time;

Ben
you are atempting to assign an int (temptime) to an array (time) which is not subscripted (ie - valid would be
Code:
temptime = time[i];
but not
Code:
temptime = time // assigning int to int array here

This is not a valid statement because 'temptime' is an integer, but 'time' is an integer array ...

Ben
 
Thanks sedj for taking time to help out...
I 've made the change as suggested I still got the error...this is the piece of code after the change..


public PCB popFront()
{

int time[] = new int[4];
PCB p = null;
int shorttime = 0;
int temptime = 0;

if(vReady.size() == 0)
return null;
else
{
PCB pcb = (PCB) vReady.elementAt(0);

for (int i=0; i < vReady.size(); ++i){
p = (PCB) vReady.get(i);
time = p.getHistory();
temptime = time;
if ((temptime < shorttime) || (i == 0)) {
shorttime = temptime;
pcb = p;
}
}


vReady.remove(pcb);
return pcb;
}

}

*************************************

Exception: java.lang.ArrayIndexOutOfBoundsException: 4
java.lang.ArrayIndexOutOfBoundsException: 4
at opsys.sched.cpu.STSchedSJF.popFront(STSchedSJF.java:59)
at opsys.sched.Kernel.runKernel(Kernel.java:114)
at opsys.sched.CPU.clockTick(CPU.java:95)
at opsys.sched.Sim.doSim(Sim.java:82)
at opsys.sched.SimTrend.<init>(SimTrend.java:109)
at opsys.sched.SimSet.<init>(SimSet.java:36)
at opsys.sched.cpu.STSchedSJF.main(STSchedSJF.java:77)
**********************************************************
As for vReady ,it is use as Vector...

private Vector vReady = new Vector();

PCB is short for process control block ..
and pcb.history..everytime FIFO is run , record or histroy will be created.

thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top