You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Adam Jordens <ad...@success-info.com> on 2001/05/07 19:21:40 UTC

weird velocity issue

I'm working with iPlanet Application Server 6.0 SP2 with iWS.

I am having a few problems getting velocity working properly.  I was unable
to get things functioning using a VelocityServlet so I have attempted
to use velocity in a standard HttpServlet sense (ie.  similar to using
velocity in its standalone form)

I have two versions of my code,  one in servlet and another as a command
line app.  Basically, the command line version works, but the servlet
version returns a NullPointerException when trying to Velocity.Evaluate().

Any suggestions or recommendations ?


Here is the servlet stack trace:

java.lang.NullPointerException
	at org.apache.velocity.runtime.Runtime.parse(Runtime.java:590)
	at org.apache.velocity.app.Velocity.evaluate(Velocity.java:259)
	at org.apache.velocity.app.Velocity.evaluate(Velocity.java:230)
	at WorkServlet.doGet(WorkServlet.java:45)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:865)
	at com.netscape.server.servlet.servletrunner.ServletInfo.service(Unknown
Source)
	at com.netscape.server.servlet.servletrunner.ServletRunner.execute(Unknown
Source)
	at com.kivasoft.applogic.AppLogic.execute(Unknown Source)
	at com.kivasoft.applogic.AppLogic.execute(Unknown Source)
	at com.kivasoft.thread.ThreadBasic.run(Native Method)
	at java.lang.Thread.run(Thread.java:479)

Here is the source code for both:


Command Line Version
-----------------------------------
import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;

import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

import java.io.*;
import java.util.ArrayList;

public class Simple
{
    public Simple()
    {
        try
        {
            try {
              Velocity.init("e:/velocity.properties");
            } catch (Exception e) {
              System.out.println("Init Error: " + e);
            }

            VelocityContext context = new VelocityContext();

            Template template =  null;

            FileInputStream fs = new
FileInputStream("e:/templates/helloWorld.vm");
            DataInputStream dis = new DataInputStream(fs);

            BufferedReader d
              = new BufferedReader(new InputStreamReader(fs));

            String temp = "";
            while (dis.available() != 0) {
              temp = temp + dis.readLine();
            }

            context.put("name", "blah");

            StringWriter sw = new StringWriter();
            try {
              Velocity.evaluate(context, sw, "test", temp);
            } catch (Exception e) {
              System.out.println("Evaluate Error: " + e);
            }

            System.out.println(sw);

        }
        catch( Exception e )
        {
            System.out.println(e);
        }
    }

    public static void main(String[] args)
    {
        Simple t = new Simple();
    }
}



Servlet Version
-------------------------
import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Properties;

import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

public class WorkServlet extends HttpServlet{

   protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws IOException {

      PrintWriter pw = response.getWriter();

      try {
        Velocity.init("e:/velocity.properties");
      } catch (Exception e) {
        pw.println("Init Error: " + e);
      }

      VelocityContext context = new VelocityContext();

      Template template =  null;

      FileInputStream fs = new
FileInputStream("e:/templates/helloWorld.vm");
      DataInputStream dis = new DataInputStream(fs);

      BufferedReader d
        = new BufferedReader(new InputStreamReader(fs));

      String temp = "";
      while (dis.available() != 0) {
        temp = temp + dis.readLine();
      }

      context.put("name", "blah");

      StringWriter sw = new StringWriter();
      try {
        Velocity.evaluate(context, sw, "test", temp);
      } catch (Exception e) {
        e.printStackTrace(pw);
//        pw.println("Evaluate Error: " + e);
      }

      pw.println(sw);
  }

}


Re: weird velocity issue

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Adam Jordens wrote:
> 
> I'm working with iPlanet Application Server 6.0 SP2 with iWS.
> 
> I am having a few problems getting velocity working properly.  I was unable
> to get things functioning using a VelocityServlet so I have attempted
> to use velocity in a standard HttpServlet sense (ie.  similar to using
> velocity in its standalone form)
> 
> I have two versions of my code,  one in servlet and another as a command
> line app.  Basically, the command line version works, but the servlet
> version returns a NullPointerException when trying to Velocity.Evaluate().
> 
> Any suggestions or recommendations ?

It looks like velocity wasn't initialized. I see from the code below
that it is, but are you really sure the code you are showing is the code
that's running?

Can you delete any velocity.log files you may have, and restart the
servlet runner to make sure ?

The line it's bombing on, and I assume this is v1.0.1, indicates that
the parserPool is still null, therefore it hasn't been initialized.

let us know... :)

geir

> 
> Here is the servlet stack trace:
> 
> java.lang.NullPointerException
>         at org.apache.velocity.runtime.Runtime.parse(Runtime.java:590)
>         at org.apache.velocity.app.Velocity.evaluate(Velocity.java:259)
>         at org.apache.velocity.app.Velocity.evaluate(Velocity.java:230)
>         at WorkServlet.doGet(WorkServlet.java:45)
>         at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
>         at javax.servlet.http.HttpServlet.service(HttpServlet.java:865)
>         at com.netscape.server.servlet.servletrunner.ServletInfo.service(Unknown
> Source)
>         at com.netscape.server.servlet.servletrunner.ServletRunner.execute(Unknown
> Source)
>         at com.kivasoft.applogic.AppLogic.execute(Unknown Source)
>         at com.kivasoft.applogic.AppLogic.execute(Unknown Source)
>         at com.kivasoft.thread.ThreadBasic.run(Native Method)
>         at java.lang.Thread.run(Thread.java:479)
> 
> Here is the source code for both:
> 
> Command Line Version
> -----------------------------------
> import org.apache.velocity.app.Velocity;
> import org.apache.velocity.VelocityContext;
> import org.apache.velocity.Template;
> 
> import org.apache.velocity.exception.ParseErrorException;
> import org.apache.velocity.exception.ResourceNotFoundException;
> 
> import java.io.*;
> import java.util.ArrayList;
> 
> public class Simple
> {
>     public Simple()
>     {
>         try
>         {
>             try {
>               Velocity.init("e:/velocity.properties");
>             } catch (Exception e) {
>               System.out.println("Init Error: " + e);
>             }
> 
>             VelocityContext context = new VelocityContext();
> 
>             Template template =  null;
> 
>             FileInputStream fs = new
> FileInputStream("e:/templates/helloWorld.vm");
>             DataInputStream dis = new DataInputStream(fs);
> 
>             BufferedReader d
>               = new BufferedReader(new InputStreamReader(fs));
> 
>             String temp = "";
>             while (dis.available() != 0) {
>               temp = temp + dis.readLine();
>             }
> 
>             context.put("name", "blah");
> 
>             StringWriter sw = new StringWriter();
>             try {
>               Velocity.evaluate(context, sw, "test", temp);
>             } catch (Exception e) {
>               System.out.println("Evaluate Error: " + e);
>             }
> 
>             System.out.println(sw);
> 
>         }
>         catch( Exception e )
>         {
>             System.out.println(e);
>         }
>     }
> 
>     public static void main(String[] args)
>     {
>         Simple t = new Simple();
>     }
> }
> 
> Servlet Version
> -------------------------
> import org.apache.velocity.app.Velocity;
> import org.apache.velocity.VelocityContext;
> import org.apache.velocity.Template;
> 
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.io.*;
> import java.util.Properties;
> 
> import org.apache.velocity.exception.ParseErrorException;
> import org.apache.velocity.exception.ResourceNotFoundException;
> 
> public class WorkServlet extends HttpServlet{
> 
>    protected void doGet(HttpServletRequest request,
>             HttpServletResponse response) throws IOException {
> 
>       PrintWriter pw = response.getWriter();
> 
>       try {
>         Velocity.init("e:/velocity.properties");
>       } catch (Exception e) {
>         pw.println("Init Error: " + e);
>       }
> 
>       VelocityContext context = new VelocityContext();
> 
>       Template template =  null;
> 
>       FileInputStream fs = new
> FileInputStream("e:/templates/helloWorld.vm");
>       DataInputStream dis = new DataInputStream(fs);
> 
>       BufferedReader d
>         = new BufferedReader(new InputStreamReader(fs));
> 
>       String temp = "";
>       while (dis.available() != 0) {
>         temp = temp + dis.readLine();
>       }
> 
>       context.put("name", "blah");
> 
>       StringWriter sw = new StringWriter();
>       try {
>         Velocity.evaluate(context, sw, "test", temp);
>       } catch (Exception e) {
>         e.printStackTrace(pw);
> //        pw.println("Evaluate Error: " + e);
>       }
> 
>       pw.println(sw);
>   }
> 
> }

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

Re: weird velocity issue

Posted by Jon Stevens <jo...@latchkey.com>.
on 5/7/01 10:21 AM, "Adam Jordens" <ad...@success-info.com> wrote:

>     FileInputStream fs = new
> FileInputStream("e:/templates/helloWorld.vm");
>     DataInputStream dis = new DataInputStream(fs);
> 
>     BufferedReader d
>       = new BufferedReader(new InputStreamReader(fs));
> 
>     String temp = "";
>     while (dis.available() != 0) {
>       temp = temp + dis.readLine();
>     }

What the heck is this code?

Take a look at:

    java.io.FileReader

-jon

-- 
If you come from a Perl or PHP background, JSP is a way to take
your pain to new levels. --Anonymous
<http://jakarta.apache.org/velocity/ymtd/ymtd.html>