aspose file tools*
The moose likes Beginning Java and the fly likes A little loop problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A little loop problem" Watch "A little loop problem" New topic

A little loop problem

Adam Blais

Joined: Dec 02, 2004
Posts: 27
I have the following code that is supposed to take entries at different position and display them in a JTextArea. My problem is that whenever the method is executed, only the last entry I made is returned. Here is the code:

Why is only the last entry being displayed and what do I need to display all of them?
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
How are items getting into the addressFile? How is it read in/etc

Also you may want to check to see what the avlue for addressFile.size actually is
Jeff Grant
Ranch Hand

Joined: Dec 19, 2001
Posts: 169
I adjusted your code to the following..

Worked fine.. printed the value of the loop on each line.
Adam Blais

Joined: Dec 02, 2004
Posts: 27
I figured out the problem wasn't in the loop.
I was getting a NullPointerException error with

So I added

AddressBook addressFile = new AddressBook.("addressbook.obj");

and this got rid of the NullPointerException. But now, everytime the add button is clicked to add an entry, a new object is created and the previous entries are erased.

I have the line

AddressBook addressFile = new AddressBook.("addressbook.obj");

in the constructor but I still keep gettting the NullPointerException. How am I supposed to avoid this NullPointerException while still adding the entries?
Joel McNary

Joined: Aug 20, 2001
Posts: 1821

You don't want the new in the addEntry method, that's for sure. I'm a bit confused why you would be receiving the NPE whith that line in the constructor. Perhaps if you posted your whole code we would get a better idea of what is going on.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Adam Blais

Joined: Dec 02, 2004
Posts: 27
In the addEntry method I tried putting something as simple as output.setText(addressFile.fileStatus()) and it STILL gave me a nullpointerexception, even with that line in the constructor. I'm boggled as to what is making addressFile null when I'm defining it in the constructor!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you have as a class variable
private AddressBook addressFile;

then, in the constructor you have
AddressBook addressFile = new AddressBook("addressbook.obj");

by including the 'type', you make addressFile local to the constructor,
leaving the class variable as null. It is the class variable which is
accessed by other methods of your program.
Adam Blais

Joined: Dec 02, 2004
Posts: 27
So how do I get rid of the NPE? Do I just initialize the file as public?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061

instead of

As Michael says, the later (incorrect) version declares a NEW local variable with the same name. This variable "disappears" after the constructor completes. However, the member field is never changed and retains its default value, which is null, and thus causes the later NPE.

One way to help you avoid such errors is to use the "this" keyword when referring to member variables. To do this, you change the first (correct) code snippet above to this:

I typically like this syntax because it helps me to distinguish between member fields and local variables.

I hope this helps clear the confusion.

Keep Coding!

[ March 21, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Adam Blais

Joined: Dec 02, 2004
Posts: 27
Thank you Layne! You're a life saver!

I'll probly have a few more questions before this assignment is done.
It is sorta covered in the JavaRanch Style Guide.
subject: A little loop problem