Results 1 to 6 of 6
  1. #1
    jessie is offline Member
    Join Date
    Nov 2010
    Posts
    29
    Rep Power
    0

    Default Null pointer exception

    Hi,

    this is my code..when i try to insert some data in to my db im getting a null pointer exception...i dont understand the reason.... im using an API for db manupulation. error comes from the scrap_all_text method at the line c1.setDate(timeStampfromDate);

    pls help

    Java Code:
    
    
    
    import java.sql.Timestamp;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Scraper {
    	
    	static final int LIMIT_JAVASCRIPT_KEYWORDS = 2;	//If after processing, the input contains more than this 
    													//Javascript keywords, scraping fails.
    	
    	Aqua _aqua1;
            AquaArticle c1;
    	public Scraper() throws Exception {
    
    
             _aqua1 = new Aqua("E:/projects/cnn/src/cnn/aqua__1.settings");
           
    
            
            }
    	
    	
    
    
    	
    	private String [B]scrap_alltext([/B]String html) throws Exception {
                
    
    
    		char[] buf_in = html.toCharArray();
    		
    		StringBuffer out = new StringBuffer(100000);
    		
    		//System.out.println(html_final);
    		
                    int hstart = html.indexOf("<h1");
                    int hend = html.indexOf("</h1");
                     int summarystart;
                    int summaryend;
                   
                                 
    		
    
    
                   Pattern p = Pattern.compile("\\{document.write\\('\\w+\\s\\d+,\\s\\d+\\s\\d+:\\d+\\s\\w.\\w.");
                   Matcher m = p.matcher(html);
                     DateFormat formatter ;
                Date date, date1 ;
                   if(m.find()){
                       String datetime = m.group();
                       datetime = datetime.replaceAll("\\{document.write\\('", "");
                      // System.out.println(datetime);
                        out.append(datetime+"\n");
                    
                       String str_from_date="05-08-10";
    
    
                formatter = new SimpleDateFormat("dd-MM-yy");
                date = (Date)formatter.parse(str_from_date);
    
                Timestamp timeStampfromDate = new Timestamp(date.getTime());
                System.out.println(timeStampfromDate);
                c1.setDate(timeStampfromDate);
    
    
                       
                   }
    
    		
                   
                    Pattern p3 = Pattern.compile("<h1>(.*)</h1>");
                   Matcher m3 = p3.matcher(html);
                   if(m3.find()){
                       String new3 = m3.group();
                        new3 = new3.replaceAll("<h1>","");
                         new3 = new3.replaceAll("</h1>","");
    
    
                        out.append("\n"+new3);
    
                       c1.setTitle(new3);
                   }
    
          /*          Pattern p2 = Pattern.compile("\\STORY HIGHLIGHTS</b></div><ul class=\\p{Punct}cnn_bulletbin cnnStryHghLght\\p{Punct}><!--\\sgoogle_ad_section_start\\s-->(<li>(.*)<br />)+");
                   Matcher m2 = p2.matcher(html);
                   String temp[];
                   while(m2.find()){
                      String new2 = m2.group();
                      //new2 = new2.replaceAll("<b>","");
                     //  new2 = new2.replaceAll("</b>","");
                        new2 = new2.replaceAll("STORY HIGHLIGHTS","");
    
                        out.append("\n"+new2);
                      
                   }*/
                     c1.setArticleID(2);
                   c1.setDescription(" ");
                   c1.setHashKey(" ");
                   c1.setContentURL(" ");
                   c1.setOutletID(2);
                   c1.setHighlights(" ");
    
    
                int commentsend;
                 int summarytitle = html.indexOf("STORY HIGHLIGHTS");
                    System.out.println(summarytitle);
                    summarystart = html.indexOf("<li", summarytitle);
                     System.out.println(summarystart);
                    summaryend = html.indexOf("</li", summarystart);
                     System.out.println(summaryend);
                    commentsend = html.indexOf("</ul", summaryend);
                     System.out.println(commentsend);
                    
                   while(summaryend<commentsend){
                    for(int j=summarystart;j<summaryend;j++)
    		{
                           
    				out.append(buf_in[j]);
    
    
                    }
                    summarystart = html.indexOf("<li", summaryend);
                      System.out.println("----"+summarystart);
    
                    summaryend = html.indexOf("</li", summarystart);
                     System.out.println(summaryend);
                   
                    //summarytitle = summaryend;
                }
    
    
                   
    
    
    
                    Pattern p1 = Pattern.compile("\\(CNN\\)</b>\\s--\\s(.*)</p>\\s");
                   Matcher m1 = p1.matcher(html);
                   if(m1.find()){
                       String new1 = m1.group();
    
    
    
                       //   new1 = new1.replaceAll("\\(CNN\\) -- ","");
                        out.append("\n"+new1);
                        c1.setContent(new1);
                   }
           
           _aqua1.insertNewArticle(c1);
    
    		
                    String outnew = PrepareHtml(out.toString());
    		
    			return outnew.trim();
    
               
    
    
    		//return null;
    
    	}
    	
    	/**
    	 * Check the input for Javascript keywords.
    	 * If more than LIMIT_JAVASCRIPT_KEYWORDS are found then returns true. Otherwise return false.
    	 * @param text
    	 * @return
    	 */
    	private boolean ContainsJavascript(String text)
    	{
    		int javakeywords = 0;
    
    		
    		if(text.contains("indexOf"))
    			return true;
    		
    		if(text.contains("var "))
    			javakeywords++;
    		
    		if(text.contains("function "))
    			javakeywords++;
    		
    		if(text.contains("marginwidth"))
    			javakeywords++;
    		
    		if(text.contains("topmargin"))
    			javakeywords++;
    		
    		if(text.contains("()"))
    			javakeywords++;
    		
    		if(text.contains("write("))
    			javakeywords++;
    		
    		if(text.contains("window."))
    			javakeywords++;
    		
    		if(javakeywords >= LIMIT_JAVASCRIPT_KEYWORDS)
    			return true;
    			
    		return false;
    	}
    	
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    And what is the value of c1 when you're calling that method?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jessie is offline Member
    Join Date
    Nov 2010
    Posts
    29
    Rep Power
    0

    Default

    c1 is an object of the AquaArticle class. im using the setters to set values to the object and then insert it into the db. its like using ejb..

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    And when do you initialize c1? Hint: use some print lines, or better yet a debugger, to find out what c1 actually is before you try to call any methods on it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    jessie is offline Member
    Join Date
    Nov 2010
    Posts
    29
    Rep Power
    0

    Default

    i used the println and it says the object is null before i call the methods on it..

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    Quote Originally Posted by jessie View Post
    i used the println and it says the object is null before i call the methods on it..
    Bingo. You have to initialize a variable before calling any methods on it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. null pointer exception
    By marvelk in forum Advanced Java
    Replies: 8
    Last Post: 02-01-2011, 09:02 AM
  2. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  3. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 11:27 AM
  4. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01:47 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 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
  •