Loading...
Logo
Processing Forum
Dear all interested readers,


I have a problem with a few null pointer exceptions, comming from XML requests that should not give null pointer exceptions. When I check the data via a browser, there seems to be no problem.

If I change the line:

  xml = new XML(this, "http://www.witregel.nl/scraper/getall.php?keyword[]=&keyword[]=assad");

to

  xml = new XML(this, "http://www.witregel.nl/scraper/getall.php?keyword[]=&keyword[]=parijs");



I get a null pointer exception. When I check the link via my webbrowser, I can't find any reason to get an error. It all looks good. Do I miss something?

Thanks in advance for the tips.


Here's the code:

Copy code

  1. String woorden = " ";

    boolean aan = false;

    int startcount;

    XML[] urls;
    XML[] bron;
    XML[] artikelen;



    int scroll;

    String[] wordsArray;
    int numLines = 0;
    float textHeight;


    String artikel;

    XML xml;

    int i;
    PFont font;

    int fontSize = 10;
    int specificWidth = 700-40;
    int lineSpacing = 5;



    void setup() {


      size(1300, 800);
      font = createFont("Arial", fontSize);
      textFont(font, fontSize);
    }


    void draw() {
      background(255);


      xml = new XML(this, "http://www.witregel.nl/scraper/getall.php?keyword[]=&keyword[]=assad");

      //xml = new XML(this, "http://www.witregel.nl/scraper/getall.php?keyword[]=&keyword[]=parijs");

      XML[] urls = xml.getChildren("result/url");
      XML[] bron = xml.getChildren("result/bron");
      XML[] artikelen = xml.getChildren("result/artikel");  



      for (int i = 0; i < artikelen.length; i++)
      {


        textFont(font, fontSize);


      
        if (frameCount % 1 == 0) {

          float m = mouseX;

          scroll = (int)map(m, 0, width, 0, artikelen.length);

          println(scroll);


          textFont(font, fontSize);

          String artikel = artikelen[scroll].getContent();


          artikel = artikel.replaceAll("\n", " ");


          fill(0, 255, 0);

          rect(60, 10, specificWidth+20, textboxhoogte(artikel, specificWidth, fontSize, lineSpacing));

          if (scroll == i) {
            fill(255);
          }
          else {
            fill(0);
          }


          fill(0, 20, 300);

          text( artikel, 70, 20, specificWidth, textboxhoogte(artikel, specificWidth, fontSize, lineSpacing));


          fill(0, 0, 250);
          textFont(font, 20);
        }
      }
    }



    int textboxhoogte(String string, int specificWidth, int fontSize, int lineSpacing) {
      String[] wordsArray;
      String tempString = "";
      int numLines = 0;
      float textHeight;

      wordsArray = split(string, " ");

      for (int i=0; i < wordsArray.length; i++) {
        if (textWidth(tempString + wordsArray[i]) < specificWidth) {
          tempString += wordsArray[i] + " ";
        }
        else {
          tempString = wordsArray[i] + " ";
          numLines++;
        }
      }

      numLines++;

      textHeight = numLines * (textDescent() + textAscent() + lineSpacing);
      return(round(textHeight));
    }

Replies(5)

These are a few messages I recieve:
[Fatal Error] :34:56: The reference to entity "v" must end with the ';' delimiter.
org.xml.sax.SAXParseException: The reference to entity "v" must end with the ';' delimiter.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
    at processing.core.XML.<init>(Unknown Source)
    at processing.core.XML.<init>(Unknown Source)
    at xml_lees_4_kleurenkiezer_enkel_art.draw(xml_lees_4_kleurenkiezer_enkel_art.java:126)
    at processing.core.PApplet.handleDraw(Unknown Source)
    at processing.core.PGraphics.requestDraw(PGraphics.java:674)
    at processing.core.PApplet.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:680)
Exception in thread "Animation Thread" java.lang.NullPointerException
    at processing.core.XML.checkChildren(Unknown Source)
    at processing.core.XML.getChildCount(Unknown Source)
    at processing.core.XML.getChildren(Unknown Source)
    at processing.core.XML.getChildrenRecursive(Unknown Source)
    at processing.core.XML.getChildren(Unknown Source)
    at xml_lees_4_kleurenkiezer_enkel_art.draw(xml_lees_4_kleurenkiezer_enkel_art.java:134)
    at processing.core.PApplet.handleDraw(Unknown Source)
    at processing.core.PGraphics.requestDraw(PGraphics.java:674)
    at processing.core.PApplet.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:680)


and for another keyword:


[Fatal Error] :87:4750: The entity name must immediately follow the '&' in the entity reference.
org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:246)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
    at processing.core.XML.<init>(Unknown Source)
    at processing.core.XML.<init>(Unknown Source)
    at xml_lees_4_kleurenkiezer_enkel_art.draw(xml_lees_4_kleurenkiezer_enkel_art.java:126)
    at processing.core.PApplet.handleDraw(Unknown Source)
    at processing.core.PGraphics.requestDraw(PGraphics.java:674)
    at processing.core.PApplet.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:680)
Exception in thread "Animation Thread" java.lang.NullPointerException
    at processing.core.XML.checkChildren(Unknown Source)
    at processing.core.XML.getChildCount(Unknown Source)
    at processing.core.XML.getChildren(Unknown Source)
    at processing.core.XML.getChildrenRecursive(Unknown Source)
    at processing.core.XML.getChildren(Unknown Source)
    at xml_lees_4_kleurenkiezer_enkel_art.draw(xml_lees_4_kleurenkiezer_enkel_art.java:134)
    at processing.core.PApplet.handleDraw(Unknown Source)
    at processing.core.PGraphics.requestDraw(PGraphics.java:674)
    at processing.core.PApplet.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:680)






Ah, the second message is more useful, as actually the root error is more explicit.
I opened the second URL with Firefox which shows only the data. If you use View Source (Ctrl+U), you will see at line 34 a red & in an URL: the XML data is improperly escaped, it should be in a CDATA section, or the & should be replaced with &amp; So the parser sees the &v as an incorrect XML entity, causing an exception and making the new XML module of Processing to choke: it still lacks good error handling, I fear...

So, the root cause is an improper XML data from the site. Except with some exception handling, it is hard to cope with that.
Thanks for the answer, didn't see that red & before. I allready looked at exception handling, but I really don't know how to use this. Is it easy to catch?
You can try something like that:
Copy code
  1.   xml = new XML(this, "http://www.witregel.nl/scraper/getall.php?keyword[]=&keyword[]=parijs");
  2.   try
  3.   {
  4.     int n = xml.getChildCount();
  5.   }
  6.   catch (Exception e)
  7.   {
  8.     println(e.getMessage());
  9.     return;
  10.   }

Note: I haven't noticed, but you should not read your XML feed in draw()! No need to access the Web site 60 times per second, I don't think they update results so often...
Thanks! The website is mine, and I use the Processing sketch to visualise articles from the database that I scraped from newspaper websites. In my full code, I don't request the XML every frame. I only request it on demand, so that's okay. My server would go nuts if I did ;-). Thank you so much for the tips. I will talk to my web programmer that wrote the PHP scripts and ask him if we can fix this problem. For now, I just use this exception trick to avoid the code from crashing.