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

  • From: "Sina Bahram" <sbahram@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 11 Mar 2008 08:28:09 -0400

The first thing I'd look at is this

you do

        TextReader file = new TextReader(new FileInputStream(filename)); //

But then you don't check to see if file is null, and then you try to use it
in the next line.

        numrooms=file.readInt(); // Reads in the first integer of the file,
which is the number of rooms the cave must hold.

Also, why numrooms-1?

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jared Wright
Sent: Tuesday, March 11, 2008 4:20 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: (Java) Dealing With a Rogue Null Pointer Exception at Runtime

Hi all, The following constructor keeps giving me a null pointer exception
if called at runtime. I guess the easiest place to start is with a code
exerpt. The context is that this class, cave, is made up of an array of
rooms. Each room has a number, three adjacent rooms, a description, etc. It
also tracks what the current room is, per the user's direction in the larger
scope of the program. The constructor gets its info from a text file, and
the amount of rooms in the cave could vary based on this text file's
contents. I think it is this flexible length of the array of rooms for each
cave object that's the trouble spot, but beyond that I am admittedly stuck.
Note that TextReader serves much as the Scanner class would, this one just
has a couple of extra quirks that I like, thus I'm using it instead.

public class cave {
private int current; private int numrooms; // Variables for the current room
the user is in and the number of rooms in the cave.
private room myroom[]; // The array of room objects for the cave.
    public cave(String filename) throws IOException {
        current = 1;
        TextReader file = new TextReader(new FileInputStream(filename)); //
        numrooms=file.readInt(); // Reads in the first integer of the file,
which is the number of rooms the cave must hold.
        myroom = new room[numrooms-1]; // Attempt to populate the myroom
array with the appropriate number of rooms. This is the line the exception
actually references, along with the line that called the constructor (from
another file).
    }

The constructor actually goes on to fill the room objects with more specific
data (room number, description, etc.). Is there a better way to  go about
this? Will I have to somehow explicitly define the array length, or is there
a way that the constructor can make the array  the appropriate size based on
the data in the file? Might I be looking in the wrong place for the source
of this error?

More code available on request. I didn't want the snipet to get too big. 
I hope I've made things clear, and thanks for any guidance, everyone.

Jared


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

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

Other related posts: