>From the JavaDocs:

public InputStream getResourceAsStream(String name)

Finds a resource with a given name. This method returns null if no
resource with this name is found. The rules for searching resources
associated with a given class are implemented by the defining class
loader of the class.

I think the problem occurs whereby your file is not found, check to see
if the stream == null first. A solution I have found is

NameOfClass.class.getClassLoader().getResourceAsStream(filename);

NameOfClass is a class that you are using, it worked for me the last
time I used it.

Regards,
Peter Dolukhanov

-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:JSP-INTEREST@;JAVA.SUN.COM] On Behalf Of Edward King
Sent: 07 November 2002 06:49
To: [EMAIL PROTECTED]
Subject: Java code can't run in JSP

I have a question about JSP,I put some Java code into JSP,it like
follows:

<%@page language="java"%>
<%@page import="java.io.*" %>

<html>
<head>
  <title>test</title>
</head>
<body background="images/send.jpg" text="FF0000">
   <%
     String value[]=new String[2];
     String str;
     value[0]="8";
     try
     {

         InputStream is =
this.getClass().getResourceAsStream("version.data");
         BufferedReader input = new BufferedReader(new
InputStreamReader(is));
         str=input.readLine();
     }
     catch(IOException e)
     {
        System.out.println("read file error");
     }
   %>

   ....
</body>
</html>

when I run it,it raise errors:
Description:
   The server encountered an internal error (Internal Server Error) that
prevented it from fulfilling this request

exception:
java.lang.NullPointerException
 at java.io.Reader.(Reader.java:61)
 at java.io.InputStreamReader.(InputStreamReader.java:55)
 at
org.apache.jsp.start_0005fsend$jsp._jspService(start_0005fsend$jsp.java:
75)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)

Then I extract my Java code,and put it into Java file,like follows:

import java.io.*;
public class test8
{
   public static void main(String args[])
   {
       test8 ss=new test8();
       ss.pp();
   }
   public void pp()
   {
       String value[]=new String[2];
       String str;
       value[0]="8";
       try
       {
          InputStream is =
this.getClass().getResourceAsStream("version.data");
          BufferedReader input = new BufferedReader(new
InputStreamReader(is));
          str=input.readLine();
          System.out.println(str);
       }
       catch(IOException e)
       {
          System.out.println("read file error");
       }
  }
}

It run ok.Why my code can't run in JSP and can run in Java? How to
correct to make my Java code run in JSP?

Thanks in advance!
Edward

========================================================================
===
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to