-1

I am trying to copy the values of the variable s1 into the element of s2 and I am getting null. What am I doing wrong. Here are my Code:

public class Main {
   public static void main(String[] args) {

       String[] s1 = new String[10];
       String[] s2 = new String[10];
       String[] s3 = new String[10];

       
       for (int i = 0; i < s1.length; i++){
           s2[i] = s1[i];
           System.out.println("s2[" + i + "] : " + s2[i]);
       }
   }
}

-------- output -------

s2[0] : null
s2[1] : null
s2[2] : null
s2[3] : null
s2[4] : null
s2[5] : null
s2[6] : null
s2[7] : null
s2[8] : null
s2[9] : null
Kali
  • 23
  • 6
  • 4
    By default, a new object array only contains `null` values . You currently never assign any `String` to any of those arrays. – Arnaud Sep 22 '20 at 07:16
  • 1
    What are you trying to achieve? – Vladislav Varslavans Sep 22 '20 at 07:17
  • You need to allocation some contend for each of the array positions. – dreamcrash Sep 22 '20 at 07:17
  • Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) (If your posted code is incomplete and the part that is initializing elements of `s1` is omitted). Also please describe your expected behavior (output). – MikeCAT Sep 22 '20 at 07:21
  • Related: [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – Lino Sep 22 '20 at 07:22
  • Thanks for help. I changed the following: String [] s1 = {"3", "4", "5"}; Everything is now correct and resolved. thank you – Kali Sep 22 '20 at 10:49

2 Answers2

2

You have created an empty array of Strings, but you have not assigned any content to the individual strings. A string defaults to null, so your output is completely normal.

You can also read this interesting post: What is a Java String's default initial value?

If you want to see any result different from null, you have to initialize the first array, by using:

String[] s1 = {"First string", "Second string", ... }

or (before the for loop, obviously)

s1[1] = "First string"
s1[2] = "Second string"
...
Calaf
  • 792
  • 1
  • 6
  • 21
  • Thanks for help. I changed the following: String [] s1 = {"3", "4", "5"}; Everything is now correct and resolved. thank you – Kali Sep 22 '20 at 10:47
  • @Kali Glad to have been helpful! If my answer has solved your problem, you can select it as "accepted" by clicking on the "v" button to the left of my answer :D – Calaf Sep 22 '20 at 11:38
  • Sory Calaf, but: "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – Kali Sep 22 '20 at 12:04
  • There is another button. It is a "check" button located under the arrows you pressed. Those are used to evaluate an answer (and give +1 or -1 to it), while the other button is to say that the answer has solved the problem. When you press on that button, you gain +2 reputation :D – Calaf Sep 22 '20 at 12:31
  • You can also read this to understand how stackoverflow works: [https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Calaf Sep 22 '20 at 12:33
1

You missed out to initialise s1 array. By default, it will contains null values which you are copying into s2, hence null values.

public class Main {
    public static void main(String[] args) {

        String[] s1 = {"A", "B", "C", "D"};
        String[] s2 = new String[10];
        String[] s3 = new String[10];

   
        for (int i = 0; i < s1.length; i++){
            s2[i] = s1[i];
            System.out.println("s2[" + i + "] : " + s2[i]);
        }
    }
}
Rahul Jain
  • 1,249
  • 7
  • 14