Re. Null Pointer Exception.

S

Steve R. Burrus

I know that SOME of you think that I am being "crazy" and foolhardy by still
posting my problem about getting that "java.lang.NullPointerException"
almost every time that I try/attempt to view a servlet image in my browser,
but I swear, I have looked OVER AND OVER at the lines in the servlet file
which I suspect could be giving me this server error, but I honestly could
not find the problem! Truth In Representation : I HAVE been able to view
just a few of the images successfully, but I really don't know why. Well,
with my apologies to those of you who don't feel like I need again the
servlet code, I humbly re-submit it to you :

package com.laundry.gal;

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

public class LuvThisGal extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws
IOException,ServletException {
response.setContentType("image/jpg");


ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/GirlAtSink.jpeg");

int read = 0;
byte[] bytes = new byte[1024];

OutputStream os = response.getOutputStream();

while((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
response.sendError(500,"Hey! Your Servlet Developed A" +
"NullPointerException.");

}
os.flush();
os.close();

}
}
 
K

kjc

Steve said:
I know that SOME of you think that I am being "crazy" and foolhardy by still
posting my problem about getting that "java.lang.NullPointerException"
almost every time that I try/attempt to view a servlet image in my browser,
but I swear, I have looked OVER AND OVER at the lines in the servlet file
which I suspect could be giving me this server error, but I honestly could
not find the problem! Truth In Representation : I HAVE been able to view
just a few of the images successfully, but I really don't know why. Well,
with my apologies to those of you who don't feel like I need again the
servlet code, I humbly re-submit it to you :

package com.laundry.gal;

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

public class LuvThisGal extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws
IOException,ServletException {
response.setContentType("image/jpg");


ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/GirlAtSink.jpeg");

int read = 0;
byte[] bytes = new byte[1024];

OutputStream os = response.getOutputStream();

while((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
response.sendError(500,"Hey! Your Servlet Developed A" +
"NullPointerException.");

}
os.flush();
os.close();

}
}
first put your code inside a try catch block.
Second, in your catch block, print a stack trace.
That will tell you exactly where the problem is.
 
K

klynn47

I have a question about your code. I really don't much about servlets,
but I'm wondering about the call to response.sendError inside the while
loop. Does this cause the NullPointerException to be displayed after
you write the first array of bytes?
 
P

Patricia Shanahan

Steve said:
I know that SOME of you think that I am being "crazy" and
foolhardy by still posting my problem about getting that
"java.lang.NullPointerException" almost every time that
I try/attempt to view a servlet image in my browser, but
I swear, I have looked OVER AND OVER at the lines in the
servlet file which I suspect could be giving me this
server error, but I honestly could not find the problem!
Truth In Representation : I HAVE been able to view just a
few of the images successfully, but I really don't know
why. Well, with my apologies to those of you who don't
feel like I need again the servlet code, I humbly
re-submit it to you :

The real problem is that you keep reposting this, with
different subject lines, without any indication that you
have read the numerous replies, or providing the additional
information they indicated was needed. "It's still about the
NPE." was a bit better than this one, because you included
the stack trace. However, I didn't see a reply to e.g.:
Almost there. Now you just need to tell us which line is
22. (If the package statement were the first line, 22
would consist of just a closing brace, which seems
unlikely.)

(From Mike Schilling's reply in that thread.)

I have some general advice on how to debug at
http://home.earthlink.net/~patricia_shanahan/debug/. I do
not recommend simply looking over the lines unless that
works very quickly. You need to start a logical, structured
debug effort.

The very first step, as you have already been told, is to
identify the line at which the exception happened. We don't
know which line it is. You should.

Once you have done that, list the pointers that are
dereferenced on that line. One of them is null when the NPE
is thrown. If there is more than one dereferenced pointer on
the line, use a debugger or println to find out which is
null on failure.

Once you have identified the null pointer, you need to
decide what to do about it.

Patricia
 
A

Anton Spaans

Steve R. Burrus said:
I know that SOME of you think that I am being "crazy" and foolhardy by still
posting my problem about getting that "java.lang.NullPointerException"
almost every time that I try/attempt to view a servlet image in my browser,
but I swear, I have looked OVER AND OVER at the lines in the servlet file
which I suspect could be giving me this server error, but I honestly could
not find the problem! Truth In Representation : I HAVE been able to view
just a few of the images successfully, but I really don't know why. Well,
with my apologies to those of you who don't feel like I need again the
servlet code, I humbly re-submit it to you :

package com.laundry.gal;

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

public class LuvThisGal extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws
IOException,ServletException {
response.setContentType("image/jpg");


ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/GirlAtSink.jpeg");

int read = 0;
byte[] bytes = new byte[1024];

OutputStream os = response.getOutputStream();

while((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
response.sendError(500,"Hey! Your Servlet Developed A" +
"NullPointerException.");

}
os.flush();
os.close();

}
}

The only variable that can possibly be null, as far as i can see right now,
is the 'InputStream is'
The 'is = ctx.getResourceAsStream("/...."); ' statement can return null. See
the java-doc:
=============================================
public java.io.InputStream getResourceAsStream(java.lang.String path)
- Returns the resource located at the named path as an InputStream object.
- The data in the InputStream can be of any type or length. The path must be
specified according to the rules given in getResource. ***This method
returns null if no resource exists at the specified path.***

- Meta-information such as content length and content type that is available
via getResource method is lost when using this method.

- The servlet container must implement the URL handlers and URLConnection
objects necessary to access the resource.

- This method is different from java.lang.Class.getResourceAsStream, which
uses a class loader. This method allows servlet containers to make a
resource available to a servlet from any location, without using a class
loader.

Parameters:
name - a String specifying the path to the resource
Returns:
the InputStream returned to the servlet, or null if no resource exists at
the specified path
=============================================

Check if the resource "/GirlAtSink.jpeg" exists. In short, test if 'is ==
null'!!!
-- Anton Spaans.
 
P

P.Hill

Steve said:
I know that SOME of you think that I am being "crazy" and foolhardy by still
posting my problem about getting that "java.lang.NullPointerException"

Is it possible you have not read any of the replies on the newsgroup
and you are waiting for an e-mail?

Read the replys in the news group, please.

-Paul
 
S

Steve R. Burrus

Can anyone explain to me, to my satisfaction, why it is that I only get the
"java.lang.NullPointerException" on SOME of the images which I try/attempt
to view in my web browser, but on maybe 1 or 2 others I can view
successfully!!!

And I need someone to please explain how to use the FileInputStream (and
the FileOutputStream class also?) with the "getRealPath( )" Java method to
enable me to finally see ALL of the images that I would like to see.

And also, I was told by somebody that I should put the various images into
my Tomcat's (I have the version 5.5.7 of it incidentally) WEB-INF\classes
folder or directory. Is this the right thing to do or not? And how do I go
about making a "Library" pertaining to these images that I was told goes
into the WEB-INF\lib folder anyway? If I have these images in the main
servlet context folder for the web application, are they said to be within
the web app's scope ot not? Frankly people, I am going crazy and downright
out of my mind with sheer frustration because I still haven't done this
right, or at least to my satisfaction at all!!! I will be friends with
someone in our group who can help me with this forever. One more question :
Do I still have to use the ServletContext class when I use the
getRealPath( ) method?
 
P

Patricia Shanahan

Steve said:
Can anyone explain to me, to my satisfaction, why it is
that I only get the "java.lang.NullPointerException" on
SOME of the images which I try/attempt to view in my web
browser, but on maybe 1 or 2 others I can view
successfully!!!

I don't know, but I have a general remark on debug technique
that might help resolve this:

**** You don't need to do it all at once. ****

Nobody expects to go from no code to the complete, final
program for anything non trivial in one step. Programmers
know they are making progress as long as the program becomes
more and more similar to the program they want. Progress can
be passing more test cases, having tests for more of the
required features, nailing down the design for a critical
part of the program, anything that brings the program closer
to the requirements.

In the same way, beyond the most trivial cases, debug is
best treated as a stepwise refinement process. It is often
impossible, or at least unnecessarily difficult, to jump
straight from external symptom to root cause. Going from
"There is a NullPointerException" to "This line sometimes
throws a NullPointerException" is progress. From there to
"When the program fails, this variable is null on arrival at
the line, and gets dereferenced." is another step.

Most of the people responding in your NullPointerException
threads seem to be taking the stepwise refinement view. They
ask questions, or propose theories to test. They expect you
to follow up by finding out the answers to the questions or
testing the theories, and posting additional information. If
you did that, you would get more questions and/or theories
to test, until someone understood the root cause, and the
conversation would switch to a discussion of solutions.

Meanwhile, you seem to expect to leap straight from "This
block of code throws a NullPointerException" to the root
cause, and what to do about it, without going through any
intermediate steps. If you were going to get lucky, and have
someone make the leap for you, it would probably have
happened about three weeks ago. Surely by now it's time to
give the stepwise refinement approach a chance?

Patricia
 
S

Steve R. Burrus

<<snip>>
I am now re-posting only a part of my post from late last nite, and I would
really like to have some specific answers to these particular questions.
Thanx.

And I need someone to please explain how to use the FileInputStream (and
the FileOutputStream class also?) with the "getRealPath( )" Java method to
enable me to finally see ALL of the images that I would like to see.

And also, I was told by somebody that I should put the various images into
my Tomcat's (I have the version 5.5.7 of it incidentally) WEB-INF\classes
folder or directory. Is this the right thing to do or not? And how do I go
about making a "Library" pertaining to these images that I was told goes
into the WEB-INF\lib folder anyway? If I have these images in the main
servlet context folder for the web application, are they said to be within
the web app's scope ot not?
 
S

Steve R. Burrus

Well, I just wanted to tell everybody/everyone that I seemed to get it
right, finally, with being able to view all of the various images in my web
browser with this 1 line of code : >>"FileInputStream fis = new
FileInputStream(ctx.getRealPath([name of image file]));"<< Sorry to be such
a "bother" to some of you people.
 
A

Anton Spaans

Steve R. Burrus said:
Well, I just wanted to tell everybody/everyone that I seemed to get it
right, finally, with being able to view all of the various images in my web
browser with this 1 line of code : >>"FileInputStream fis = new
FileInputStream(ctx.getRealPath([name of image file]));"<< Sorry to be such
a "bother" to some of you people.


Steve R. Burrus said:

Your solution will work in web-applications that are deployed as a
file-directory hierarchy. If the web-app is deployed in a WAR file (and the
WAR file is not being 'exploded' by the web-server/web-container), then
getRealPath() will return null!
The ctx.getResourceAsStream(String pPath) will work in both situations. Just
make sure that the pPath is correct.

ctx.getResourceAsStream("/image.gif") will return the inputstream for the
image file "<web-root>/image.gif".
ctx.getResourceAsStream("/somedir/image.gif") will return the inputstream
for the image file "<web-root>/somedir/image.gif".
Do not put them in or below the "<web-root>/WEB-INF" directory. The
WEB-INF/classes and WEB-INF/lib are only know are 'root'-paths to your
web-server's classloader!

BTW: you were never a "bother" :=)

-- Anton.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top