Forums Register Login

Confused about invoking methods

+Pie Number of slices to send: Send
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
}
}
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
thanks alot its really help
Would you like to try a free sample? Today we are featuring tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 680 times.
Similar Threads
preparing for SCJP 5
IS-A and HAS-A relationship
Doubt in K&B SCJP 5 Book
invoking method by refrence
JavaRanch Exclusive! temporary K&B 5 errata
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:57:06.