2

In my code i have to pass a bye array (byte[] temp = null;) to a function which is allocated and filled with data inside it. After returning from function it is still null. How can i find a solution to this problem.??? Please help me.

  byte[] temp = null;
  ret = foo(temp);

  boolean foo(byte[] temp)
  {
      temp = new byte[];
      //fill array
  }
6
  • Show us the code you have. If you are not returning the type that was created then of course it will be null. The object is destroyed once you return to main. Commented Sep 19, 2012 at 13:21
  • Can you provide the code of your function ? Commented Sep 19, 2012 at 13:21
  • If thats your code then you can see why its null, post your code in your question as an edit. Commented Sep 19, 2012 at 13:23
  • i know it will be null, i need data in temp array in main method. Is there any way to do so? Commented Sep 19, 2012 at 13:27
  • Java only passed reference by value so you can't change an argument the way you suggest. Commented Sep 19, 2012 at 13:37

2 Answers 2

8

You're not very clear about your code, but if I understand you right, you have something like the following:

byte[] temp = null;
methodThatAllocatesByteArray(temp);
// temp is still null.

If this is a correct understanding of what you're saying, the problem is that temp is a reference to nothing. Sending that reference to another method makes a copy of that reference (rather than use the same reference), therefore, changing that reference (assigning to the parameter variable) only changes it for the local method. What you need to do is to return a new byte[] from the method like this:

public byte[] methodThatAllocatesByteArray() {
    // create and populate array.
    return array;
}

and call it like this: byte[] temp = methodThatAllocatesByteArray(). Or you can initialise the array first, then pass the reference to that array to the other method like this:

byte[] temp = new byte[size];
methodThatAllocatesByteArray(temp);

Since in this case the parameter in methodThatAllocatesByteArray will point to the same array as temp, any changes to it (other than reassigning it to a different array or null), will be accessible through temp.

Sign up to request clarification or add additional context in comments.

2 Comments

please see my comments @ankur
@UnnikrishnanPV I'm afraid I have to second Peter Lawrey; why can't you return a byte[]? And at some point you must know the size of the array, there should be a way of determining that from where you're calling this. The way you want to do this is simply impossible unless you want to change to a different language (such as C). You'll have to tell us what your limiting factors are though if you want us to work around them. Is your API frozen? If so you might have to initialise the array with a value that you know is AT LEAST large enough, then treat the first null value as the end.
4

You need to use this:

 byte[] temp = new byte[some_const];
  ret = foo(temp);

  boolean foo(byte[] temp)
  {
      //fill array
  }

or

 byte[] temp = null;
  temp  = foo(temp);

  byte[] foo(byte[] temp)
  {    temp = new byte[some_const];
      //fill array
      return temp;
  }

2 Comments

i cant return temp.:-) Also i size of temp is only known inside foo
Why can't you return a byte[]? Having foo know what size it should be is ideal, I don't see a problem with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.