java - NullPointer exception when using second non-static reference to static object -


i have static object in class, class writer, , instances of class need refer 1 of static objects. avoid code duplication (where i'd have write same code multiple times, each difference in of static objects used). solution have static writer called writer1 , static writer call writer7, , have non-static writer called otherwriter, in constructor of either writer1 or writer7 points other writer.
however, keep getting nullpointer exceptions when access otherwriter. error , code below - ideas? sorry, code not neat - it's hacky fiddeling @ stage. thanks

error:

java.lang.nullpointerexception     @ popl.poplformative.generalwrite(poplformative.java:108)     @ 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;     }  } 

in main, have

writer1 = new poplformative(1); 

which invokes constructor

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) {         }     } } 

in case, code executed

if (writerid == 1) {     this.otherwriter = poplformative.writer7; } 

but poplformative.writer7 null, hasn't been initialized yet, otherwriter becomes null.

if need these circular references, consider using setters.


Comments

Popular posts from this blog

Change the color of an oval at click in Java AWT -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -