srvOut is defined twice: once at the class level, once as a local variable
in doGet(...). You are initializing the local variable in doGet, but using
the class variable (srvOut) in your printW(...) method. Since the class
level version was never initialized, it has a value of null. My suggestion:
pass srvOut as an argument to the methods that need it, and get rid of the
class level version altogether. This will help you start addressing the
errors Craig referred to.
----- Original Message -----
From: RVASUDEV <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, November 20, 1999 5:28 AM
Subject: Re: Bug in servlet program (still there)
> > Hi,
> > Still getting the NPE.
> > After adding more debugging log messages, I
> > have been able to pinpoint the line throwing
> > the NPE but can't figure out why it does.
> > It's the srvOut.println() call in printW() that's
> > the trouble spot.
> > Advice please ! Error and code below.
> > Also, Craig, could you please elaborate on how to
> > fix the multiple-users-will-make-it-crash problem,
> > using method variables as you said in your earlier reply.
> > Vasudev
> > Error :
> > ======================================================
> > Host : atlas
> > id To1010mC676918158975596At
> > created Sat Nov 20 16:50:39 GMT+05:30 1999
> > lastaccessed Sat Nov 20 16:50:55 GMT+05:30 1999
> > uptime output:
> > 4:51pm up 40 days, 1:08, 24 users, load average: 3.63, 3.15, 2.63
> > who output:
> > Error: 500
> > Internal Servlet Error:
> > java.lang.NullPointerException
> > at SrvStat.printW(SrvStat.java:199)
> > at SrvStat.doGet(SrvStat.java:87)
> > at javax.servlet.http.HttpServlet.service(HttpServlet.java:715)
> ...
> > Code :
> > import java.io.*;
> > import java.text.*;
> > import java.util.*;
> > import javax.servlet.*;
> > import javax.servlet.http.*;
> > public class SrvStat extends HttpServlet {
> > PrintWriter srvOut; // servlet output stream
> > PrintWriter prcOut; // output stream to process input
> > PrintWriter logOut; // output stream for log file for program
> > File logFile; // log file for program
> > BufferedReader prcIn; // input stream from process output
> > Runtime runtime; // to start the process
> > Process process; // a process
> > String command; // command to run and write/read to/from
> > String result;
> > public void doGet(HttpServletRequest request, HttpServletResponse
> > response)
> > throws IOException, ServletException
> > {
> > logFile = new File("/tmp/SrvStat.log");
> > logOut = new PrintWriter( new FileWriter(logFile), true);
> > response.setContentType("text/html");
> > PrintWriter srvOut = response.getWriter();
> > runtime = Runtime.getRuntime();
> > srvOut.println("<html>");
> > srvOut.println("<body bgcolor=\"white\">");
> > srvOut.println("<head>");
> > String title = "Server Status";
> > srvOut.println("<title>" + title + "</title>");
> > srvOut.println("</head>");
> > srvOut.println("<body>");
> > srvOut.println("<h2>Host : " + getHostName() + "</h2>");
> > srvOut.println("<br><hr>");
> > HttpSession session = request.getSession();
> > srvOut.println("<P><hr>");
> > String uptimeOn = request.getParameter("uptime");
> > if (uptimeOn != null) {
> > if (uptimeOn.equals("on")) {
> > srvOut.println("uptime output: <br>" +
> > getUptime() + "<br><hr>");
> > }
> > }
> > String wOn = request.getParameter("w");
> > if (wOn != null) {
> > if (wOn.equals("on")) {
> > logOut.println("Before calling printW");
> > srvOut.println("who output: <br>");
> > printW();
> > srvOut.println("<br><hr>");
> > }
> > }
> > srvOut.println("<P>");
> > srvOut.print("<form action=\"");
> > srvOut.print("SrvStat\" ");
> > srvOut.println("method=GET>");
> > srvOut.println("<input type=checkbox name=uptime>");
> > srvOut.println("uptime");
> > srvOut.println("<br>"); srvOut.println("<input
> > type=checkbox name=w>");
> > srvOut.println("w"); srvOut.println("<br>");
> > srvOut.println("<input type=submit>");
> > srvOut.println("</form>");
> > srvOut.println("</body>"); srvOut.println("</html>");
> > srvOut.close();
> > }
> >
> > String getHostName() throws IOException {
> > try {
> > command = "hostname"; process =
> > runtime.exec(command);
> > prcOut = new PrintWriter( new OutputStreamWriter (
> > process.getOutputStream() ) );
> > prcIn = new BufferedReader( new InputStreamReader
(
> > process.getInputStream() ) );
> > result = prcIn.readLine();
> > }
> > catch(Exception e) {
> > result = "Error executing " + command + ": " +
> > e.getMessage();
> > }
> > return result;
> > }
> > String getUptime() throws IOException {
> > try {
> > command = "sh -c \"who | sed 1q\"";
> > process = runtime.exec(command);
> > prcOut = new PrintWriter( new OutputStreamWriter (
> > process.getOutputStream() ) );
> > prcIn = new BufferedReader( new InputStreamReader
(
> > process.getInputStream() ) );
> > result = prcIn.readLine();
> > }
> > catch(Exception e) {
> > result = "Error executing " + command +
> > ": " + e.getMessage();
> > }
> > finally {
> > prcOut.close();
> > prcIn.close();
> > return result;
> > }
> > }
> > void printW() throws IOException {
> > try {
> > command = "date";
> > process = runtime.exec(command);
> > logOut.println("Before calling getOutputStream");
> > prcOut = new PrintWriter( new OutputStreamWriter (
> > process.getOutputStream() ) );
> > logOut.println("Before calling getInputStream");
> > prcIn = new BufferedReader( new InputStreamReader
(
> > process.getInputStream() ) );
> > result = ""; String line = "";
> > line = prcIn.readLine();
> > srvOut.println("<br>" + line);
> > while (line != null) {
> > srvOut.println("<br>"); // + line);
> > line = prcIn.readLine();
> > }
> > }
> > catch(Exception e) {
> > srvOut.println("Error executing " + command +
> > ": " + e.getMessage());
> > logOut.println("Leaving catch clause");
> > }
> > finally {
> > try {
> > prcOut.close();
> > prcIn.close();
> > }
> > catch(Exception e) {
> > e.getMessage());
> > }
> > }
> > }
> > }
> >
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html