java - NullPointerException during writeObject() while using Nimbus Look and Feel -


i have 4 classes: testgui, mainmenubar, mainframe , projecttoadd. mainframe , projecttoadd implements serializable. when click on new>project in mainmenubar want create new project, new dir , in dir write serializable object. when object has been writen nullpointerexception (see stacktrace @ end). if remove , feel part of code in main method not error more... i'm using netbeans ide 8.0.2.

testgui code:

package testgui;  import javax.swing.uimanager; import javax.swing.uimanager.lookandfeelinfo;  public class riskmanagergui {      public static void main(string[] args) {          java.awt.eventqueue.invokelater(new runnable() {             public void run() {                  try {                     (lookandfeelinfo info : uimanager.getinstalledlookandfeels()) {                         if ("nimbus".equals(info.getname())) {                             uimanager.setlookandfeel(info.getclassname());                             break;                         }                     }                 } catch (exception e) {                     // if nimbus not available, fall cross-platform                     try {                         uimanager.setlookandfeel(uimanager.getcrossplatformlookandfeelclassname());                     } catch (exception ex) {                         // not worth time                     }                 }                  new mainframe().setvisible(true);              }         });     }  } 

mainmenubar code:

package menu_package;  import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; import testgui.mainframe;  public class mainmenubar extends jmenubar{      private mainframe mainframe;      public mainmenubar(mainframe mainframe){          this.mainframe = mainframe;          jmenu filemenu = new jmenu("file");         jmenu newmenu = new jmenu("new");          jmenuitem projectmenuitem = new jmenuitem("project");           projectmenuitem.addactionlistener(new addnewproject());          newmenu.add(projectmenuitem);          filemenu.add(newmenu);          add(filemenu);     }      class addnewproject implements actionlistener {     public void actionperformed(actionevent event) {              mainframe.addnewproject();      }     } } 

mainframe code:

package testgui;  import java.awt.*; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectoutputstream; import java.io.serializable; import java.util.arraylist; import javax.swing.*; import javax.swing.border.*; import left_panel_package.projectsjtree; import menu_package.mainmenubar;  public class mainframe extends jframe implements serializable{      private string instalationlocation;     private arraylist <projecttoadd> listofprojects;      public mainframe(){         initcomponents();     }      public void initcomponents() {           setinstalationlocation("c:\\users\\pera\\desktop\\riskmanagergui");         listofprojects = new arraylist<projecttoadd>();          setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close);         settitle("test");         setname("mainframe");          jpanel mainpanel = new jpanel();          setcontentpane(mainpanel);         setjmenubar(new mainmenubar(this));          graphicsdevice gd = graphicsenvironment.getlocalgraphicsenvironment().getdefaultscreendevice();         int width = gd.getdisplaymode().getwidth();         int height = gd.getdisplaymode().getheight();         setsize(width, height);         setlocationrelativeto(null);     }      public string getinstalationlocation() {         return instalationlocation;     }      private void setinstalationlocation(string instalationlocation) {         this.instalationlocation = instalationlocation;     }      public void addnewproject(){         string nameofproject = joptionpane.showinputdialog(null, "enter name of new project: ", "add new project",joptionpane.ok_cancel_option);         if ((nameofproject != null) && (nameofproject.length() > 0)) {             try {                 projecttoadd newproject = new projecttoadd(this, nameofproject);                 listofprojects.add(newproject);                  file thedir = new file(newproject.getprojectpath());                 thedir.mkdirs();                  newproject.writetofile();              } catch (ioexception ex) {                 system.out.println(ex.getmessage());             }          }        } } 

projecttoadd code:

package testgui;   import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectoutputstream; import java.io.serializable;  public class projecttoadd implements serializable{      private string nameofproject;     private mainframe mainframe;     private string projectpath;      public projecttoadd (mainframe mainframe, string nameofproject) {              this.mainframe = mainframe;             setnameofproject(nameofproject);             setprojectpath(mainframe.getinstalationlocation()+"\\projects\\"+nameofproject);      }      public string getnameofproject() {         return nameofproject;     }      public void setnameofproject(string nameofproject) {         this.nameofproject = nameofproject;     }      public void writetofile() throws ioexception{         objectoutputstream objectoutputstream =              new objectoutputstream(new fileoutputstream(projectpath+                     "\\"+nameofproject+".bin"));         objectoutputstream.writeobject(this);     }     public void readfromfile(){      }      public string getprojectpath() {         return projectpath;     }      public void setprojectpath(string projectpath) {         this.projectpath = projectpath;     } } 

stack trace:

exception in thread "awt-eventqueue-0" java.lang.nullpointerexception     @ javax.swing.plaf.synth.synthlookandfeel.paintregion(synthlookandfeel.java:371)     @ javax.swing.plaf.synth.synthlookandfeel.update(synthlookandfeel.java:335)     @ javax.swing.plaf.synth.synthrootpaneui.update(synthrootpaneui.java:119)     @ javax.swing.jcomponent.paintcomponent(jcomponent.java:780)     @ javax.swing.jcomponent.paint(jcomponent.java:1056)     @ javax.swing.jcomponent.painttooffscreen(jcomponent.java:5210)     @ javax.swing.repaintmanager$paintmanager.paintdoublebuffered(repaintmanager.java:1579)     @ javax.swing.repaintmanager$paintmanager.paint(repaintmanager.java:1502)     @ javax.swing.repaintmanager.paint(repaintmanager.java:1272)     @ javax.swing.jcomponent._paintimmediately(jcomponent.java:5158)     @ javax.swing.jcomponent.paintimmediately(jcomponent.java:4969)     @ javax.swing.repaintmanager$4.run(repaintmanager.java:831)     @ javax.swing.repaintmanager$4.run(repaintmanager.java:814)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76)     @ javax.swing.repaintmanager.paintdirtyregions(repaintmanager.java:814)     @ javax.swing.repaintmanager.paintdirtyregions(repaintmanager.java:789)     @ javax.swing.repaintmanager.prepaintdirtyregions(repaintmanager.java:738)     @ javax.swing.repaintmanager.access$1200(repaintmanager.java:64)     @ javax.swing.repaintmanager$processingrunnable.run(repaintmanager.java:1732)     @ java.awt.event.invocationevent.dispatch(invocationevent.java:311)     @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:756)     @ java.awt.eventqueue.access$500(eventqueue.java:97)     @ java.awt.eventqueue$3.run(eventqueue.java:709)     @ java.awt.eventqueue$3.run(eventqueue.java:703)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76)     @ java.awt.eventqueue.dispatchevent(eventqueue.java:726)     @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:201)     @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:116)     @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:105)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:101)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:93)     @ java.awt.eventdispatchthread.run(eventdispatchthread.java:82) 

the npe being thrown because you're trying serialize mainframe, object contains gui detritus, including nimbus l&f information, stuff shouldn't serialized , looks non-serializable. how know? mark field transient , see happens:

class projecttoadd implements serializable {     private static final long serialversionuid = 1l;     private string nameofproject;     private transient mainframe mainframe;  // **********     private string projectpath; 

if need serialize information that's held within mainframe, create non-gui model class contains key business data mainframe , serialize that, not view -- not gui itself. should in general never serialize view regardless, , it's idea refactor program logic out of view code.


and right, this:

objectoutputstream.writeobject(this); 

was related npe. apologies you.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -