Solved

Java bean gives null pointerexception

Posted on 2003-11-01
9
312 Views
Last Modified: 2010-04-01
Hi,
   I know this is really very very funny.At times my java bean application works and at times it give an exception of null pointer.
I think my problem is that maybe i am not using the get  properly,

I mean, when i use set i use:

<jsp:useBean id="vol" class="com.dhal.volun" scope="session"/>
<jsp:setProperty name="vol" property="*"/>

But i use the getters such as:
       username=vol.getUsername();
       passwd=vol.getPasswd();

Do i need to write something maybe to use it, for example,

jsp:getProperty......

coz, when i look at work folder for the jas_jsp files it seems as if vol is null.

But if write
System.out.println(username);
username gets printed so i am surprise which is null.

Thanks
0
Comment
Question by:Jasbir21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 14

Expert Comment

by:kennethxu
ID: 9664825
the is nothing wrong of the code that u have posted. the problem is in somewhere else, post the full stack trace of you exception.
0
 

Author Comment

by:Jasbir21
ID: 9664907
java.lang.NullPointerException
      org.apache.jsp.jsp2.jas_jsp._jspService(jas_jsp.java:250)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
      org.apache

When  i looked at the line 250,it is about this line
passwd=rs.getString(2).trim();

The java code for query that i use is....
 try
{

   Class.forName("com.mysql.jdbc.Driver").newInstance();
   connection = DriverManager.getConnection(connectionURL, "", "");
   
   statement=connection.createStatement();
   String myquery="select * from account where username='Jasbir22'";
   rs=statement.executeQuery(myquery);
   boolean anyRecords=rs.next();


   String query="select * from volunteer where username='Jasbir22'";
   statement1=connection.createStatement();
   rs1=statement1.executeQuery(query);
   boolean any=rs1.next();


if(rs1.getString(1).trim().equals(rs.getString(1).trim()))
   {
     username=rs.getString(1).trim();
     passwd=rs.getString(2).trim();
     verify=rs.getString(3).trim();
     .......
    .......//its is alot here
     name=rs1.getString(2).trim();
     day=rs1.getString(19).trim();
     month=rs1.getString(20).trim();
     year=rs1.getString(21).trim();

}


  }//try
 
   catch(SQLException ex ) {  ex.printStackTrace() ; }

   catch( ClassNotFoundException ex ){  ex.printStackTrace() ; }


 %>

0
 
LVL 14

Accepted Solution

by:
kennethxu earned 60 total points
ID: 9665009
rs.getString(2) is giving you null.

make sure password is the column number 2 and it is not null for user Jasbir22.
BTW, I'd suggest you to use column name instead of possition, since position might change if you add new columns to database.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Jasbir21
ID: 9665059
Hi,
   I don't understand something, does it mean that a column value cannot be null because if its it would give a null pointer,
sometimes, a user would need not enter a column, soit is null

Thnaks
0
 

Author Comment

by:Jasbir21
ID: 9665154
when i type describle table,

i get
username|password|verifypassword,

So, i guess it is at column 2 ????

How do i do it the column name way

Thanks
0
 
LVL 15

Assisted Solution

by:dualsoul
dualsoul earned 40 total points
ID: 9665270
>So, i guess it is at column 2 ????
columns are counted from ResultSet, not DB schema, but in your it's the same, because you retrive all columns.

>How do i do it the column name way
just use overloading methods in ResultSet:
    String s=rs.getString("password");

0
 

Author Comment

by:Jasbir21
ID: 9665607
When you say column are counted from Resultset,

how are they are counted...

thanks
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9665838
>> When you say column are counted from Resultset.
that's the order in your select statment. it's same if you use select *, but if you use select password, username from..., then the order is password, username.

>> does it mean that a column value cannot be null
NO, the problem is that you are doing a trim(), so if the rs.getString() returns null, you are doing s = null.trim(), of course, you get NPE.
for those columns that could be null in database, you should always use
String s = rs.getString("columnname");
if( s != null ) s=s.trim();
0
 

Author Comment

by:Jasbir21
ID: 9667239
......I have been spending days to find out what my problem, and you guys in experts - exchange solved in seconds.Thank you so much....

Regards,
Jasbir
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Getting to know the threat landscape in which DDoS has evolved, and making the right choice to get ourselves geared up to defend against  DDoS attacks effectively. Get the necessary preparation works done and focus on Doing the First Things Right.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question