2014-04-06 86 views
0

我在Class中有一個靜態對象,其中Class是一個編寫器,然後是需要引用其中一個靜態對象的那個Class的實例。爲了避免代碼重複(我必須多次編寫相同的代碼,每個代碼中唯一的區別在哪個靜態對象被使用)。 我的解決方案是有一個名爲writer1的靜態寫入器和一個靜態寫入器調用writer7,然後有一個名爲otherWriter的非靜態寫入器,它在writer1或writer7的構造函數中指向另一個寫入器。
但是,當我訪問otherWriter時,我總是收到NullPointer異常。下面的錯誤和代碼 - 任何想法? 對不起,代碼並不整齊 - 這只是在這個階段只是討厭而已。 感謝當對靜態對象使用第二個非靜態引用時,NullPointer異常

錯誤:

java.lang.NullPointerException 
    at popl.PoplFormative.generalWrite(PoplFormative.java:108) 
    at popl.PoplFormative.run(PoplFormative.java:51) 

package popl; 

import java.util.Arrays; 

public class PoplFormative extends Thread { 

    int currentIndex = 0; 
    int lastIndexWritten = -1; 

    static int data[]; 
    int writerId; 

    static PoplFormative writer1; 
    static PoplFormative writer7; 

    PoplFormative otherWriter; 

    public static void main(String[] args) { 
     data = new int[10]; 
     writer1 = new PoplFormative(1); 
     writer7 = new PoplFormative(7); 
     writer1.start(); 
     writer7.start(); 
     try { 
      Thread.sleep(600); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println(Arrays.toString(data)); 
    } 

    public PoplFormative(int writer) { 
     this.writerId = writer; 
     if (writerId == 1) { 
      this.otherWriter = PoplFormative.writer7; 
     } 
     else if (writerId == 7) { 
      this.otherWriter = PoplFormative.writer1; 
     } 
     else { 
      System.out.println("Big nasty error occurred"); 
      while (true) { 
      } 
     } 
    } 

    public void run() { 
     try { 
      generalWrite(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      System.out.println("Exception Thrown"); 
      e.printStackTrace(); 
     } 
    } 

    public void generalWrite() throws InterruptedException { 
     while (currentIndex < 5) { 
      //System.out.println(writerId + " writer has currentIndex; " + currentIndex); 
      if (data[Math.max(lastIndexWritten,0)] == writerId || (lastIndexWritten == -1 && writerId == 7)) { 
       write(); 
       synchronized (this) { 
        notify(); 
       } 
       synchronized (otherWriter) { 
        otherWriter.wait(); 
       } 
      } 
      else { 
       synchronized (otherWriter) { 
        otherWriter.wait(); 
        write(); 
       } 
       synchronized (this) { 
        notify(); 
       } 
      } 
      lastIndexWritten += 1; 
      currentIndex += 1; 
     } 
     System.out.println(writerId + " has completed"); 
    } 

    public void write() { 
     System.out.println("writer: " + writerId + " currentIndex: " + currentIndex + " lastIndex: " + lastIndexWritten); 
     data[currentIndex] = writerId; 
    } 

} 
+1

一個對象不是'static'。參考是「靜態」。一個對象不是'null'。引用是'null'。 –

+0

對不起 - 懶惰的語義。但是你也可以說在類中以這種方式使用Object時是靜態的,因爲它不僅僅是不改變的引用,因爲它在Class的所有實例中都是? –

+0

我不明白你的問題。這裏的要點是你的一個變量有一個'null'引用,但你試圖解引用它,導致一個NPE。只要找出哪一個,可能是'otherWriter'。 –

回答

2

在你main,你有

writer1 = new PoplFormative(1); 

它調用

public PoplFormative(int writer) { 
    this.writerId = writer; 
    if (writerId == 1) { 
     this.otherWriter = PoplFormative.writer7; 
    } 
    else if (writerId == 7) { 
     this.otherWriter = PoplFormative.writer1; 
    } 
    else { 
     System.out.println("Big nasty error occurred"); 
     while (true) { 
     } 
    } 
} 

在這種情況下,執行該代碼構造

if (writerId == 1) { 
    this.otherWriter = PoplFormative.writer7; 
} 

PoplFormative.writer7null,因爲它尚未初始化,因此otherWriter也成爲null

如果您需要這些循環引用,請考慮使用setter。

+0

補充 - 謝謝 - 不能相信我錯過了。 –

相關問題