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

  • Home
  • SEARCH
  • 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 6074653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:24:22+00:00 2026-05-23T10:24:22+00:00

I am working on some project in java. Here I’m stuck at this problem

  • 0

I am working on some project in java.
Here I’m stuck at this problem and not able to figure out where I am going wrong.

I have made two classes: Test and Child.

When I run the code I am get a NullPointerException.

package com.test;
public class Test {

    child newchild = new child();
    public static void main(String[] args) {
        new Test().method();
    }
    void method() {
        String[] b;
        b = newchild.main();
        int i = 0;
        while (i < b.length) {
            System.out.println(b[i]);
        }
    }
}

package com.test;
public class child {
    public String[] main() {
        String[] a = null;
        a[0] = "This";
        a[1] = "is";
        a[2] = "not";
        a[3] = "working";
        return a;
    }
}
  • 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-23T10:24:23+00:00Added an answer on May 23, 2026 at 10:24 am

    Here’s the problem:

    String[] a = null;
    a[0]="This";
    

    You’re immediately trying to dereference a, which is null, in order to set an element in it. You need to initialize the array:

    String[] a = new String[4];
    a[0]="This";
    

    If you don’t know how many elements your collection should have before you start populating it (and often even if you do) I would suggest using a List of some sort. For example:

    List<String> a = new ArrayList<String>();
    a.add("This");
    a.add("is");
    a.add("not");
    a.add("working");
    return a;
    

    Note that you have another problem here:

    int i=0;
    while(i<b.length)
        System.out.println(b[i]);
    

    You’re never changing i, so it will always be 0 – if you get into the while loop at all, you will never get out of it. You want something like this:

    for (int i = 0; i < b.length; i++)
    {
        System.out.println(b[i]);
    }
    

    Or better:

    for (String value : b)
    {
        System.out.println(value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im working on a java project where i need to read some objects from
I was reading through some java code in a project I was working on
I'm currently working on this project that implies some DSP skills. I must extract
I'm new to Java, and I need some help working on this program. This
I'm currently working on some evaluation work for a project that I'm planning. I
I've started working on some enhancements to an ASP.NET web forms e-commerce project, one
The project I'm working on needs some simple audit logging for when a user
I'm working on a project involving some large XML files (from 50MB to over
I am working with asp.net website project that some of pages need authentication. I
I´m working on a project that basically will show some data collected from hardware

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.