• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

A little loop problem

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I adjusted your code to the following..

Worked fine.. printed the value of the loop on each line.
 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how do I get rid of the NPE? Do I just initialize the file as public?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!

Layne
[ March 21, 2005: Message edited by: Layne Lund ]
 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Layne! You're a life saver!

I'll probly have a few more questions before this assignment is done.
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic