G4P Buffers have not been created

edited March 2017 in Library Questions

@quark

Sorry to bother once again. I'd used a GButton to create a GWindow every time it is clicked. Inside, there is a GTextArea that displays some text. Strangely, half the time it is working well (after adding spaces to \n), half the time, it returns a Buffers have not been created error on closing the window:

===================================================
   G4P V4.1.1 created by Peter Lager
===================================================
java.lang.IllegalStateException: Buffers have not been created
    at sun.awt.windows.WComponentPeer.getBackBuffer(WComponentPeer.java:1018)
    at java.awt.Component$FlipBufferStrategy.getBackBuffer(Component.java:4065)
    at java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Component.java:4050)
    at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4165)
    at java.awt.Component$FlipBufferStrategy.revalidate(Component.java:4147)
    at java.awt.Component$FlipBufferStrategy.getDrawGraphics(Component.java:4139)
    at processing.awt.PSurfaceAWT.render(PSurfaceAWT.java:296)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1541)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

Sometimes, I get another error while trying to open the window, I am not sure of the errors are related:

===================================================
   G4P V4.1.1 created by Peter Lager
===================================================
################  EXCEPTION IN EVENT HANDLER  ################
An error occured during execution of the eventhandler:
CLASS: Defense_Spread_Calculator_0_11_0   METHOD: btn_details_click
    Caused by java.lang.NullPointerException
    g4p_controls.StyledString.getLines(Unknown Source)
    g4p_controls.GTextArea.setStyledText(Unknown Source)
    g4p_controls.GTextArea.setTextImpl(Unknown Source)
    g4p_controls.GTextArea.setText(Unknown Source)
    Defense_Spread_Calculator_0_11_0.createDetailsWindow(Defense_Spread_Calculator_0_11_0.java:1835)
    Defense_Spread_Calculator_0_11_0.btn_details_click(Defense_Spread_Calculator_0_11_0.java:2110)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    g4p_controls.GAbstractControl.fireEvent(Unknown Source)
    g4p_controls.GButton.mouseEvent(Unknown Source)
    g4p_controls.GWindowImpl.mouseEvent(Unknown Source)
    sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1408)
    processing.core.PApplet.handleMethods(PApplet.java:1603)
    processing.core.PApplet.handleMouseEvent(PApplet.java:2695)
    processing.core.PApplet.dequeueEvents(PApplet.java:2618)
    processing.core.PApplet.handleDraw(PApplet.java:2429)
    processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1540)
    processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
##############################################################

Here is a portion of my code:

void createDetailsWindow(){
  int winX=windowX()+width+20, winY=windowY();
  int winW=400, winH=height;
  PImage icon = loadImage(iconFn);

  win_details = GWindow.getWindow(this, "Details", winX, winY, winW, winH, JAVA2D);
  win_details.addDrawHandler(this, "win_details_draw");
  win_details.setActionOnClose(G4P.CLOSE_WINDOW);
  win_details.addOnCloseHandler(this, "win_details_close");

  //Set windows Icon
  JFrame frame = (JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) win_details.getSurface().getNative()).getFrame();
  frame.setIconImage((java.awt.Image) loadImage(iconFn).getNative());

  txt_details = new GTextArea(win_details, 20, 20, winW-40, winH-40-30, G4P.SCROLLBARS_VERTICAL_ONLY);
  txt_details.setPromptText("No Results.");
  txt_details.addEventHandler(this, "txt_details_change");

  btn_detailsCopy = new GButton(win_details, winW-20-90, winH-20-20, 90, 20);
  btn_detailsCopy.setText("Copy");
  btn_detailsCopy.addEventHandler(this, "btn_detailsCopy_click");

  txt_details.setText(generateFullResults());

  win_aboutOpen=true;

}

The function is called on clicking one of the GButton:

public void btn_details_click(GButton source, GEvent event) { //_CODE_:btn_details:922113:
  createDetailsWindow();
  source.setEnabled(false);
} //_CODE_:btn_details:922113:

Once again, I thank you for helping out.

Answers

  • Answer ✓

    The first error is related to Processing closing windows apparently you get similar problems with other GUI libraries and only started happening with Processing V3. Creating and closing windows uses a lot of resources and I recommend hiding and reusing windows if possible. See my G4P User Guides on multiple windows.

    The second stack trace is interesting, starting at line 20 we see that the mouse event has been passed to the g4p_controls.GWindowImpl which tells me that you have clicked on the main display. Now we see that this calls the GButton event (line 19) which calls the fireEvent method which will call your method btn_details_click (line 13) which calls your method createDetailsWindow (line 12).

    This then creates the new window and tries to set the text in the text area (line 11). The NPE was generated in the getLines() method inside the StyledString class. This method formats and styles the text ready for rendering inside the text area.

    After all that I am still not sure of the cause but you might try this

    void createDetailsWindow(){
      int winX=windowX()+width+20, winY=windowY();
      int winW=400, winH=height;
      PImage icon = loadImage(iconFn);
    
      win_details = GWindow.getWindow(this, "Details", winX, winY, winW, winH, JAVA2D);
      win_details.noLoop(); // Stop the window draw loop for a bit
      win_details.addDrawHandler(this, "win_details_draw");
      win_details.setActionOnClose(G4P.CLOSE_WINDOW);
      win_details.addOnCloseHandler(this, "win_details_close");
    
      //Set windows Icon
      JFrame frame = (JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) win_details.getSurface().getNative()).getFrame();
      frame.setIconImage((java.awt.Image) loadImage(iconFn).getNative());
    
      txt_details = new GTextArea(win_details, 20, 20, winW-40, winH-40-30, G4P.SCROLLBARS_VERTICAL_ONLY);
      txt_details.setPromptText("No Results.");
    
      btn_detailsCopy = new GButton(win_details, winW-20-90, winH-20-20, 90, 20);
      btn_detailsCopy.setText("Copy");
      btn_detailsCopy.addEventHandler(this, "btn_detailsCopy_click");
    
      txt_details.setText(generateFullResults());
    
      win_aboutOpen=true;
       // Add the text change handler here after the initial text has been set
       txt_details.addEventHandler(this, "txt_details_change");
      win_details.loop(); // Restart the window draw loop for a bit
    }
    
  • I had read your post on reusing windows (open once, then set the visibility to true or false). However, the animation of opening the window will still show before it vanishes, and it might be confusing for the user (thinking the sudden opening and closing of the window as a bug).

    I'll try your method and report back soon!

  • Answer ✓

    No problem, create the Window when the button is first clicked then set the visibility to true/false thereafter.

  • create the Window when the button is first clicked then set the visibility to true/false thereafter.

    Why didn't I thought of that?!

  • edited March 2017

    Sadly, it does not work. I made some observations:

    1. I have another window created in a similar way and no matter how many times I open and close it, no errors occur.

    2. Changing my code so the text area outputs a fixed simple one line of text makes the NPE goes away, but the "Buffers have not been created" still occurs at times. I've check my function to generate the String to be passed on and it is free from NPE.

    It seems like some problems with the Text Area not being able to handle large amount of text. The String I passed to txt_details has about 1500 characters.

    My code for opening and closing the other window, for reference:

    ///////////////////////
    //Create About window//
    ///////////////////////
    void createAboutWindow(){
    
      String tou="PokéDefense";
      String cheongHei=VERSION+"\n"
                      +"Zombie Cygig 2017\n\n"
                      +"Note: Attack and Special Attack are optional for Defending Pokemon."
                      +"Enter 1.1 for a stat buffed by nature and 0.9 for one being nerfed.\n\n"
                      +"Credits: \n"
                      +"Imouto (moral support)\n"
                      +"Sister (motivational scorning)\n"
                      +"Ricket (Wimpods under bed)\n";
    
      //Height and width of window
      int winW=300, winH=400; 
    
      //Position the About window in the centre of main window
      int winX=windowX()+(width-winW)/2, winY=windowY()+(height-winH)/2; 
    
      win_about = GWindow.getWindow(this, "About", winX, winY, winW, winH, JAVA2D);
      win_about.addDrawHandler(this, "win_about_draw");
      win_about.setActionOnClose(G4P.CLOSE_WINDOW);
      win_about.setAlwaysOnTop (true);
      win_about.addOnCloseHandler(this, "win_about_close");
    
      //Set windows Icon
      JFrame frame = (JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) win_about.getSurface().getNative()).getFrame();
      frame.setIconImage((java.awt.Image) loadImage(iconFn).getNative());
    
      lbl_aboutHeader = new GLabel(win_about, 20, 20, 80, 20);
      lbl_aboutBody = new GLabel(win_about, 20, 50, 260, 160);
    
      lbl_aboutContact = new GLabel(win_about, 20, 210, 80, 20);
    
      btn_FB=new GImageButton(win_about, 20, 230, 125, 20, new String[] { "fb.png"} );
    
      btn_YT=new GImageButton(win_about, 20, 250, 125, 20, new String[] { "yt.png"} );
    
      btn_email=new GImageButton(win_about, 155, 230, 125, 20, new String[] { "email.png"} );
    
      btn_web=new GImageButton(win_about, 155, 250, 125, 20, new String[] { "web.png"} );
    
      lbl_aboutSupport = new GLabel(win_about, 20, 280, 150, 20);
    
      btn_red=new GImageButton(win_about, 20, 300, 125, 40, new String[] { "red.png"} );
    
      btn_loot=new GImageButton(win_about, 155, 300, 125, 40, new String[] { "loot.png"} );
    
      btn_aboutOK = new GButton(win_about, 200, 350, 80, 30);
    
      lbl_aboutHeader.setText(tou);
      lbl_aboutHeader.setTextBold();
      lbl_aboutHeader.setOpaque(false);
    
      lbl_aboutBody.setTextAlign(GAlign.LEFT, GAlign.TOP);
      lbl_aboutBody.setText(cheongHei);
      lbl_aboutBody.setOpaque(false);
    
      lbl_aboutContact.setText("Say Hello");
      lbl_aboutContact.setOpaque(false);
    
      lbl_aboutSupport.setText("Please Support");
      lbl_aboutSupport.setOpaque(false);
    
    
    
      btn_aboutOK.setText("OK");
      btn_aboutOK.addEventHandler(this, "btn_aboutOK_click");
    
      btn_FB.addEventHandler(this, "btn_FB_click");
      btn_YT.addEventHandler(this, "btn_YT_click");
      btn_email.addEventHandler(this, "btn_email_click");
      btn_web.addEventHandler(this, "btn_web_click");
      btn_red.addEventHandler(this, "btn_red_click");
      btn_loot.addEventHandler(this, "btn_loot_click");
    
      win_aboutOpen=true;
    }
    
    
    
    /////////////////////////////////////////////
    //Extra GUI event handlers for About Window//
    /////////////////////////////////////////////
    void win_about_draw(PApplet app, GWinData data){ app.background(245); }
    void win_about_close(GWindow window){ btn_about.setEnabled(true); win_aboutOpen=false; }
    
    void btn_aboutOK_click(GButton source, GEvent event) { win_about.close(); btn_about.setEnabled(true); win_aboutOpen=false;} 
    
    void btn_FB_click(GImageButton imagebutton, GEvent event) { link("https://www.facebook.com/cygig"); } 
    void btn_YT_click(GImageButton imagebutton, GEvent event) { link("https://www.youtube.com/c/zombiecygig"); } 
    void btn_email_click(GImageButton imagebutton, GEvent event) { link("mailto:cygig@outlook.com"); }
    void btn_web_click(GImageButton imagebutton, GEvent event) { link("https://www.cygig.com"); }
    void btn_red_click(GImageButton imagebutton, GEvent event) { link("https://www.youtube.com/channel/UC4mp9A_5VnCkBPQrbEB50Pw"); }
    void btn_loot_click(GImageButton imagebutton, GEvent event) { link("https://www.facebook.com/groups/992704557475462/"); } 
    
  • edited March 2017

    A sample of the String I passed to txt_details:

    == Best Results ==
    Best EV Spread for Both Defenses (averaged)
    Nature: Neutral (Hardy/Docile/Bashful/Quirky/Serious)
    HP: 244
    Defense: 236
    Special Defense: 28
    Damage Taken: 30.075% / 80HP
    
    Best EV Spread for Physical Defense
    Nature: Neutral (Hardy/Docile/Bashful/Quirky/Serious)
    HP: 252
    Defense: 252
    Special Defense: 4
    Damage Taken: 31.835% / 85HP
    
    Best EV Spread for Special Defense
    Nature: Neutral (Hardy/Docile/Bashful/Quirky/Serious)
    HP: 252
    Defense: 12
    Special Defense: 244
    Damage Taken: 23.221% / 62HP
    
    
    == Second Best Results ==
    Best EV Spread for Both Defenses (averaged)
    Nature: Neutral (Hardy/Docile/Bashful/Quirky/Serious)
    HP: 252
    Defense: 228
    Special Defense: 28
    Damage Taken: 30.150% / 81HP
    
    Best EV Spread for Physical Defense
    Nature: Neutral (Hardy/Docile/Bashful/Quirky/Serious)
    HP: 252
    Defense: 248
    Special Defense: 8
    Damage Taken: 32.210% / 86HP
    
    Best EV Spread for Special Defense
    Nature: Neutral (Hardy/Docile/Bashful/Quirky/Serious)
    HP: 252
    Defense: 8
    Special Defense: 248
    Damage Taken: 23.221% / 62HP
    
    
    == Third Best Results ==
    Best EV Spread for Both Defenses (averaged)
    Nature: Neutral (Hardy/Docile/Bashful/Quirky/Serious)
    HP: 244
    Defense: 220
    Special Defense: 44
    Damage Taken: 30.263% / 81HP
    
    Best EV Spread for Physical Defense
    Nature: Neutral (Hardy/Docile/Bashful/Quirky/Serious)
    HP: 252
    Defense: 244
    Special Defense: 12
    Damage Taken: 32.210% / 86HP
    
    Best EV Spread for Special Defense
    Nature: Neutral (Hardy/Docile/Bashful/Quirky/Serious)
    HP: 252
    Defense: 4
    Special Defense: 252
    Damage Taken: 23.221% / 62HP
    
  • edited March 2017

    Try moving this line to the end of the method after you have created the TextArea control.

    win_about.addDrawHandler(this, "win_about_draw");

    The second window event loop will start immediately after the window is created with

    win_about = GWindow.getWindow(this, "About", winX, winY, winW, winH, JAVA2D);

    but adding a large amout of text takes some time and may not be ready when the windows draw method is called.

  • I tried moving the addDrawHandler towards the end, interesting, I consistently get another error this time:

    ===================================================
       G4P V4.1.1 created by Peter Lager
    ===================================================
    java.lang.NullPointerException
        at g4p_controls.StyledString.getLines(Unknown Source)
        at g4p_controls.GTextArea.updateBuffer(Unknown Source)
        at g4p_controls.GTextArea.draw(Unknown Source)
        at g4p_controls.GWindowAWT.draw(Unknown Source)
        at processing.core.PApplet.handleDraw(PApplet.java:2418)
        at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1540)
        at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
    

    Also i realised that even without any errors, repeatedly opening and closing the windows distorts the text that is supposed to appear in the text area, where portions of the text will go missing or get repositioned and highlighting the text area will produce the selection at elsewhere other than where my mouse had selected. Example:

    Notice how the caret is so far away from the highlighted text and the a whole chunk of text is missing from the beginning.

  • The example G4P_EditTextControls has a textarea control with 1850 characters that works fine so I am not sure what the problem is.

    Without being able to run the sketch I am not sure what to suggest next.

  • Here is the source code: https://we.tl/mXuJIgJbma It is too long to paste everything in the forum and to make sense of it. Warning though: It is extremely amateurish and messy, pardon me for me.

  • edited March 2017

    OK I have had a look at the code and some points to note.

    In the 'createDetailsWindow() method' you
    1) use the variable win_aboutOpen, I suspect an incomplete copy and paste job ;) (It is also used in the win close handler.

    2) You have added a text change handler for the text area but it is empty. So why add the handler in the first place.

    3) The window is still allowed to close by clicking on the red cross so will have to be recreated when the details button is clicked.

    I have modified this method (see below) so that the window CANNOT be closed. To simiulate closing/opening of the details method I have added an extra button 'Close' to the details window. The button event handler makes the window invisible and clears the text area.

    The results are generated before the window is created or made visible. This method is time consuming so makes sense to get it done before we worry about creating the window.

    Immediately after the window is looping is disabled until the GUI is ready and re-enabled just before we leave the method.

    The textArea no longer has an event handlet to detect changes because it is not needed.

    Hopefully this should simplify the management of the details window and make the NPE less likely to happen. It seems to work well on my iMac.

    void createDetailsWindow() {
      // Do this first since it might take some time
      String results = generateFullResults();
    
      // See if the window already exists. If it does 
      // simply make it visible and update text area.
      if (win_details != null) {
        win_details.setVisible(true);
        btn_details.setEnabled(false);
        txt_details.setText(results);
        return;
      }
      // The sindow does not exist so make it
      int winX = windowX()+width+20, winY=windowY();
      int winW = 400, winH=height;
      PImage icon = loadImage(iconFn);
    
      win_details = GWindow.getWindow(this, "Details", winX, winY, winW, winH, JAVA2D);
      win_details.noLoop();
      // Disable the details button until we 'close' the window
      btn_details.setEnabled(false);
    
      win_details.setActionOnClose(G4P.KEEP_OPEN);
      // Close handle=r no longer needed
      //win_details.addOnCloseHandler(this, "win_details_close");
    
      //Set windows Icon
      JFrame frame = (JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) win_details.getSurface().getNative()).getFrame();
      frame.setIconImage((java.awt.Image) loadImage(iconFn).getNative());
    
      txt_details = new GTextArea(win_details, 20, 20, winW-40, winH-40-30, G4P.SCROLLBARS_VERTICAL_ONLY | G4P.SCROLLBARS_AUTOHIDE);
      txt_details.setPromptText("No Results.");
    
    
      btn_detailsCopy = new GButton(win_details, winW-20-90, winH-20-20, 90, 20);
      btn_detailsCopy.setText("Copy");
      btn_detailsCopy.addEventHandler(this, "btn_detailsCopy_click");
      // New button to 'close' the window in reality it simply makes the window invisible
      GButton btnDetailsClose = new GButton(win_details, winW-20-200, winH-20-20, 90, 20);
      btnDetailsClose.setText("Close");
      btnDetailsClose.addEventHandler(this, "btn_detailsCLose_click");
    
      // Display results generated earlier
      txt_details.setText(results);
      //txt_details.setStyledText(generateFullResultsSS());
    
      //  win_aboutOpen=true;  ?????? Do you mean win_detailsOpen open
    
      win_details.addDrawHandler(this, "win_details_draw");
      // This is not needed because we are not 
      // txt_details.addEventHandler(this, "txt_details_change");
      win_details.loop();
    }
    
    
    ///////////////////////////////////////////////
    //Extra GUI event handlers for Details Window//
    ///////////////////////////////////////////////
    void win_details_draw(PApplet app, GWinData data) {
      app.background(245); 
    }
    
    void btn_detailsCLose_click(GButton button, GEvent event) {
      win_details.setVisible(false);
      btn_details.setEnabled(true);
      // Clear out the detail text
      txt_details.setText("");
    }
    
Sign In or Register to comment.