看板 java 關於我們 聯絡資訊
小妹有個問題需請求各位高手幫忙: 最近在接觸JAVA Thread這部分,練習了一個使用Thread取得亂數的小程式, 以下為程式部分: class MyThread extends Thread{ MyThread(int c){ start(); ...} public void run(){ lotto.getChose(); } } class BigLottos{){ public void getChose(){ 選取亂數的過程... try{ Thread.sleep(2000); } catch(InterruptedException e){ } } } public class ThreadLotto { public static void main(String[] args){ MyThread ob1=new MyThread(50); System.out.println("所選取的號碼如下: "); } } 編譯後產生以下例外訊息,似乎是取得的執行緒是空的~"~ 不知道問題出在哪裡,為什麼會讀不到所產生的Thread? Exception in thread "Thread-0" java.lang.NullPointerException at test1.MyThread.run(ThreadLotto.java:23) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.217.209.221
tkcn:都說問題出在23行了 10/10 00:52
PsMonkey:而且我們也不知道 23 行是啥 ==.==== 10/10 01:20
PttTime:lotto是null? 10/10 01:29
ken915007:我的推測~在MyThread的建構子中太早start()了… 10/10 02:18
ken915007:導致還沒assign int c就在執行run了…(猜測) 10/10 02:21
puzi:不是要在thread的外部做start()嗎@@" 10/10 18:45