This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

problem in session

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 67343
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that Stefan's inversion of the expression factors is the cleaner solution.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!