aspose file tools*
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays issue..." Watch "Arrays issue..." New topic
Author

Arrays issue...

SurferBoy
Greenhorn

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

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

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
uL1[0].SetFirstName("nnn");

to initialize each one, use a simple for loop.
SurferBoy
Greenhorn

Joined: Nov 20, 2004
Posts: 7
ohh....now 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
Bartender

Joined: Aug 20, 2001
Posts: 1815
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,


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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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: http://aspose.com/file-tools
 
subject: Arrays issue...
 
Similar Threads
Inheritance Trouble
How do display the contents of List<>
program error
Help with Println and Scanner
XStream + variable as an attribute to another variable which is tag.