Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4566928
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:51:29+00:00 2026-05-21T18:51:29+00:00

I am new to JAVA and found some of its concepts very irritating and

  • 0

I am new to JAVA and found some of its concepts very irritating and no matter how hard I try I can not find suitable explanation for this behavior…of course there are wor around for these problems but still I want to know am I missing something very simple here or JAVA is like this???

  1. I have a string array in one of my class A and I want it to be filled through a method of another class B…so I create an object of class B into A and call the method B.xyz and equate it to the string arra but BOOM I can’t do it….java throws a nullpointer exception……….I dont know why…

.

public class B{
    public void xyz() {
         String[] mystrings=new String[70];
         for(int i=0;i<5;i++)
             mystrings[i]=value;    
         return mystrings;
    }
}

public class A {
   public void abc() {

      B b=new B();
      String[] StringList;
      StringList=b.xyz();
      System.out.println(StringList.length);
   }
}

I have a similar code fragment now sadly the length of the StrinList becomes 70….if I want to print all the strings of this array I dont have any other way….remember even though the size of mystring is 70 in class B only 5 of its components are properly initialized……..SO considering I am in class A and have no way to find out how many times did the for loop in B executed……how do I accurately loop through all the elements of StringList in A………

PS: There are workarounds to solve this problem but I wanted to know why this happens,i.e, why the length attribute doesn’t change according to the components initialized??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:51:30+00:00Added an answer on May 21, 2026 at 6:51 pm

    If you only need an array of length 5 then only initialize it as that size, e.g.:

    public String[] xyz(String value) {
        String[] mystrings = new String[5];
        for (int i = 0; i < mystrings.length; i++) {
            mystrings[i] = value;
        }
        return mystrings;
    }
    

    If you want an array that you can expand you should consider using ArrayList instead. E.g.:

    public List<String> abc(String value) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 5; i++) {
            list.add(value);
        }
        return list;
    }
    

    Then you can get its size, add to it and print the elements like this:

    List<String> list = abc("foo");
    System.out.println(list.size());
    list.add("bar");
    for (String value : list) {
        System.out.println(value);
    }
    

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not able to find actual differences between these two Java EE servers.
I'm studyin' some Java code written by my ex-colleague, and I've found something I
Hey everyone! I am new to blackberry programming although I do have some java
I'm new to Java and to OOP, so some things below may sound silly,
I'm building a new Java web application for my employer. First thing it needs
For a new Java web project I thought about using: Eclipse as IDE Mercurial
Almost every new Java-web-project is using a modern MVC-framework such as Struts or Spring
I'm a relatively new Java programmer coming from C++/STL, and am looking for a
We are starting a new Java EE project and am looking for suggestions regarding
When creating a new Java project in IntelliJ IDEA, the following directories and files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.