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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:58:41+00:00 2026-06-08T16:58:41+00:00

I have a method which takes an array as a parameter from another class:

  • 0

I have a method which takes an array as a parameter from another class:

public void memPass(Memory[] memLocList) {

    memList = memLocList;

            for (int i = 0; i < 10; i++) {
        System.out.println(memList[i].getSomething());
    }
}

-EDIT-
The above prints out 10 values (integers) but if I try the same in the other method with an integer between 0 & 10 I get an NPE.

Can anyone tell me how to access the elements of this array from another method, which also takes in a parameter from another class?

I’m trying to do something along these lines:

public void accessArray(int mem) {

           int someInt = memList[mem].getSomething();
}

-EDIT- Sorry, I should add that this gives a NullPointerException.

-NEW EDIT-

OK, I’ve now edited the code so that all I have in the class is:

public class PLoop {

// instance variable
public Memory[] memlist;

  // method 1 
  public void memPass(Memory[] memLocList) {

   memList = memLocList;
   System.out.println(memList.length);
  }

  // method 2
  public void accessArray(int mem) {

   System.out.println(memList.length);
  }
}

The first method prints an integer representing the length of “memList” and the second gives an NPE.

  • 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-06-08T16:58:43+00:00Added an answer on June 8, 2026 at 4:58 pm

    If I’m understanding you right, you want to be able to store memLocList then access it later? If so, I can’t see what creating an instance variable wouldn’t work.

    public class Test {
        public Memory[] memlist;
    
        public void memPass(Memory[] memLocList) {
            memList = memLocList;
        }
    
        public void accessArray(int mem) {
            int someInt = memList[mem].getSomething();
        }
    }
    

    Of course, I don’t work in Java enough any more, so it might not be possible to create and assign an instance variable like that. But you could always store the elements in another container.

    public class Test {
        public List<Memory> memlist;
    
        public void memPass(Memory[] memLocList) {
            memlist = new ArrayList<Memory>(Arrays.asList(memLocList));
        }
    
        public void accessArray(int mem) {
            int someInt = memList.get(mem).getSomething();
        }
    }
    

    Sorry if I have any syntax errors. But you get the main idea.

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

Sidebar

Related Questions

I have a method which takes an array of strings as parameter and queries
I have a Java method which takes a String as parameter and returns a
I have a method which takes params object[] such as: void Foo(params object[] items)
I have a method which takes a list and do some processing on it
i have a method which takes in a DotNetOpenAuth (formally known as DotNetOpenId) Response
I have a method which takes a directory path as a string. In the
I have an ATL COM component method which takes a BSTR as an in
I have the following method, which takes in the name of a file as
I have the following code which takes an improperly saved Image from the database
Ok so I have a class called Dog() which takes two parameters, a string

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.