aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused about invoking methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused about invoking methods" Watch "Confused about invoking methods" New topic
Author

Confused about invoking methods

saied ims
Ranch Hand

Joined: Jun 21, 2005
Posts: 109
here my previous post
class Over{
void printHi(){
System.out.println("hi iam over");
}
void printHi2(){
System.out.println("hi iam testing2");
}
}
public class TestOver extends Over{
Over hh ;
public static void main(String [] islam){
Over r=new Over();
TestOver f=new TestOver();
r.printHi();
f.printHi();
}
void printHi(){

hh.printHi2();}
\\\ why this in not legeal its compile but runtime error , i used the hh refrence to invoke the method,why should i make a new Over hh=new Over(); thanks i got the answers that it must be initalized and i got it but in this example here

public class Horse extends Animal {
private Halter myHalter;
public void tie(LeadRope rope) {
myHalter.tie(rope); //why i didnt make Halter myHalter=new Halter,this work but the previous example didnt work iam pretty confused
}
}
public class Halter {
public void tie(LeadRope aRope) {
// Do the actual tie work here
}
}
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Hi,

IT WILL BE BETTER IF YOU PUT YOUR CODE IN CODE TAG. SO THAT OTHERS CAN EASILY UNDERSTAND AND IT WOULD INCREASE NUMBER OF REPLIES.

For those still wondering how to use code tag see it here...How to use code tag



In this example too, if you do not create an instance of Halter before calling method tie(), you will get same exception NullPointer what you get in your previous program.

For example, this code writte below is similar to your previous code in other thread. This code will also gets compiled and will give same exception at runtime because myHalter is assigned to default value of null. Calling any method on null reference will give NullPointerException.



NullPointerException from very first page of google


Naseem
[ July 15, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
saied ims
Ranch Hand

Joined: Jun 21, 2005
Posts: 109
thanks alot its really help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused about invoking methods