Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

why the method returns null?

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys
i wrote 2 classes , 1 main , the other one is a constructor

but for some reason when i try to print it , it prints the address of the object and null . why?






thank you
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, you are increasing the "i" variable in your for loop twice. First in the increment statement (the last part of the loop) and then at the end of the for block.

So you only end up creating an object at index 0, 2, 4 and so on.
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And as for the location of the object, you're not trying to print the variable age, but the instances of Age. To do so, you'll need a toString method or a getter to get the value of the instance variable.
Since you didn't mark the instance variable private you could also use age[y].age
Also, just a piece of advice, don't call everything age, your class, instance variable and array are all called age, it's hard to keep track about what your talking. An array is general pluriel.
So if you set on using thoose kind of names, which I don't recomend change it to Age (class) myAge(instance variable) and ages (array)
 
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . . your class, instance variable and array are all called age, . . .

And if that weren't confusing enough, you have a class called ag. Please have a look at the old Sun style guide about naming things. Yes, naming conventions are important, not just things for us mods to get annoyed about You can introduce errors inot your code by poor naming of things.

Also, GG: welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, youi don't appear to have a method returning null; you have nulls remaining in your array, which GG has correctly explained (). I was a bit surprised after reading this th‍read, to see what title you had given it.
Beware of nulls in an array like that; they can cause you no end of trouble later.
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:And as for the location of the object, you're not trying to print the variable age, but the instances of Age. To do so, you'll need a toString method or a getter to get the value of the instance variable.
Since you didn't mark the instance variable private you could also use age[y].age
Also, just a piece of advice, don't call everything age, your class, instance variable and array are all called age, it's hard to keep track about what your talking. An array is general pluriel.
So if you set on using thoose kind of names, which I don't recomend change it to Age (class) myAge(instance variable) and ages (array)



yeah but why doe's it prints null  with just saying "          System.out.println(age[y]);"?
inside the Age array there are 10 objects of Age who consists the age  ( as i set at the constructor)
doesn't it makes sense the printing the object in the array who has only 1 variable should print this variable value?
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rian bron wrote:
yeah but why doe's it prints null  with just saying "          System.out.println(age[y]);"?


It dosn't print null, it prints the objects location. As for the null values, like both gg and cr tried to explain,
you increment your i variable, that you use as index when filling your array, twice.
So in the first run i = 0, you create your object and place it in your array at index 0.
At the end of your loop you increment i (i is now 1) and when the loop finishes, before the second loop i is incremented again (i is now 2).
The next object being created is placed in the array at index 2, not 1

rian bron wrote:
inside the Age array there are 10 objects of Age who consists the age  ( as i set at the constructor)


You don't have an array with 10  Ageobjects, you have an array with 5 Age objects and 5 null values.
Remove the i++ at the end of your for-loop and the null values will disapear.

rian bron wrote:
doesn't it makes sense the printing the object in the array who has only 1 variable should print this variable value?


It simply dosn't make sence to create an object with a single instance variable. In a real example the class would be for ex Person or Member or whatever and they wouldn't just have an age, but also for example a name, gender, address, etc...
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to make it clear with an example (to show the difference between getMethod and toString I added an other variable):

 
Campbell Ritchie
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote: . . . It dosn't print null, it prints the objects location. . . .

It does actually print null for half the elements in that array, not a location. It isn't usually possible to print an object's location in memory in Java®

It simply dosn't make sence to create an object with a single instance variable. . . .

I would have thought an object with one field is useful as an example whilst learning. There surely are other uses for one&‑field objects, too.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It does actually print null for half the elements in that array, not a location. It isn't usually possible to print an object's location in memory in Java®


Lol I wanted to proof you wrong, cause I had actualy cases where I forget to print a value and was trying to print an object and it returned it's location, so I changed the example to:

but it resulted in the result of the toString method, is it safe to asume that when there is a toString method it's is being automaticly called when trying to print the object?
When removing the toString method the result is:

that is the location of the objectin the heap isn't it?
 
Campbell Ritchie
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . . trying to print an object and it returned it's location . . . is it safe to asume that when there is a toString method it's is being automaticly called when trying to print the object? . . .

That is the hash code after the “@”. If you look in the Object documentation, you will find that the toString() method prints class name then @ then hashcode. Since that method is first declared in Object, every object in Java® has a toString() method. You shuld really always override toString, and in many cases hashCode() and equals() too. You can get the same hash code as returned from the unoverridden method with System#identityHashCode(). Object#hashCode() might return a location, or it might not; if there has been a round of garbage collection, however, most objects will have moved from their original location and identityHashCode() might give you their old locations.
 
reply
    Bookmark Topic Watch Topic
  • New Topic