Pleeb0101
July 22nd, 2005, 09:25
Hello!
Being bored and jobless I'm trying to brush up on my java for the coming school-year, and I'm have some trouble with interfaces, at least I think I do...
here's the code:
test.java
public class test {
public testWatcher[] list;
public int top = 0;
public void test() {
list = new testWatcher[5];
}
public void setPop (testWatcher watcher) {
if(top < 10) {
System.out.println("Inside setPop " + watcher.toString());
list[top++] = (testWatcher)watcher;
}
}
public static void main (String[] args) {
testClass tester = new testClass();
testWatcher tw = (testWatcher)tester;
test testpop = new test();
tw.testPop();
testpop.setPop(tw);
int l;
for (l = 0 ; l < testpop.top; l++) {
tw = testpop.list[l];
tw.testPop();
}
}
}
testWatcher.java
public interface testWatcher {
public void testPop ();
}
testClass.java
class testClass implements testWatcher {
public void testPop (){
System.out.println("POP!");
}
}
Now, aside from the fact that this has lots of silly bits and hooorrrible code in it and is incomplete, I can't understand why I'm getting a Null Pointer Reference error when it gets to
list[top++] = (testWatcher)watcher;
I've tried with and without the casting, and if you run the code the testWatcher in the main function works, so why is it screwing up?
Any help GREATLY appreciated forever and ever!!
Being bored and jobless I'm trying to brush up on my java for the coming school-year, and I'm have some trouble with interfaces, at least I think I do...
here's the code:
test.java
public class test {
public testWatcher[] list;
public int top = 0;
public void test() {
list = new testWatcher[5];
}
public void setPop (testWatcher watcher) {
if(top < 10) {
System.out.println("Inside setPop " + watcher.toString());
list[top++] = (testWatcher)watcher;
}
}
public static void main (String[] args) {
testClass tester = new testClass();
testWatcher tw = (testWatcher)tester;
test testpop = new test();
tw.testPop();
testpop.setPop(tw);
int l;
for (l = 0 ; l < testpop.top; l++) {
tw = testpop.list[l];
tw.testPop();
}
}
}
testWatcher.java
public interface testWatcher {
public void testPop ();
}
testClass.java
class testClass implements testWatcher {
public void testPop (){
System.out.println("POP!");
}
}
Now, aside from the fact that this has lots of silly bits and hooorrrible code in it and is incomplete, I can't understand why I'm getting a Null Pointer Reference error when it gets to
list[top++] = (testWatcher)watcher;
I've tried with and without the casting, and if you run the code the testWatcher in the main function works, so why is it screwing up?
Any help GREATLY appreciated forever and ever!!