This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Arrays issue... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays issue..." Watch "Arrays issue..." New topic

Arrays issue...


Joined: Nov 20, 2004
Posts: 7
ok,i've made a small program..for educational porpuses only...
i have two Packages..the first named "impl":

The second package is named "UserData":

The main Program(class with the main method looks like that) :

when im trying to compile that im getting the following error:
"Hit uncaught exception java.lang.NullPointerException"

any idea?
[ November 27, 2004: Message edited by: Joe nothing ]
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
the line

UserLog uL1[] = new UserLog[10];

creates an array of 10 references to UserLogs, however, it doesn't create UserLog objects for them to refer to, they all refer to null.

so, the line


attempts to access the setFirstName method of null, which it doesn't have, of course, and you get the NullPointerException.
Amit Saini
Ranch Hand

Joined: Oct 20, 2004
Posts: 280
UserLog uL1[] = new UserLog[10]; //creates 11 reference variables of UserLog type

--->uL1[0] = new UserLog(); //create the object that uL1 will refer to

to initialize each one, use a simple for loop.

Joined: Nov 20, 2004
Posts: 7 i see.
many thanks guys!
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
And here are a few usage and style pointers.Make sure you use String.equals(String) when comparing Strings and other objects. The == and != operators test the references for equality -- not the objects to which they refer. Sometimes you do want to test for reference equality, but not usually. The above should beThe first part before the && is to make sure you don't get a NullPointerException if fname is null and the second part uses trim() in case fname is " " (a space).

Java style typically saves TitleCasing for interface and class names. Packages (impl) are usually all lowercase and methods (setFirstName) leave their first character in lowercase. Constants (you didn't use any, but e.g. EMPTY_NAME = "") use all caps with underscores separating words.

Following the standards that are almost universal will make working on a team and reading from books easier. Feel free to deviate when you have a good reason.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824


Welcome to JavaRanch! UNfortunately, your display name does not meet the JavaRanch Naming Policy. (If you don't want to use your own name, at least pick a name that looks real.) You can change it here.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

Originally posted by Amit Saini:
UserLog uL1[] = new UserLog[10]; //creates 11 reference variables

Not 11, but 10, actually, at indices 0 through 9.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Arrays issue...
jQuery in Action, 3rd edition