0

I'm creating custom HTML reports for my selenium framework. Now, when I'm trying to add a screenshot link to my HTML page, I get 'NULL pointer Exception'. Could someone point me where I'm going wrong.

This is my code to write to a HTML file.

public static void writeResults(String SummaryHTMLFile) {
        FileOutputStream out; // declare a file o/p object
        PrintStream print = null;   // declare a print stream object

        try {

            out = new FileOutputStream(SummaryHTMLFile);
            print = new PrintStream(out);

            String reportIn = "";
            for (int i = 0; i < details.size();i++) {

                reportIn = "<html><head><title>Automation Execution Results</title>";

                reportIn +="</head><Body>"+
                        "<p align = center><table border=2 bordercolor=#000000 id=table1 width=900 height=31 bordercolorlight=#000000>"+
                        "<tr><td COLSPAN = 6 bgcolor = "+h1color+">";
                reportIn+= "<p align=center><font color="+fontColor+" size=4 face= Copperplate Gothic Bold>"+"Framwork"+" Automation Execution Results </font><font face= Copperplate Gothic Bold></font> </p>";
                reportIn +="</td></tr>"+
                        "<tr>"+
                        "<td COLSPAN = 6 bgcolor = "+h1color+">"+
                        "<p align=justify><b><font color="+fontColor+" size=2 face= Verdana>DATE:"+ currentDate+
                        "</td></tr>";
                reportIn+="<tr bgcolor="+h2color+">"+
                        "<td><b>Step No</b></td>"+
                        "<td><b>Step Name</b></td>"+
                        "<td><b>Description </b></td>"+
                        "<td><b>Status</b>  </td>"+
                        "<td><b>Screen Shot </b></td>"+
                        "</tr>";

                reportIn+="<tr><td>" + Integer.toString(i+1) +"</td>"+
                        "<td>" + details.get(i).getStepName() + "</td>"+
                        "<td>" + details.get(i).getDesc() + "</td>" +
                        "<td>" + details.get(i).getStrStatus()+ "</td>" +
                        "<td><a href=" + new File(details.get(i).getResultScreenshot()).toURI().toURL() + ">screenshot</a></td></tr>" +resultPlaceholder;

            print.println(reportIn);
                print.close();
            }


        } catch (Exception e) {
            System.out.println("Error when writing report file:\n" + e.toString());
        }
    }

This is the line where I get the Null Pointer exception. If I remove this line, the file gets generated successfully.

"<td><a href=" + new File(details.get(i).getResultScreenshot()).toURI().toURL() + ">screenshot</a></td></tr>" +resultPlaceholder;

EDIT: There is a result class which has getters & setters to retrieve the results.

package com.reporting;

public class Result {

    private String result;
    private String resultText;
    private String stepName;
    private String Desc;
    private String strStatus;
    private String resultScreenshot;

    public Result(String resultText,String result) {
        this.result = result;
        this.resultText = resultText;
    }

    public Result(String strStepName,String strDescription,String strStatus, String resultScreenshot) {
        this.setStepName(strStepName);
        this.setDesc(strDescription);
        this.setStrStatus(strStatus);
        this.setResultScreenshot(resultScreenshot);
        //this.resultScreenshot = resultScreenshot;
    }

    public Result(String strStepName,String strDescription,String strStatus) {
        this.setStepName(strStepName);
        this.setDesc(strDescription);
        this.setStrStatus(strStatus);
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getResult() {
        return this.result;
    }

    public void setResultText(String resultText) {
        this.resultText = resultText;
    }

    public String getResultText() {
        return this.resultText;
    }

    public String getStepName() {
        return stepName;
    }

    public void setStepName(String stepName) {
        this.stepName = stepName;
    }

    public String getDesc() {
        return Desc;
    }

    public void setDesc(String desc) {
        Desc = desc;
    }

    public String getStrStatus() {
        return strStatus;
    }

    public void setStrStatus(String strStatus) {
        this.strStatus = strStatus;
    }   
    public void setResultScreenshot(String resultScreenshot) {
        this.resultScreenshot = resultScreenshot;
    }

    public String getResultScreenshot() {
        return this.resultScreenshot;
    }
}
AdiBoy
  • 145
  • 7
  • 20
  • What is `details` here? it's hard to decode what are these functions : `getResultScreenshot())` `toURI()` `toURL()` – Kushal Bhalaik Apr 14 '17 at 08:44
  • @kushal. Edited my question. getResultScreenshot is got from the Results class, which I've added above – AdiBoy Apr 14 '17 at 08:54
  • What about `details`? – Kushal Bhalaik Apr 14 '17 at 09:16
  • details is neither declared nor passed, so I was wondering the same thing. – Bill Hileman Apr 14 '17 at 14:28
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – takendarkk Apr 14 '17 at 15:43
  • It seems to me that you're meaning to include a text string version of the URL where a screenshot is stored, but I see no code that actually takes, and stores, the screenshot. Is there more code that populates the Result class array that we need to see? – Bill Hileman Apr 14 '17 at 17:04
  • By the way, if you put an e.printStackTrace(); after your text message in the catch block, you'll see that it's actually reporting two null pointer exceptions in a row. They are almost certainly related, however. – Bill Hileman Apr 14 '17 at 17:06

1 Answers1

0

Based on your code, I think the issue is in getResultScreenshot() method. It returns null, and when you try to invoke toURI() method it throws NPE.

You should check why it returns null, and maybe add handler for such case. Somethind like put if in the getResultScreenshot to return empty / default image url. Or you could put if to check only construct the link to screenshot when there is value

for example

if(details.get(i).getResultScreenshot() != null){


     reportIn+="<td><a href=" + new File(details.get(i).getResultScreenshot()).toURI().toURL() + ">screenshot</a></td></tr>" +resultPlaceholder;

}
adee alamsz
  • 393
  • 2
  • 6