Re: Need assistance with arrays
RookThis wrote:
I'm new to Java and trying to understand the array process. I have
file that I am trying to read in and populate an array with the
data. I have this so far, but still having problems. Can someone
tell me what I'm doing wrong? Thank you!
It's usually a good idea to describe the nature of the
problems you're having. In the case at hand I can make a
pretty good guess, but guesses aren't always accurate; next
time (or even this time!), you may find that people guess
wrong and give you lots of advice about problems you're *not*
having ...
carFile[] items = new carFile[50];
This creates the items array and fills it with fifty
null reference values. It does *not* create fifty carFile
objects for them to refer to, so ...
while (ifile1.hasNext())
{
type = ifile1.next();
color = ifile1.nextInt();
description = ifile1.next();
make = ifile1.nextLine();
items[index].setType(type);
.... right here you get a NullPointerException. The value
of items[index] is null; it does not refer to an actual
carFile object. You need to create a carFile object for it
to refer to, perhaps by inserting
items[index] = new carFile();
just before this line (what you actually insert will depend on
what the carFile constructor requires).
By the way, it is customary for the names of classes and
interfaces to begin with upper-case letters: `Test' instead of
`test', `CarFile' instead of `carFile'.
--
Eric Sosman
esosman@ieee-dot-org.invalid