Results 1 to 11 of 11
  1. #1
    morya123 is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default 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 06:57 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    morya123 is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    lMap.put("SECOND_NAME",Res.getString("SECOND_NAME" ));


    this is the line!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    this is the line!
    OK! That is the line!

    But are you absolutely sure that this line is throwing a NullPointerException and not another line, or not throwing an SQLException?

  5. #5
    morya123 is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    yes i am sure because in my database second_name columns are empty in few rows!!!

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    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.

  8. #8
    morya123 is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    not clear !!! plzz explain!!!

    another thing is that, if i remove second_name and only compare other columns !!! code is working fine!!!!!

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    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 11:41 AM.

  10. #10
    morya123 is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    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. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Which of the advices given in this thread so far did you not understand?

Similar Threads

  1. null point exception in array lists
    By c_walker in forum New To Java
    Replies: 3
    Last Post: 10-17-2009, 06:38 AM
  2. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 06:04 PM
  3. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:27 PM
  4. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 08:22 PM
  5. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 07:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •