We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › ArrayIndexOutOfBoundsException
Page Index Toggle Pages: 1
ArrayIndexOutOfBoundsException (Read 3377 times)
Jun 11th, 2010, 10:34am
Hi. I'm trying to rewrite and customize this java code


into processing. And I have a lot of problems, becouse it's mindcrappity smack for my brain Cheesy.

Here is my code: http://wierzba.wzks.uj.edu.pl/~ciembor/pub/bspline.pde

What I want to rewrite now is DrawFun() function (in my code it is in Bspline class). I really don't know where is the problem. I checked it few times, and every array's size looks correct Sad.
Re: ArrayIndexOutOfBoundsException
Reply #1 - Jun 11th, 2010, 11:58am
The error happens at the line:
while ( (t >= knot[i1]) ) i1++;
You should check that i1 doesn't go beyond knot.length-1...
But if I fix that (with while ( t >= knot[i1] && i1 < knot.length - 1 ) i1++;), I get the same error some lines below...
As I don't understand the code (the original is awful with its one-letter variables and total lack of comments!), I won't debug further...
Re: ArrayIndexOutOfBoundsException
Reply #2 - Jun 11th, 2010, 3:54pm
I know this code is very ugly. Here is some explanation:


Maybe someone who is good in math would know what am I doing wrong.
Re: ArrayIndexOutOfBoundsException
Reply #3 - Jun 12th, 2010, 5:33am
for what it's worth, i tried running the original code in netbeans and it does exactly the same thing if you change n and k (to 3 and 4, as per your code)

but for n=1, k=2, the original values, it's fine, if unexciting.

which makes me think that the code for each of those 4 applets is different and you can't just take the code for the first and change a couple of values to get the second or third.
Re: ArrayIndexOutOfBoundsException
Reply #4 - Jun 12th, 2010, 12:18pm
Java code works fine, if you add new knots to ti[] array (should be k+n+1 knots). I found another mistake in my code. N[][] array was wrong initialized, becouse width in constructor was... 0. I corrected it, now I can run it, but there is black screen Sad

Here is current version:
Re: ArrayIndexOutOfBoundsException
Reply #5 - Jun 12th, 2010, 9:47pm
One issue I noticed (there might be others) is in the lines

for (int j = 0; j < nt; j++) {
     knots.add(new Knot(j * 1/nt));

since both j and nt are ints the result is also an int, which will always be 0 in this case.
You need to cast at least one of them to a double to get a double as a result - which is what the Knot constructor is expecting.

 knots.add(new Knot(j * 1/(double)nt)); 

Re: ArrayIndexOutOfBoundsException
Reply #6 - Jun 13th, 2010, 3:27am
MC wrote on Jun 12th, 2010, 12:18pm:
Java code works fine, if you add new knots to ti[] array (should be k+n+1 knots).

if the size of an array depends on the values of other constant settings then those settings should be final statics defined at the top of the code and the array size defined in terms of these.

readability of the code is borderline terrible. i know it's inherited code but... here are some guidelines. these are often personal preferences (and often provoke religious wars) but this is my list, honed over the years and not set in concrete.

using N and n as names is asking for trouble - the sound they make in your head when reading code is the same so they are easy to confuse. using l and i (and l1 and i1) is also asking for trouble even if programming fonts usually make them look different. single character variable names are generally a poor choice.

compound lines like
int jb = i-m+1;  if (jb < 0) jb = 0;
make it easy to skip important stuff when reading quickly. so use one statement per line.  and use brackets around code blocks, even if the block is only one line long. anything else is cutting corners and will get you in the end. (especially necessary when posting to forums that'll sometimes chomp leading whitespace)

i always use spaces around operators too: i - m + 1, makes it easier to scan. it's not like you're wasting paper 8)

good spot guyy. netbeans also warns me that the k used as a for loop counter is hiding the global k in that one method, but i don't think it matters.
Re: ArrayIndexOutOfBoundsException
Reply #7 - Jun 13th, 2010, 8:54am
koogy's comments on style and code writing are all good.  When I was first getting into Java/processing, I found it helpful to read a few Java style guides such as this one.

I'm not saying that particular style guide is "correct" or that it's the "best" one (as koogy says, there are religious wars about these things).  What's important though, is that if you read a few of those (you can find others by googling "Java style guide" or something similar) it'll get you thinking about how to keep your code clear and readable.  You don't have to agree with or follow every rule in that guide (I don't) but after you read it you'll understand some of the ways that code can be hard to read, and at least you'll be aware of those things when you're writing your own.
Page Index Toggle Pages: 1