Re: (Java) Dealing With a Rogue Null Pointer Exception at Runtime

Hi Sina, First, thanks for your help with this one. I fixed the array declaration and will leave it as such, since your explanation makes perfect sense. It doesn't seem to have fixed the problem, so I'll keep working with it. Any further thoughts are of course welcome. Perhaps if I get irked enough, I'll start posting more of the project code to give better context, but I think I'll give another look through my Java documentation and do some experimentation on my end first.


JW

Sina Bahram wrote:
Two things ... yes, I realize the IOException is thrown, but that doesn't
necessarily mean that the returned object won't be null, so you should check
to see if it's null ... I didn't say you should catch the exception and
handle it, since I did see your throws clause. In reality, anything that
will cause it to be null, should, throw an exception, so you should be OK.

Secondly, I think you're thinking of accessing the array, rather than
allocating it, and I think this might actually be the source of your bug
possibly.

If I have 10 rooms, then yes the array goes from 0 to 9, but I have to
allocate an array of size 10, not of size 9, so you have to still allocate
an array of the size of the number of rooms, not of the number of rooms
minus one.
Remember, the maximum index is not equal to the size, it's the size minus
one because arrays are 0 based; however, the size is the size, you dont'
have to subtract one from it, that's how many elements there actually are.

Take care,
Sina
-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jared Wright
Sent: Tuesday, March 11, 2008 7:47 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: (Java) Dealing With a Rogue Null Pointer Exception at Runtime

Sina Bahram wrote:
"you don't check to see if file is null, and then you try to use it in the
next line."
The constructor throws the IOException. I know I don't have much in the way
of sophistocated error handling for this, but that's not really in the scope
of what I'm trying to do. The program will only be run by my professor to
check for what he's actually having us work through on this particular
project, so I can afford to leave some of those loose ends untied. Thanks
for the good reminder of good programming practice though, for sure.

"Also, why numrooms-1?"
To account for the room numbering starting at 1 and the array index starting
at 0. I guess I could just have the room numbers start at 0, but I found it
was easier for me to keep track of things if I just did it this way.
Besides, I'd have to rewrite the text file that has all the info about the
cave's layout if I were to change the first room's number to 0.

Thanks for the help in nailing this down. I'm about to start working on it
in earnest again for the evening, so we'll see what can be done tonight.

JW
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at http://www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at http://www.freelists.org/list/programmingblind

Other related posts: