Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Asit Dhal Newsgroups: comp.lang.java.programmer Subject: java thread nullPointerException Date: Sun, 21 Aug 2011 02:50:40 -0700 (PDT) Organization: http://groups.google.com Lines: 99 Message-ID: Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 124.155.242.245 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1313921183 14649 127.0.0.1 (21 Aug 2011 10:06:23 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 21 Aug 2011 10:06:23 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=124.155.242.245; posting-account=vSlqhgoAAADUdkyj0d-a9Sx42La7jIwX User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7272 Please check the following code... /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package pc1; /** * * @author asit */ import java.util.concurrent.Semaphore; class Q { int n; static Semaphore semCon = new Semaphore(0); static Semaphore semProd = new Semaphore(1); void get() { try { semCon.acquire(); } catch(InterruptedException e) { System.out.println("InterruptedException caught"); } System.out.println("Got : " + n); semProd.release(); } void put(int n) { try { semProd.acquire(); } catch(InterruptedException e) { System.out.println("Caught InterrutedException "); } this.n = n; System.out.println("Put : " + n); semCon.release(); } } class Producer implements Runnable { Q q; Producer(Q p) { this.q = q; new Thread(this, "Producer").start(); } public void run() { //line 58 for(int i=0; i<20; i++) q.put(i); } } class Consumer implements Runnable { Q q; Consumer(Q q) { this.q = q; new Thread(this, "Consumer").start(); } public void run() { for(int i=0; i<20; i++) q.get(); } } public class PC1 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Q q = new Q(); new Consumer(q); new Producer(q); } } The above code gives me the following error. run: Exception in thread "Producer" java.lang.NullPointerException at pc1.Producer.run(PC1.java:58) at java.lang.Thread.run(Thread.java:619) Please help me to find the error.