File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays issue...

 
SurferBoy
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

uL1[0].SetFirstName("nnn");

attempts to access the setFirstName method of null, which it doesn't have, of course, and you get the NullPointerException.
 
Amit Saini
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UserLog uL1[] = new UserLog[10]; //creates 11 reference variables of UserLog type

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

to initialize each one, use a simple for loop.
 
SurferBoy
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh....now i see.
many thanks guys!
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SurferBoy:

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.

Thanks,
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Saini:
UserLog uL1[] = new UserLog[10]; //creates 11 reference variables


Not 11, but 10, actually, at indices 0 through 9.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic