srikit2014
srikit2014

Reputation: 45

Java Compilation error: New instance ignored

This is my java code for creating a pdf document using itext.

package com.cdac.pdfparser;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class PDFCreate {
    public static String RESULT = "results/part1/chapter01/";
    public static void main(String[] args)
    throws DocumentException, IOException {
        Scanner sc = new Scanner(System.in);
        String fileName = sc.nextLine();
        RESULT = RESULT + fileName;
        new PDFCreate.createPdf(RESULT);
    }
    public void createPdf(String filename)
    throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World!"));
        // step 5
        document.close();
    }
}

But I'm getting a compilation error: New instance ignored

Please help me out...

Upvotes: 2

Views: 964

Answers (2)

Simmant
Simmant

Reputation: 1513

i change new PDFCreate.createPdf(RESULT) with new PDFCreate().createPdf(RESULT);

new PDFCreate.createPdf(RESULT) is not right way to create object in java.

hope it work

 package com.cdac.pdfparser;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;




  public class PDFCreate {
    public static String RESULT = "results/part1/chapter01/";
    public static void main(String[] args)
    throws DocumentException, IOException {
     Scanner sc = new Scanner(System.in);
     String fileName = sc.nextLine();
     RESULT = RESULT + fileName;
     new PDFCreate().createPdf(RESULT);
    }
     public void createPdf(String filename)
    throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World!"));
        // step 5
       document.close();

} }

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

    new PDFCreate.createPdf(RESULT);
          -------^

That is not the correct way to create an Object.

Should be

 new PDFCreate().createPdf(RESULT);

You forgot to write ().

Upvotes: 4

Related Questions