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!

still got this error ...Exception: java.lang.ArrayIndexOutOfBoundsExce 1

Status
Not open for further replies.

112055

Programmer
May 13, 2002
61
US

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
 
I would guess then that in this loop :

Code:
for (int i=0; i < vReady.size(); ++i){
when you assign
Code:
temptime
to
Code:
time[i]
, you attempt to assign to time[] at subscript [4], whci is not possible because you declared
Code:
int time[] = new int[4];
- so you are trying to access a part of the array which does not 'exist'.

Before beginning the loop, determine the size of
Code:
vReady
by doing a
Code:
 System.out.println(vRead.size());
, and if this is higher than 4, then you know this is the reason ...


Ben
 
Ok Ben,
What if I set ..
int [] time = null; // I've put in System.out.println(vReady.size()); and there are 51 ..but if I set it to null, then what will I have to do to make the loop work?

thanks again
 
well, I think you are misunderstanding arrays - they are, if you like a set of shelfs which you can place objects or items - each shelf takes only one item, and you can't put up any more shelves after you have designed how many there are to begin with ...

when you assign an array as you have with time[] - you have said there are 4 elements in that array - and you cannot make that array any larger. Thats the way it its. This is why you get the ArrayOutOfBoundsException - because you are trying to assign an item to part of an array which does not exist.

I would suggest using an ArrayList or Vector instead of an array in this case, becasue the size of these objects is dynamic - not static like an array.

Read up on ArrayList on the java.sun.com site or in your favourite Java textbook. Once you have implemented an ArrayList instead of array, I'll check over the code if its still not working ...

Good Luck !

Ben
 
Ben,
I sorted this mess out now, it is working. Thank you.
You've been very helpful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top