eclipse - 'Null pointer access' when using java output parameters -


i working in java , wanting use output parameters. here example:

classa obja = null;   if(mymethod(obja))   {      // position (1)    //use obja somehow   }  

======================

public bool mymethod(classa obj)    {         obj = .....    }   

i using eclipse , problem i'm having eclipse shows warning:
null pointer access. variable obja can null @ location when reach position (1)

since there no concept of out parameters in java i'm little stumpped

==================================================================
edit: have gotten several people mention change method return object instead of bool. if need method alter several objects? ex:

 classa obja = null;     classb objb = null;     if(mymethod(obja, objb))       {          // position (1)        //use obja , objb somehow       }  

======================

public bool mymethod(classa obj, classb obj2)    {         //do stuff  }   

how can this? dont want make custom class every flavor of return objects possible?

thanks,
stephanie

you can't you're trying in java since, say, java doesn't support output parameters.

method parameters local variables, making assignments them has no effect outside scope of method. in other words, this:

public void foo(object obj) {     obj = new object(); } 

is equivalent (from point of view of code calling foo(object)) this:

public void foo() {     object obj = new object(); } 

pointless, since created object thrown out once foo() returns.

probably, want change method return object method creates:

public classa mymethod() {    classa obj = ....    ...    return obj; } 

then in calling code:

classa obja = mymethod(); if (obja != null) {     ... } 

alternatively, instantiate instance of classa outside method , pass value is, , have method modify in way:

public boolean mymethod(classa obj) {     obj.setvalue(...);     return true; }  ...  classa obja = new classa(); if (mymethod(obja) {    object val = obja.getvalue();    ... } 

without knowing more specific problem, it's hard design better.

update:

the multi-parameter example added flat-out impossible in java, unfortunately. says java pass-by-reference, in reality it's more pass-by-reference-value. method can modify objects passed it, cannot modify variables in calling scope refer to. if come c++ background, object references in java more pointers (without pointer arithmetic) c++ references.

to make more concrete, consider following class:

public class parameterpassing {     public static void setparams(integer value1, integer value2) {         system.out.println("value1 before: " + value1);         system.out.println("value2 before: " + value2);         value1 = new integer(1);         value2 = new integer(2);         system.out.println("value1 after: " + value1);         system.out.println("value2 after: " + value2);     }      public static void main(string[] args) {         integer valnull = null;         integer val0 = new integer(0);         system.out.println("valnull before: " + valnull);         system.out.println("val0 before: " + val0);          setparams(valnull, val0);          system.out.println("valnull after: " + valnull);         system.out.println("val0 after: " + val0);     } } 

when run this, you'll output:

valnull before: null val0 before: 0 value1 before: null value2 before: 0 value1 after: 1 value2 after: 2 valnull after: null val0 after: 0 

as can see, assignments inside setparams() method have no effect on valnull , val0 refer to.

if need multiple "output" parameters single method, you're going have wrap them in other object, or rethink design. perhaps make references member variables rather locals , have method modify them directly:

public class myclass {     private classa obja;     private classb objb;      ...      private boolean initobjects() {         obja = ...;         objb = ...;         return true;     }      public void othermethod() {         ...         if(initobjects() {             // use obja, objb         }     } } 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -