Page 1 of 1

If anyone is familar with programming in java..

Posted: November 23, 2004, 1:30 pm
by Akaran_D
Could you drop me a PM?
Have a bug that's been ticking me off for days that I can't seem to work around.

edit: Specifically, while/for loop & a file reading issue with a null pointer exception error.

Posted: November 23, 2004, 1:32 pm
by Tenuvil
post code pls.

Posted: November 23, 2004, 1:39 pm
by Akaran_D

Code: Select all

public static void LoadFile()
{
 String Filename;
 TextInputFile LoadedFile;
 Stdout.println("Enter file to load...");
 Filename = Stdin.readLine();
 LoadedFile = new TextInputFile(Filename);
 
 Index = new String[100];
 VIDA = new int[100];
 VPA = new int[100];
 VAA = new int[100];
 VGA = new int[100];

while (!LoadedFile.eof())
{
 for (int i = 0; i <= 100; i++)
  {
  
    VIDA[i] = LoadedFile.readInt();
    VPA[i] = LoadedFile.readInt();
    VAA[i] = LoadedFile.readInt();
    VGA[i] = LoadedFile.readInt();
    Stdout.println(VIDA[i]);
    Stdout.println(VPA[i]);
    Stdout.println(VAA[i]);
    Stdout.println(VGA[i]);
   }
   
   Stdout.println("End of file");
   LoadedFile.close();
  }

}
I'm getting mutiple "reading past end of file" errors. I think I've got it traced to the i = 100, but when I set it up so that it reads all the lines in the file and sets that value to a variable called "FileSize" and then set i <= Filesize, it throws out another IO exception.. Null pointer, I think.

I'm only a first semester programmer and my teacher is a brilliant man but can't explain his way out of a plastic bag sometimes. He hates to give us actual code, wants us to 'figure it out' on our own using what we've found that works.

edit: If I remove the For loop, it's fine, and doesn't attempt to read past the file. However, then it doesn't scale the i values for the arrays.

Posted: November 23, 2004, 2:08 pm
by Tenuvil
try getting rid of the for/next loop and increment i manually in the while loop.

it seems that if you have less than 100 records in your input file you'd hit eof without the outer while (!eof)/do loop ever seeing eof.

why 100?

Posted: November 23, 2004, 2:16 pm
by Akaran_D
Answer:
Because that is the length my professor told me to do it. I spent two hours figuring out an algorithm that would calculate the number of lines in the file (each data entry is 4 lines, each line needs to go into an array), showed it to him, and he's like, "Oh, just take that out and set the value to 100 and be done with it".

I'll give that a try right now.

edit: That worked, thanks Ten. Enjoy the VV's.

Posted: November 23, 2004, 3:40 pm
by Tenuvil
WOOT, Thanks Akaran!

:)

Posted: November 23, 2004, 5:03 pm
by Llaffer
I'm not familiar with java, but if it is similar to C, your "for" should have been :

for (i = 0; i < 100; i++)

vs. i <= 100

You're arrays were defined as 100 (which is numbered 0-99)

So your 0 through 100 inclusive is 101 elements where element "100" isn't defined since max is 99.

Don't know if this was your problem for java or not, but if you want to try to go back to your using "for", try removing the "=".

Posted: November 23, 2004, 5:06 pm
by Lohrno
That's what I said, but I deleted my message...

i = 0; i<=100 will result in 101 iterations.

-=Lohrno

Posted: November 23, 2004, 5:12 pm
by Tenuvil
Why would you need the inner loop anyway? Since processing is stopped by the eof condition, and all you need to do is increment the counter to fill the array nodes, it's inelegant to have a separate for loop inside the while (!eof) loop.

Course what do I know, I'm not a programmer, I'm an auditor :)

Posted: November 23, 2004, 5:28 pm
by Lohrno
Tenuvil wrote:Why would you need the inner loop anyway? Since processing is stopped by the eof condition, and all you need to do is increment the counter to fill the array nodes, it's inelegant to have a separate for loop inside the while (!eof) loop.
Agreed, but I'm not going to coach him on style in a language I don't know. =D
Course what do I know, I'm not a programmer, I'm an auditor :)
You audit code? In that case you are probably more qualified. =D Oh the horrors I bet you've seen! =D

-=Lohrno

Posted: November 23, 2004, 5:41 pm
by Lynks
Well I am a programmer. One possibility is that once you enter you eof loop, you begine to enter your second loop for 100 times. If by any chance you have 1 left over, you re-enter that enter for/next loop and read 100 more times, even though you might only of had 1 line to read.

Or, this might happen:
- Enter EOF loop
- Read 100 times
- Closes file.
- EOF checks to see if File is EOF but it cannot (since its closed) and returns an error


You might run into some problems later on so it would be wise to get rid of it.

Posted: November 23, 2004, 5:42 pm
by Tenuvil
heh, I keep the fact that I can audit code a secret from my corporate masters, otherwise I'd be looking at an 80 hour workweek. :D

I audit the IT environment, generally limited to information security, program change process, system development methodologies, and operations procedures. If you've heard of Sarbanes-Oxley or 404 reviews, that's what I do.

Posted: December 10, 2004, 5:54 pm
by Ashur
SOX SUX

Posted: December 10, 2004, 6:34 pm
by Tenuvil
Ashur wrote:SOX SUX
indeed it do but it pays people like me a fucking king's ransom

Posted: December 10, 2004, 6:36 pm
by Ashur
Yep, no doubt.

I suppose I should mention I work in a J2EE shop for a large bank.

Posted: December 10, 2004, 7:29 pm
by Tenuvil
I feel your pain. People in banking have had to deal with the Money Laundering regs, Privacy Act, Patriot Act and its inherent contradictions to the Privacy Act, and now SOx. It's a wonder more banks don't say "fuck it" and change to become, say, dry cleaners. Privately held dry cleaners at that.