• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invoke method from outside package

 
Nada Mohammed
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello every body ,
i have the follwoing code :
file a.java conatin :
class a {
.......
public void invokeUserMethod(){
System.out.println("i'm here ") ;
m.invoke(......) ;
.................
}
}// a
file b.java conatin :
class b {
void m() {
................ ; invokeUserMethod() ;.............
}
}//class b
the previous will work correctly .However , when i put class a in package then i import it in class b and from class b i call method invokeUserMethod then the problem araise and the result be at run time :
i'm here
NullPoinetr....
is this mean that invoke method can't call method from outside package and is there is any solution for this problem .
please i need any body to tell me where is the problem
and what is the solution
Thanks a lot
Nada
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method is defined as "public" so you are able to call it from outside of the package. Which you are doing successfully. If you were not able to call the method, if for example it was defined as "private", than you would get a compiler error. So there must be some other problem.
When you say "NullPointer..." do you mean that your program has crashed? Can you post the stack dump?
 
Nada Mohammed
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply
my method is public but i think the problem with invoke method it self .
NullPointer mean that invoke can't found the method and i think this happen because class import package set in differnt sudirectry to the package BUT if i change the order of the subdirectires to solve invoke problem i will get anther problem
so pleeease i need solution

 
Nada Mohammed
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply ,
my method is public therefore i think the problem with invoke method may be it can't invoke method from class set in different directry than the package that's why NullPointer... exception appear at run time .However, if i change the directry i will get new problem .
so pleeease i need solution for this problem
Nada
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your invokeUserMethod() is a Member Method. Therefore you need to call it on an instance of class a
Mya.invokeUserMethod()
When the JVM went to look for the instance to call the method on, there was nothing there . . . therefore a nullPointerException.
Create an instance of class a and use it to call the method, OR make the method static and use the class name to invoke the method.
ClassName.staticMethod();
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try recompiling both classes. The compiler should explain to you about the access restriction and thus you wont see the run time exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic