Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

copy an array of strings in a setter

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everybody :

i m  triing hard to copy  an array of strings inside a setter from an other class , in the same time i m triing to  output it using a system.out.println() , that s the 2 peace of codes from a different parts :



and the other peace of code :

 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what's your question?
 
Sheriff
Posts: 17153
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code won't compile because Person.setName() accepts a String array whereas you're trying to pass a single String argument to it. You can't assign a String to a String[]. Those types are not compatible.

Disregard that. I misread the code.
 
belykia bekkali
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the question is why the System.out.println , dont print my input ? instead of printing my input , i receive an error Exception in thread "main" java.lang.NullPointerException
at GuettersSetters/GuettersSetters.GettersSetters.main(GettersSetters.java:22)
 
Junilu Lacar
Sheriff
Posts: 17153
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's probably this code that you're confused about, isn't it?

This code will not display anything intelligible because it prints out the toString() value of a String array. You do understand that Person.getName() returns a String[] and not a String, right?
 
Junilu Lacar
Sheriff
Posts: 17153
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 17, the for-loop terminates at iname.length - 1

On line 21, the for-loop terminates at iname.length, which is one more element that what the previous for-loop covered. The NullPointerException is happening because of this last element that wasn't set by the previous for-loop.
 
Junilu Lacar
Sheriff
Posts: 17153
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and Welcome to the Ranch!
 
belykia bekkali
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first of all, thank you  for your answers, Junilu Lacar , Swastik Dey , yeah i know that i created a private array of elements in Person.java, instead of making 2 arrays in the same file ,one full and an other empty , i said why not i try to store a string of elments in an other private String , in an other file , so is it posssible to  do this compilation ???
 
 
Saloon Keeper
Posts: 9574
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think choosing better names would help you understand where you have mismatched intent.
'Person' is good, but it has an array that should be plural, as in 'names', not 'name'.
Likewise the getter and setter should be 'setNames()' and 'getNames()'.

Person[] newname... should be Person persons... or Person personArray...
'iname' should be plural, 'inames'.

Note that you fill iname[0] twice but never fill iname[4].

This is an accident waiting to happen. You loop for the size of iname but use that index to go through the Person array. They both happen to have a length of 5 so you are getting away with it for now but any change will probably cause it to break. Also, every Person will then contain the same array of names.
Again, using iname length to iterate through the Person array which could potentially be of a different length.
A better way to print your arrays...
 
Carey Brown
Saloon Keeper
Posts: 9574
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are allocating an array of Person but that array contains nothing but nulls, and nowhere are you actually constructing a Person instance.
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic