java - how to get the object inside an object? -
i trying use thread can tell me wrong in following code. getting nullpointerexception
in main.
public class threadtest implements runnable { thread t; public threadtest(string name) { thread t = new thread(name); } public void run() { for(int = 0; <= 10; i++) { try { thread.sleep(1000); } catch(exception e) { system.out.println(e); } } } public static void main(string args[]) { threadtest ob = new threadtest("satheesh"); ob.t.start(); } }
in constructor declare local variable called t
uses same name field t
. replace thread t
this.t
or simple t
there:
public threadtest(string name) { this.t=new thread(name); }
btw1, it's highly recommended start class names capital letters, i.e. threadtest
in case better name.
btw2, decent ide spot mistake , drew attention this.
Comments
Post a Comment