my dog learned polymorphism*
The moose likes JSP and the fly likes problem in session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "problem in session" Watch "problem in session" New topic
Author

problem in session

biswajit ray
Ranch Hand

Joined: Nov 30, 2005
Posts: 30
i have two dropdown box in a page.one dropdown bow is headed as category and another dropdown box headed as subcategory.below the category i have an add button.and below the subcategory i have an another add buton.

the code which i have written is that

if(request.getParameter("N1").equalsIgnoreCase("ADD"))
{
str0=request.getParameter("N1");
System.out.println("the value of str0 is"+str0);
str1=request.getParameter("D1");
System.out.println("the value of str1 is"+str1);

}

if(request.getParameter("D1").equalsIgnoreCase("AllenSolly"))
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/operations","","");
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from category where categoryname='"+str00+"'");
if(rs.next())
{
catid=rs.getInt(2);
CATEGORYID=Integer.toString(catid);
System.out.println("THE CATEGORYID IS"+CATEGORYID);

session.setAttribute("ALLENSOLLY",CATEGORYID);
System.out.println(" NOW THE CATEGORYID IS"+CATEGORYID);
//application.setAttribute("ALLENSOLLY",CATEGORYID);
//pageContext.setAttribute("ALLENSOLLY", CATEGORYID, PageContext.APPLICATION_SCOPE);

session.setAttribute("INFORMATION",information0);
System.out.println(" NOW you are here");
}

}catch(Exception e)
{

System.out.println("THE EXCEPTION IS"+e);
}

}
this is for category

and for similarly for subcategory i have written code



if(request.getParameter("E2").equalsIgnoreCase("pleaseadd"))
{
st00=request.getParameter("E2");
System.out.println("the value of st00 is"+st00);

}

if(request.getParameter("E1").equalsIgnoreCase("men"))
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/operations","","");
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from subcategory where subcategoryname='"+str001+"'");
if(rs.next())
{
parent_id=rs.getInt(2);
PARENT_ID=Integer.toString(parent_id);
System.out.println("The PARENT_ID is"+PARENT_ID);
childid=rs.getInt(3);
CHILD_ID=Integer.toString(childid);
System.out.println("The CHILDID is"+CHILD_ID);
session.setAttribute("PARENT",PARENT_ID);
session.setAttribute("MEN",CHILD_ID);
session.setAttribute("INFORMATION1",information1);
}
}catch(Exception e)
{
System.out.println("THE EXCEPTION IS"+e);
}

}

System.out.println("i m complete>>>>");

%>


but the problem is that whenever i m clicking the add button of category i m getting null pointer excption.

but if i remove the code of 2nd dropdown box it is running correctly.

plz help me in this regard.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
if(request.getParameter("N1").equalsIgnoreCase("ADD"))
if(request.getParameter("D1").equalsIgnoreCase("AllenSolly"))
if(request.getParameter("E2").equalsIgnoreCase("pleaseadd"))

Any of the lines above can potentially create an exception if the parameter is not present in the request - ie request.getParameter(...) returns null.

That is the most likely reason for a null pointer exception in your code.
Your parameter names N1, D1, D2, E1, E2 don't really communicate much about what data they have or which page they are on, so I can't really say much more about it.

One thing you might try is turning the expression around.

if("ADD".equalsIgnoreCase(request.getParameter("N1"))
This trick avoids null pointer exceptions from the parameter being null.
sunitha chekuri
Greenhorn

Joined: Oct 20, 2005
Posts: 8
It is always necessary to check that the value is not null before calling a method on it.

Instead of
if(request.getParameter("N1").equalsIgnoreCase("ADD"))
its better this way
if(request.getParameter("N1")!=null && request.getParameter("N1").equalsIgnoreCase("ADD"))

that would avoid a Nullpointer Exception.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

I think that Stefan's inversion of the expression factors is the cleaner solution.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in session
 
Similar Threads
extracting data from two table and populating them in a dropdownbox
Struts: request parameters and ActionForms
extracting data from two table and populating them in a drop down box
Online Shopping
Multiple forms in a single JSP page