Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7399487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:04:00+00:00 2026-05-29T04:04:00+00:00

I was examining the use of ServletContext when i got the null pointer exception.

  • 0

I was examining the use of ServletContext when i got the null pointer exception. I don’t understand why do i get this exception.

I have set the attribute in the context object from one class and then try to retrieve that from the second class using getAttribute(...).

package ServletContext; // servlet1

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

public class servlet1 extends HttpServlet {
  public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
    LinkedList list = new LinkedList();
    list.add("suhail");
    ServletContext servletContext = getServletContext();
    servletContext.setAttribute("name", list);
  }
}

package ServletContext;  // servlet2

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

public class servlet2 extends HttpServlet {
 public void doGet(HttpServletRequest request , HttpServletResponse response) throws IOException,ServletException {
    LinkedList list2 = new LinkedList();
    ServletContext context = getServletContext();
    list2 = (LinkedList)context.getAttribute("name");
    PrintWriter writer = response.getWriter();
    response.setContentType("text/plain");
    writer.println(list2.pop()); //**15th statement**
  }
}

Exception is :

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
ServletContext.servlet2.doGet(servlet2.java:15)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.11 logs.

Why am i getting this exception ? list should be initialized in servlet2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:04:01+00:00Added an answer on May 29, 2026 at 4:04 am

    list2 is null there – you can’t invoke methods on null. Perhaps you still haven’t put the list in the servlet context? A null-check (if (list != null)) would fix the exception, but make sure you are properly putting the list in the context before you invoke the 2nd servlet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am examining a core dump, and noticed that in one frame the 'this'
We have a 32-bit .NET application which makes use of the 32-bit version of
I have a set of Berkeley DB files on my Linux file system that
I have an XML document that is digitally signed. I use the XML Digital
I have been slowly examining all of the features that F# brings to the
I am examining example Blog example of CouchDB. I use Ektorp at Spring for
I've been examining the Thematic Theme Framework for WordPress (http://wordpress.org/extend/themes/thematic). I just got a
Examining bytecode, I've noticed javac seems to duplicate checkcast instructions when casting to array
I'm examining the feasibility of porting an existing Windows MFC control to OS X/Carbon.
Our environment: Drupal+MySQL Examining the query log indicates that the following query, originating from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.