Problems with the application and iText

Hello

I have a small problem and I would like to help me, estubo creating a dynamic menu for an application vaadin, and the dynamic menu is created and works well. But for this process removes the argument class that refers to the class containing the application.

Now the pdf is not displayed, the program sends me an error saying that the pointer is null.

To better explain this little code I will
class Home

This class contains all the variables start and extends to application.

The other class is extended to verticallayout

and thus use

public void muestrapdf (Home app) {
sample = app;
}

In this way I work the code found in the forum is in this direction.
https://vaadin.com/forum/-/message_boards/view_message/246358

public void openPdf (StreamSource StreamSource, final String filename) {
StreamResource streamResource = new StreamResource (StreamSource, filename + “. Pdf”, this); / / $ NON-NLS-1 $
streamResource.setCacheTime (500), / / no cache (<= 0) does not Work with IE8
streamResource.setMIMEType (“application / pdf”) / / $ NON-NLS-1 $

In this line is that I send null pointer.
this.getMainWindow (). open (streamResource, “_blank”) / / null pointer exception In This line

 }

I wonder how I can pass this argument without inserting it to the contractor.

regards

You don’t provide enough information for us to help you, and no, vaadin does NOT “empty” variables.
In the line that you say contains a null pointer, which one is null ? is it getMainWindow ? is it the streamsource ?

I’m guessing that you have a menu or button that creates a PDF, and that you are trying to show it.

Typically, this is done as follows:

  • a button is created
  • in the listener (ClickEvent),
    • the PDF is created, often as a temporary file.
    • the temporary file is used (see the vaadin FileResource) to create a StreamSource
    • the application opens the Stream source

The last 3 actions are typically done together, in the same routine, using regular Java variables.

To better explain my problem I send my main class that calls the class method and create pdf argument it receives and the class that creates the PDF in memory and in the file. the steps above tells me all I’ve been following my program and it works before changing a dynamic menu.

I will explain my problem in more detail

My main class sends you class as an argument to the argument OrdenComprasNueva (Main app)

But to change the dynamic menu and I could not send him, I would like to know if there any way to send this story to the class for me to work OrdenComprasNueva routine PDF creation, before I worked after the change dynamic menu where use reflection to load the class, it does not work or to view the pdf or to save on your computer. Just want to know how I can send this story to the class if there is another way to do it.

This is my main class

import com.vaadin.Application;
import com.vaadin.service.ApplicationContext.TransactionListener;
import com.vaadin.terminal.StreamResource;
import com.vaadin.terminal.StreamResource.StreamSource;
import com.vaadin.ui.Window;

/**

  • The Application’s “main” class
    */
    @SuppressWarnings(“serial”)
    public class Principal extends Application {

    @Override
    public void init() {
    attachVaadinTransactionListener();
    }

    private void attachVaadinTransactionListener() {
    getContext().addTransactionListener(new TransactionListener() {

         public void transactionEnd(Application application, Object transactionData) {
             // Transaction listener gets fired for all (Http) sessions
             // of Vaadin applications, checking to be this one.
         }
    
         public void transactionStart(Application application, Object transactionData) {
         }
     });
    

    }

    private void closeSession() {
    Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
    if (sess.getTransaction().isActive()) {
    sess.getTransaction().commit();
    }
    if (sess.isOpen()) {
    sess.close();
    }
    }

    /**

    • Used to get current Hibernate session. Also ensures an open Hibernate
    • transaction.
      */
      public Session getSession() {
      Session currentSession = HibernateUtil.getSessionFactory().getCurrentSession();
      if (!currentSession.getTransaction().isActive()) {
      currentSession.beginTransaction();
      }
      return currentSession;
      }

    public void openPdf(StreamSource streamSource, final String filename) {
    StreamResource streamResource = new StreamResource(streamSource, filename + “.pdf”, this); //$NON-NLS-1$
    streamResource.setCacheTime(500); // no cache (<=0) does not work with IE8
    streamResource.setMIMEType(“application/pdf”); //$NON-NLS-1$

     this.getMainWindow().open(streamResource, "_blank"); // null pointer exception in this line
    

    }
    }

This is my kind OrdenComprasNuevas, the problem lies in this case, if I remove the argument that this underlined the PDF creation routine does not work for me for the simple reason that I’m using reflection to load the class I’m using in my menu, however I want to know how I can send this parameter for the class to use it without affecting my dynamic menu code.

When I use reflection to load my classes I could send this story is here where I need help.

import com.provinfor.app.modinventario.interfaz.Principal;
import com.vaadin.data.Property;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.VerticalLayout;

/**
*

  • @author rolando
    */
    public class OrdenComprasNueva extends VerticalLayout implements Button.ClickListener, Property.ValueChangeListener {
    /**/

    Principal principal;
    Button btnviewpdf = new Button(“View PDF”);

    public OrdenComprasNueva(Principal app) {
    principal = app;
    btnviewpdf.addListener(new Button.ClickListener() {

         public void buttonClick(ClickEvent event) {
           createpdf(obj1, lst1);
         }
     });
    

    }
    }

    public void createpdf(Object obj1, List lst1) {
    final OrdenComprasPDFStreamSource streamSource = new OrdenComprasPDFStreamSource();
    streamSource.ordencompras(obj1, lst1);
    streamSource.OnlyView(false);
    principal.openPdf(streamSource, strordencompra);

    }

Class that creates the pdf

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import com.provinfor.app.modinventario.OrdenCompras;
import com.vaadin.terminal.StreamResource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.List;

/**
*

  • @author rolando
    */
    @SuppressWarnings(“serial”)
    public class OrdenComprasPDFStreamSource_1 implements StreamResource.StreamSource {

    public void OnlyView(boolean vista) {
    OnlyOpen = vista;
    }

    public void ordencompras(OrdenCompras ofacts, List lstordencomprasdetalles) {
    oordencompras = new OrdenCompras();
    this.oordencompras = ofacts;
    this.lstordencomprasdetalles = lstordencomprasdetalles;

    }

    @Override
    public InputStream getStream() {

     try {
         PipedInputStream in = new PipedInputStream();
         final PipedOutputStream out = new PipedOutputStream(in);
    
         new Thread(new Runnable() {
    
             @Override
             public void run() {
                 try {
                     writePdf(out);
    
                 } catch (Throwable e) {
                     throw new RuntimeException(e);
                 }
             }
         }).start();
         return in;
     } catch (Throwable e) {
         throw new RuntimeException(e);
     }
    

    }

    protected void writePdf(PipedOutputStream out) throws IOException, DocumentException {
    FileOutputStream fos = null;
    Document document = new Document();
    PdfWriter.getInstance(document, out);

         PdfWriter.getInstance(document, fos);
              document.setHeader(header);
         document.open();
         //body of the pdf
         document.add(table);
         document.close();
     } catch (Exception ex) {
     }
    

    }

    }

Saludos