Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > object becomes null on return

Reply
Thread Tools

object becomes null on return

 
 
HalcyonWild
Guest
Posts: n/a
 
      09-21-2005
Hi,

I am facing a strange problem and I cannot find the reason. I have a
calling method, processOrder() in a plain java file in which I am doing
this.

public Order processOrder()
{
Customer c = null;
if (orderType.equals("typeA")
{
processA(c, orderManager);
}
else if (orderType.equals("typeB")
{
processB(c, orderManager);
}

logger.log("customer is "+c);
//this prints null. though i populate it in process methods.

}

private void processB(Customer c, OrderManager orderMgr)
{
-- some lines --
c = CustomerBean.getCustomerByID(orderMgr.getCustomerI d());
//this is a session Bean.

logger.log("customer in process method = "+c); //prints fine

}

I wonder what am I doing wrong. How can c become null in the calling
method. I am getting a nullpointer exception for sub. Am I missing
something very fundamental. Or is it some strange behaviour of session
beans. References are passed for objects in Java. So c should retain
its value. I have come across very similar situations and this has
worked for me. I checked if I am making c = null anywhere, but that is
not the case. Also the logs show c just before return in called method.
And immediately, I log c in the calling method, which shows null. This
happens for both processA and processB methods, and they are similar,
with some extra processing for processA.

Thanks,
Halcyon

 
Reply With Quote
 
 
 
 
Ingo R. Homann
Guest
Posts: n/a
 
      09-21-2005
Hi,

What you explain, is not strange, it is simply the behaviour Java uses
when passing parameters. (If you like it or not, that is another
question!

Parameters in Java are not "inout parameters". The reference (=pointer)
to an object is copied when passed to a method.

You can use a return parameter:

public Order processOrder()
{
Customer c = null;
if (orderType.equals("typeA")
{
c=processA(c, orderManager);
}
else if (orderType.equals("typeB")
{
c=processB(c, orderManager);
}

logger.log("customer is "+c);
//this prints null. though i populate it in process methods.

}

private Customer processB(Customer c, OrderManager orderMgr)
{
-- some lines --
c = CustomerBean.getCustomerByID(orderMgr.getCustomerI d());
//this is a session Bean.

logger.log("customer in process method = "+c); //prints fine
return c;
}

Ciao,
Ingo

 
Reply With Quote
 
 
 
 
Michal Waclawek
Guest
Posts: n/a
 
      09-21-2005
Hello there.
I think your problem is this: Costumer is always null becose you never give
it a value in this method. Try something like this:


.....else if (orderType.equals("typeB")
{
c = processB(orderManager);
}


private Costumer processB(OrderManager orderMgr)
> {

return CustomerBean.getCustomerByID(orderMgr.getCustomerI d));
> }

.....

Sorry for poor English :/

Uzytkownik "HalcyonWild" <> napisal w wiadomosci
news: oups.com...
> Hi,
>
> I am facing a strange problem and I cannot find the reason. I have a
> calling method, processOrder() in a plain java file in which I am doing
> this.
>
> public Order processOrder()
> {
> Customer c = null;
> if (orderType.equals("typeA")
> {
> processA(c, orderManager);
> }
> else if (orderType.equals("typeB")
> {
> processB(c, orderManager);
> }
>
> logger.log("customer is "+c);
> //this prints null. though i populate it in process methods.
>
> }
>
> private void processB(Customer c, OrderManager orderMgr)
> {
> -- some lines --
> c = CustomerBean.getCustomerByID(orderMgr.getCustomerI d());
> //this is a session Bean.
>
> logger.log("customer in process method = "+c); //prints fine
>
> }
>
> I wonder what am I doing wrong. How can c become null in the calling
> method. I am getting a nullpointer exception for sub. Am I missing
> something very fundamental. Or is it some strange behaviour of session
> beans. References are passed for objects in Java. So c should retain
> its value. I have come across very similar situations and this has
> worked for me. I checked if I am making c = null anywhere, but that is
> not the case. Also the logs show c just before return in called method.
> And immediately, I log c in the calling method, which shows null. This
> happens for both processA and processB methods, and they are similar,
> with some extra processing for processA.
>
> Thanks,
> Halcyon
>



 
Reply With Quote
 
HalcyonWild
Guest
Posts: n/a
 
      09-21-2005


> Parameters in Java are not "inout parameters". The reference (=pointer)
> to an object is copied when passed to a method.


Thanks Ingo!!
I now realized what I was doing wrong. Basically, it is not possible to
get the object back in the calling method if you are making a new
object in the called method.
In case you are not making a new object and just modifying what you
received as parameter in the called method, the modifications will be
seen in the calling method.
I just tried out the below sample program. I tried out commenting and
uncommenting w=new Vector() line.

import java.util.*;
public class sample2 {
public void modVector() {
Vector v = new Vector();
//Vector v = null;
addVector(v);
System.out.println("Vector vals in calling method\n"+v);
}
public void addVector(Vector w) {
//w = new Vector();
w.add("xyz");
System.out.println("Vector vals in called method\n"+w);
}
public static void main(String args[])
{
sample2 s = new sample2();
s.modVector();
}
};

Thanks all.
Halcyon.

 
Reply With Quote
 
Timbo
Guest
Posts: n/a
 
      09-21-2005
Another possible solution is to have something like a Holder
class, that simulates a pointer. It has an instance variable that
is used to hold the return value:

public class Holder<E>
{
private E value_ = null;

public Holder() {}
public E getValue() { return value_; }
public void setValue(E value) { value_ = value; }
}

So, you can use it like this:

public static void setInteger(Holder<Integer> h)
{
h.setValue(new Integer(5));
}

public static void main(String [] args)
{
Holder<Integer> h = new Holder<Integer>();
setInteger(h);
System.out.println("value = " + h.getValue());
}
 
Reply With Quote
 
HalcyonWild
Guest
Posts: n/a
 
      09-21-2005

Michal Waclawek wrote:
> I think your problem is this: Costumer is always null becose you never give
> it a value in this method. Try something like this:
>
>
> ....else if (orderType.equals("typeB")
> {
> c = processB(orderManager);
> }
>


hi Michal,
The solution that you gave is right. I wanted it to be void return
type, but thats ok. The reason for failure is not that I never give it
a value. The reason is that, I am reassigning c in below method. I must
not do that. In fact see the sample vector program I sent. If you make
new Vector and pass it as method argument, and just add elements in it,
it will reflect in calling method. if you reassign it, it is lost. This
is because references to objects themselves are copied and not passed
by reference.
Maybe you mean to say the same thing.
Thanks.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-21-2005
On 21 Sep 2005 01:58:01 -0700, "HalcyonWild" <>
wrote or quoted :

> nullpointer exception for sub


Please post the exact stack trace.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-21-2005
On 21 Sep 2005 01:58:01 -0700, "HalcyonWild" <>
wrote or quoted :

> logger.log("customer is "+c);
> //this prints null. though i populate it in process methods.


You can't possibly modify a local variable by calling a method, unless
you return a value and put it in the variable. In your methods you
are modifying a copy of c not c itself. Java calls by VALUE not by
REFERENCE.

See http://mindprod.com/jgloss/callbyvalue.html
http://mindprod.com/jgloss/callbyreference.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Vova Reznik
Guest
Posts: n/a
 
      09-21-2005
Roedy Green wrote:

> You can't possibly modify a local variable by calling a method, unless
> you return a value and put it in the variable. In your methods you
> are modifying a copy of c not c itself. Java calls by VALUE not by
> REFERENCE.


You can possibly modify a local variable by calling a method
if local variable is initiated:
private void doWork(){
Rectangle r = new Rectangle(1, 2, 3, 4);
System.out.println(r);
changeVariable(r);
System.out.println(r);
}

private void changeVariable(Rectangle r){
r.x++;
r.y++;
r.width++;
r.height++;
}
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-21-2005
On Wed, 21 Sep 2005 20:20:57 GMT, Vova Reznik <>
wrote or quoted :

>You can possibly modify a local variable by calling a method
>if local variable is initiated:
> private void doWork(){
> Rectangle r = new Rectangle(1, 2, 3, 4);
> System.out.println(r);
> changeVariable(r);
> System.out.println(r);
> }
>
> private void changeVariable(Rectangle r){
> r.x++;
> r.y++;
> r.width++;
> r.height++;
> }


you are not changing the value of r. You are changing the fields in
the object r points to.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
and becomes or and or becomes and Stef Mientki Python 9 05-28-2011 02:04 PM
Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit 15 benn686@hotmail.com C++ 9 08-22-2007 12:13 AM
Why use "return (null);" instead of "return null;" ? Carl Java 21 08-24-2006 04:33 AM
RAM - 2gb becomes 4gb becomes 2gb b Computer Support 10 04-27-2006 11:58 PM
Date entered from textbox becomes null (1/1/1900) when entered into SQL table. TN Bella ASP .Net 1 07-01-2004 02:53 PM



Advertisments