• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about invoking methods

 
saied ims
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
saied ims
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot its really help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic