My code is as follows. After the creation of MyThread object the control is going directly to line 1 ( commented in code ) and then throwing NullPointerException. What is the reason of this behavior ? i want to know why s is not initialized, i am using the constructor and passing the value while creating the object for it ?
public class ClassOne {
public static void main(String[] args) {
MyThread t = new MyThread("name");
Thread tone = new Thread(t);
tone.start();
for (int i =0; i<=10;i++) {
System.out.println(i);
}
}
}
class MyThread implements Runnable {
String s;
MyThread (String s) {
this.s=s;
System.out.println("Constructor");
}
char[] ch = s.toCharArray(); // line 1
public void run() {
for (char c : ch) {
System.out.println(c);
}
}
}