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 1061153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:26:08+00:00 2026-05-16T18:26:08+00:00

I am new to programming and Java and trying to write a program which

  • 0

I am new to programming and Java and trying to write a program which takes two arrays as input and reports back their sum. I want to do this by creating a class which takes two arrays as constructor inputs and then creating a method that adds them together and a method which prints out this new sum array.

Here is my class:

public class test1 {
    int [] a;
    int [] b;
    int [] final23;

    public test1 (int x [], int y [])
    {
        int [] a = x;
        int [] b = y;
    }

    public int [] sum(int [] x, int[] y)
    {
    int [] a = x;
    int [] b = y;
    for (int i = 0; i < Math.min(x.length, y.length); i++)
    {
        final23 [0]=x[0] + y[0] ;

    }
    return final23;
    }

    public void print()
    {
    for (int i = 0; i < final23.length; i++)
    {
        System.out.println(final23[0]);
    }
    }

}

Here is my main class:

public class main1 {
    public static void main(String[] args)
    {
        int l[] = {4,7,2};
        int k[] = {4,6,2};
        test1 X = new test1(k,l);

        X.sum(k,l);
        X.print();
    }
}

I keep getting an error when I run this through:

Exception in thread “main” java.lang.NullPointerException
at test2.sum(test2.java:17)
at main1.main(main1.java:8)

I guess what I really want is for my sum method to take a test1 object as input. However, I don’t know how to do this.

  • 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-16T18:26:09+00:00Added an answer on May 16, 2026 at 6:26 pm

    Your variable final23 is never initialized.
    In java you have to initialize an array before using it. Either you do it during the declaration (like you did with k and l) or you have to do it later with a new arrayType[arraySize];


    Here are the way an array can be declared/initialized.

    int[] iArray = {1, 2, 3}; //Declaration, Initialization, set values
    
    
    int[] iArray; //Declaration
    iArray = new int[3]; //Initialization
    iArray[0] = 1; //Set value
    
    int[] iArray; //Declaration
    iArray = new Array[3]{1, 2, 3}; // Initialization and set values
    

    You can of course for the two last sample put the initialization on the same line that the declaration.


    Try this (cleaned) code :

    public class test1 {
        int[] final23;
    
        public int[] sum(int[] x, int[] y) {
            final23 = new int[Math.min(x.length, y.length)];
            for (int i = 0; i < final23.length; i++) {
                final23[i] = x[i] + y[i];
    
            }
            return final23;
        }
    
        public void print() {
            for (int aFinal23 : final23) {
                System.out.println(aFinal23);
            }
        }
    
    
        public static void main(String[] args) {
            int l[] = {4, 7, 2};
            int k[] = {4, 6, 2};
            test1 x = new test1();
    
            x.sum(k, l);
            x.print();
        }
    }
    

    Resources :

    • Oracle.com – Arrays
    • JLS – Array Initializers
    • JLS – Array Creation Expressions
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to programming, but I'm preparing to write a Java program. As I'm
I'm still new to programming/Java/Android so I'm trying to understand everything I do and
I'm new to Java programming. I want to write simple Java network protocol. I
I am new to Android and Java programming. I have a class which implements
I'm trying to write a java program that reads data from an image and
I am new to thread programming in Java. To understand threading I'm trying to
I am new to Java programming, I am trying to print names, read multiple
I am new to Java and am trying to run a program using Eclipse.
I'm new to graphics programming. I'm trying to create a program that allows you
Hi I'm new to Java programming. I am trying to execute an external command

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.