Results 1 to 11 of 11
Thread: Null point exception Error
- 11-25-2009, 05:19 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
Null point exception Error
i am using this following code to compare to columns from two different tables
but i am getting null point exception because some of my fields in columns
Like first_name is empty in few rows!!!
are empty!!!
how can i go beyond those null values and could print whatever there in the database!
my code!!
SpySearchServlet.java
Java Code:import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.util.*; public class SpySearchServlet extends HttpServlet{ public void doPost(HttpServletRequest pRequest , HttpServletResponse pResponse)throws ServletException,java.io.IOException { Connection Con = null; List<Map> lSearchList = new ArrayList<Map>(); try{ List<Map> SearchCriteriaList = new ArrayList<Map>(); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Con = DriverManager.getConnection("jdbc:odbc:orcl","test", "test"); Statement Stat=Con.createStatement(); ResultSet Res = Stat.executeQuery("Select SORT_KEY,FIRST_NAME,SECOND_NAME from NegativeList"); while (Res.next()){ Map lMap = new HashMap(); lMap.put("FIRST_NAME",Res.getString("FIRST_NAME")); lMap.put("SECOND_NAME",Res.getString("SECOND_NAME")); lMap.put("SORT_KEY",Res.getString("SORT_KEY")); SearchCriteriaList.add(lMap); } Stat.close(); //Res.close(); PreparedStatement SearchQuery = Con.prepareStatement(" Select * from EmpInfo where Long_Name like ? or Long_Name like ? or Long_Name like ?"); for ( int IntI = 0 ; IntI < SearchCriteriaList .size(); IntI ++ ){ Map CriteriaMap = SearchCriteriaList.get(IntI); System.out.println("Searching for "+ CriteriaMap ); SearchQuery.setString(1,"%"+CriteriaMap.get("FIRST_NAME").toString()+"%"); SearchQuery.setString(2,"%"+CriteriaMap.get("SECOND_NAME").toString()+"%"); SearchQuery.setString(3,"%"+CriteriaMap.get("SORT_KEY").toString()+"%"); ResultSet Resl = SearchQuery.executeQuery(); while(Resl.next() ){ Map lInfoMap = new HashMap(); lInfoMap.putAll(CriteriaMap); lInfoMap.put("Long_Name" ,Resl.getString("Long_Name")); lInfoMap.put("Emp_Id" ,Resl.getString("Emp_Id")); lSearchList.add(lInfoMap); } //Resl.close(); } System.out.println(lSearchList ); }catch(Exception e){ e.printStackTrace(); }finally{ try{Con.close();}catch(Exception e){} } RequestDispatcher Dispatcher = pRequest.getRequestDispatcher("SpySearch.jsp"); pRequest.setAttribute("SearchList",lSearchList); Dispatcher .forward(pRequest,pResponse); }//end of Post }
throwing null point exception in 34th line!!
otherwise the code is working fine!!!Last edited by morya123; 11-25-2009 at 05:57 AM.
-
Which line is the 34th? Also, could you kindly back off on the bold and exclamation marks? It makes your post a bit difficult to read.
- 11-25-2009, 05:58 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
lMap.put("SECOND_NAME",Res.getString("SECOND_NAME" ));
this is the line!
-
OK! That is the line!this is the line!
But are you absolutely sure that this line is throwing a NullPointerException and not another line, or not throwing an SQLException?
- 11-25-2009, 08:53 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
yes i am sure because in my database second_name columns are empty in few rows!!!
- 11-25-2009, 09:21 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Just check if the returned value is null or not using an if test before processing.
P.S. I would throw away that ugly List<Map> and create a class to hold the database row data instead. I would also use the JDBC driver and java code naming conventions.
- 11-25-2009, 10:02 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
First off, you're not closing your statements or your resultset in the finally block. Indeed, you're not closing your resultsets at all. This is Not Good.
Secondly, I'm pretty sure this could be done as a single SQL query. I have yet to encounter a "do one query, cycle through the results and do a second query on those" that could not be done as a single query.
Finally, as r035198x says, you should be using a class to represent each row of results, which are then stuck in a List. That Map is horrible.
- 11-25-2009, 10:26 AM #8
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
not clear !!! plzz explain!!!
another thing is that, if i remove second_name and only compare other columns !!! code is working fine!!!!!
- 11-25-2009, 10:39 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Nice collection of exclamation marks.
Are you sure the null pointer exception isn't being thrown here:
?Java Code:SearchQuery.setString(2,"%"+CriteriaMap.get("SECOND_NAME").toString()+"%");
This is where I would expect it to be, and it is (give or take a line) line 34 of what you posted above? If SECOND_NAME is null (that is the entry in the Map is null) then the toString call can't happen, which would be your NPE. So stick a check around it, or remove the toString().
ETA: Of course if each of your results were in a nice class of their own you could write your own toString() which would do all this for you.
Oh, and looking at that code again I might be wrong about the single SQL. Though I'm not entirely sure what it is you're trying to do.Last edited by Tolls; 11-25-2009 at 10:41 AM.
- 11-25-2009, 10:49 AM #10
Member
- Join Date
- Nov 2009
- Posts
- 9
- Rep Power
- 0
if u could suggest better one plzzz suggest!!!
actually i am trying to compare two tables!!!
i.e LONG NAME from one table and FIRST_NAME ,SECOND_NAME and sort_key from another table.
- 11-25-2009, 11:22 AM #11
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
null point exception in array lists
By c_walker in forum New To JavaReplies: 3Last Post: 10-17-2009, 05:38 AM -
null pointer exception
By jyothi.priyanka in forum New To JavaReplies: 12Last Post: 03-11-2009, 05:04 PM -
Null Pointer Exception
By ScKaSx in forum New To JavaReplies: 1Last Post: 01-24-2009, 11:27 AM -
null pointer exception
By cityguy503@yahoo.com in forum New To JavaReplies: 4Last Post: 08-22-2008, 07:22 PM -
Null pointer exception error
By brownie_jedi in forum New To JavaReplies: 3Last Post: 03-15-2008, 06:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks