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

  • From: Jared Wright <wright.jaredm@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 12 Mar 2008 15:54:12 -0400

Hi Sina, Your point is taken. I confess it's not the biggest thing I'm worrying about right now, but if you could point me towards any primers on elements of style for OOP or have any individual tips you feel would be appropriate, I'll certainly take a look at them and try and modify my coding practices accordingly. I am, as of now, exclusively a speech user when programming, and I bet there's plenty of things like this I do "incorrectly" as a byproduct of the speech medium.


Best,
JW

Sina Bahram wrote:
He does ... they are of type room. Why room is not capitilized , since it is
a class, I think is a further major big deal ; however.

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Michael Malver
Sent: Tuesday, March 11, 2008 6:19 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: (Java) Dealing With a Rogue Null Pointer Exception at Runtime

Look up the proper declaration for arrays. I think you have to explicitly
state the object type that is going to go in each element.  I haven't had to
declare an array in quite some time, so I may be wrong about this.

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jared Wright
Sent: Tuesday, March 11, 2008 3: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

__________
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: