Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Index out of bounds exception

807589Sep 21 2008 — edited Sep 21 2008
Hey everyone I have a major problem with an Index out of bounds exception in execution time....

It's probably because I have sets of defining coordinates X and Y, first one defining X and the others defining Y

Let me show you what I've written:

private int xPos;
private int yPos;
private String note;
private void gPanel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gPanel1MouseClicked


xPos = evt.getX();
yPos = evt.getY();



if (xPos >= 100 && xPos <= 228 )
{xPos = 158;
gPanel1.setXCoordinates(xPos);
}
else if (xPos >= 242 && xPos <= 346 )
{xPos = 290;
gPanel1.setXCoordinates(xPos);
}
else if (xPos >= 365 && xPos <= 462 )
{ xPos = 406;
gPanel1.setXCoordinates(xPos);
}
else if (xPos >= 481 && xPos <= 571 )
{xPos = 518;
gPanel1.setXCoordinates(xPos);
}
else if (xPos >= 590 && xPos <= 667 )
{ xPos = 621;
gPanel1.setXCoordinates(xPos);
}
else if (xPos >= 694 && xPos <= 771 )
{xPos = 725;
gPanel1.setXCoordinates(xPos);
}
else if (xPos >= 788 && xPos <= 864 )
{ xPos = 819;
gPanel1.setXCoordinates(xPos);
}


//Set 1st string distances
if (yPos >= 15 && yPos <= 39 && xPos >= 100 && xPos <= 228)
{yPos = 22;
xPos = 158;
gPanel1.setXCoordinates(xPos);
gPanel1.setYCoordinates(yPos);
note = "F";
}
else if(yPos >= 15 && yPos <= 39 && xPos >= 242 && xPos <= 346)
{yPos = 20;
gPanel1.setYCoordinates(yPos);
note = "F#";
}
else if(yPos >= 15 && yPos <= 39 && xPos >= 365 && xPos <= 462)
{yPos = 18;
gPanel1.setYCoordinates(yPos);
note = "G";
}
else if(yPos >= 15 && yPos <= 39 && xPos >= 481 && xPos <= 571)
{yPos = 16;
gPanel1.setYCoordinates(yPos);
note = "G#";
}
else if(yPos >= 15 && yPos <= 39 && xPos >= 590 && xPos <= 667)
{yPos = 16;
gPanel1.setYCoordinates(yPos);
note = "A";
}
else if(yPos >= 15 && yPos <= 39 && xPos >= 694 && xPos <= 771)
{yPos = 14;
gPanel1.setYCoordinates(yPos);
note = "A#";
}
else if(yPos >= 15 && yPos <= 39 && xPos >= 788 && xPos <= 864)
{yPos = 12;
gPanel1.setYCoordinates(yPos);
note = "B";
}




//Set 2nd string distances
else if (yPos >= 40 && yPos <= 68 && xPos >= 100 && xPos <= 228)
{yPos = 45;
gPanel1.setYCoordinates(yPos);
note = "C"; }
else if (yPos >= 40 && yPos <= 68 && xPos >= 242 && xPos <= 346)
{yPos = 45;
gPanel1.setYCoordinates(yPos);
note = "C#"; }
else if (yPos >= 40 && yPos <= 68 && xPos >= 365 && xPos <= 462)
{yPos = 44;
gPanel1.setYCoordinates(yPos);
note = "D"; }
else if (yPos >= 40 && yPos <= 68 && xPos >= 481 && xPos <= 571)
{yPos = 45;
gPanel1.setYCoordinates(yPos);
note = "D#"; }
else if (yPos >= 40 && yPos <= 68 && xPos >= 590 && xPos <= 667)
{yPos = 45;
gPanel1.setYCoordinates(yPos);
note = "E"; }
else if (yPos >= 40 && yPos <= 68 && xPos >= 694 && xPos <= 771)
{yPos = 43;
gPanel1.setYCoordinates(yPos);
note = "F"; }
else if (yPos >= 40 && yPos <= 68 && xPos >= 788 && xPos <= 864)
{yPos = 40;
gPanel1.setYCoordinates(yPos);
note = "F#"; }




//Set 3rd string distances
else if (yPos >= 69 && yPos <= 95 && xPos >= 100 && xPos <= 228)
{ yPos = 72;
gPanel1.setYCoordinates(yPos);
note ="G#"; }
else if (yPos >= 69 && yPos <= 95 && xPos >= 242 && xPos <= 346)
{ yPos = 72;
gPanel1.setYCoordinates(yPos);
note ="A"; }
else if (yPos >= 69 && yPos <= 95 && xPos >= 365 && xPos <= 462)
{ yPos = 70;
gPanel1.setYCoordinates(yPos);
note ="A#"; }
else if (yPos >= 69 && yPos <= 95 && xPos >= 481 && xPos <= 571)
{ yPos = 69;
gPanel1.setYCoordinates(yPos);
note ="B"; }
else if (yPos >= 69 && yPos <= 95 && xPos >= 590 && xPos <= 667)
{ yPos = 68;
gPanel1.setYCoordinates(yPos);
note ="C"; }
else if (yPos >= 69 && yPos <= 95 && xPos >= 694 && xPos <= 771)
{ yPos = 69;
gPanel1.setYCoordinates(yPos);
note ="C#"; }
else if (yPos >= 69 && yPos <= 95 && xPos >= 788 && xPos <= 864)
{ yPos = 68;
gPanel1.setYCoordinates(yPos);
note ="D"; }



//Srt 4th string distances
else if (yPos >= 96 && yPos <= 124 && xPos >= 100 && xPos <= 228)
{yPos = 98;
gPanel1.setYCoordinates(yPos);
note = "D#"; }
else if (yPos >= 96 && yPos <= 124 && xPos >= 242 && xPos <= 346)
{yPos = 98;
gPanel1.setYCoordinates(yPos);
note = "E"; }
else if (yPos >= 96 && yPos <= 124 && xPos >= 365 && xPos <= 462)
{yPos = 100;
gPanel1.setYCoordinates(yPos);
note = "F"; }
else if (yPos >= 96 && yPos <= 124 && xPos >= 481 && xPos <= 571)
{yPos = 102;
gPanel1.setYCoordinates(yPos);
note = "F#"; }
else if (yPos >= 96 && yPos <= 124 && xPos >= 590 && xPos <= 667)
{yPos = 102;
gPanel1.setYCoordinates(yPos);
note = "G"; }
else if (yPos >= 96 && yPos <= 124 && xPos >= 694 && xPos <= 771)
{yPos = 102;
gPanel1.setYCoordinates(yPos);
note = "G#"; }
else if (yPos >= 96 && yPos <= 124 && xPos >= 788 && xPos <= 864)
{yPos = 100;
gPanel1.setYCoordinates(yPos);
note = "A"; }



//Set 5th string distances
else if (yPos >= 125 && yPos <= 149 && xPos >= 100 && xPos <= 228)
{yPos = 125;
gPanel1.setYCoordinates(yPos);
note = "A#"; }
else if (yPos >= 125 && yPos <= 149 && xPos >= 242 && xPos <= 346)
{yPos = 126;
gPanel1.setYCoordinates(yPos);
note = "B"; }
else if (yPos >= 125 && yPos <= 149 && xPos >= 365 && xPos <= 462)
{yPos = 128;
gPanel1.setYCoordinates(yPos);
note = "C"; }
else if (yPos >= 125 && yPos <= 149 && xPos >= 481 && xPos <= 571)
{yPos = 129;
gPanel1.setYCoordinates(yPos);
note = "C#"; }
else if (yPos >= 125 && yPos <= 149 && xPos >= 590 && xPos <= 667)
{yPos = 130;
gPanel1.setYCoordinates(yPos);
note = "D"; }
else if (yPos >= 125 && yPos <= 149 && xPos >= 694 && xPos <= 771)
{yPos = 132;
gPanel1.setYCoordinates(yPos);
note = "D#"; }
else if (yPos >= 125 && yPos <= 149 && xPos >= 788 && xPos <= 864)
{yPos = 133;
gPanel1.setYCoordinates(yPos);
note = "E"; }



//Set 6th string distances
else if (yPos >= 150 && yPos <= 175&& xPos >= 100 && xPos <= 228)
{yPos = 151;
gPanel1.setYCoordinates(yPos);
note = "F";}
else if (yPos >= 150 && yPos <= 175&& xPos >= 242 && xPos <= 346)
{yPos = 152;
gPanel1.setYCoordinates(yPos);
note = "F#";}
else if (yPos >= 150 && yPos <= 175&& xPos >= 365 && xPos <= 462)
{yPos = 153;
gPanel1.setYCoordinates(yPos);
note = "G";}
else if (yPos >= 150 && yPos <= 175&& xPos >= 481 && xPos <= 571)
{yPos = 154;
gPanel1.setYCoordinates(yPos);
note = "G#";}
else if (yPos >= 150 && yPos <= 175&& xPos >= 590 && xPos <= 667)
{yPos = 155;
gPanel1.setYCoordinates(yPos);
note = "A";}
else if (yPos >= 150 && yPos <= 175&& xPos >= 694 && xPos <= 771)
{yPos = 156;
gPanel1.setYCoordinates(yPos);
note = "A#";}
else if (yPos >= 150 && yPos <= 175&& xPos >= 788 && xPos <= 864)
{yPos = 157;
gPanel1.setYCoordinates(yPos);
note = "B";}
else if(xPos < 23 || xPos > 50 && xPos < 100 || xPos > 228 && xPos < 242 || xPos > 346 && xPos < 365 || xPos > 462 && xPos < 481 || xPos > 571 && xPos < 590 || xPos > 677 && xPos < 694 || xPos > 771 && xPos < 788 || xPos > 864)
JOptionPane.showMessageDialog(null, "Invalid position. Try again towards the center of the fret","Error",JOptionPane.ERROR_MESSAGE);


//Paint the dot
gPanel1.paint( gPanel1.getGraphics());

System.out.println(note);
}


What it does is specifying where to draw when the mouse is clicked and where it should print error...

The thing is that I believe that something in the way this is written causes the exception I mentioned...

If you see something wrong please let me know...

Thanks in advance,

Akis
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 19 2008
Added on Sep 21 2008
5 comments
1,982 views