Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6774811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:49:56+00:00 2026-05-26T15:49:56+00:00

All, the following code is throwing an NullPointerException. SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp

  • 0

All, the following code is throwing an NullPointerException.

        SAXParserFactory spf = SAXParserFactory.newInstance(); 
        SAXParser sp = spf.newSAXParser(); 

        XMLReader xr = sp.getXMLReader(); 

        dataHandler dataHandler = new dataHandler(); 
        xr.setContentHandler(dataHandler); 

xmldata="<ArrayOfNacionalClass xmlns="http://schemas.datacontract.org/2004/07/ServiceCompras" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><NacionalClass><ProdutoName>Batata Congelada Pré Frita Perdigão 400g</ProdutoName><ProdutoPreco>2,50</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Iogurte Polpa Morango Batavo 540g</ProdutoName><ProdutoPreco>2,20</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Protetor Solar Loção FPS 15 Sundown 120ml</ProdutoName><ProdutoPreco>16,83</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Costela Bovina Resfriada por kg com Peso Aprox. 1,250 Kg</ProdutoName><ProdutoPreco>10,87</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Refrigerante Pepsi 2 Litros</ProdutoName><ProdutoPreco>2,99</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Pizza Mussarela Ristorante 340g</ProdutoName><ProdutoPreco>5,98</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Néctar de Uva Del Valle Mais 1,5L</ProdutoName><ProdutoPreco>4,99</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Batata Original Ruffles 400g</ProdutoName><ProdutoPreco>11,70</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Bolo de Chocolate Granulado por kg com Peso Aprox. 0,500 Kg</ProdutoName><ProdutoPreco>14,00</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Sorvete Especial Diamante Negro Kibon 2L</ProdutoName><ProdutoPreco>15,38</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Bebida Láctea Nescau Fast Nestlé 300ml</ProdutoName><ProdutoPreco>2,38</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Cerveja Pilsen Nova Schin 473ml</ProdutoName><ProdutoPreco>1,69</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Lava Roupas Líquido Concentrado Omo Multiação 315ml</ProdutoName><ProdutoPreco>5,20</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Kit Shampoo e Condicionador Controle de Queda 400ml Pantene Pro-V</ProdutoName><ProdutoPreco>17,00</ProdutoPreco></NacionalClass><NacionalClass><ProdutoName>Amaciante Comfort Original 500ml</ProdutoName><ProdutoPreco>5,00</ProdutoPreco></NacionalClass></ArrayOfNacionalClass>"

InputSource is=new InputSource(new ByteArrayInputStream(xmldata.getBytes()));



try {
      xr.parse(is);
  }catch (Exception e)
  {
     e.printStackTrace();
   }

I honestly dont understand why its giving me NullPointer Exception, I also tried usind stringreader instead of ByteArray with NO luck

Please help..thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:49:56+00:00Added an answer on May 26, 2026 at 3:49 pm

    When I used a SAXParser I found that I needed to add the XML header to my file before it would work, and I also needed to enclose all my data in a single top level tag, e.g.

      private static final String XML_HEADER = "<?xml version=\"1.0\"?><z>";
      private static final String XML_FOOTER = "</z>"
    
      String xmldata = XML_HEADER + 
                       "<ArrayOfNacionalClass ... your data >" +
                       XML_FOOTER;
      sp.parse(new ByteArrayInputStream(xmldata.getBytes("UTF-8")), 
               this);
    

    You might not need the <z></z> parts above if all your data will always be enclosed in your top level ArrayOfNacionalClass tag.

    <ArrayOfNacionalClass>your data</ArrayOfNacionalClass>
    

    If you leave my <z></z> tags in then you’ll need to handle them in your DefaultHandler class.

    In case none of that helps, here’s some sample code for a SAXParser that you might find useful:
    http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

    The code you’ve posted doesn’t look like it will compile, unless this is your own dataHandler class? If that’s the case then you should post the code for that too.

    I recommend you use an IDE that will help you fix compile errors as you type (e.g. Eclipse).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All the variations of wait(...) are throwing the below exception from the following code.
I write the following code all the time to handle when the enter key
I had no problem at all running the following code on a local server,
The following code works in all browsers apart from IE6... var mylib = {
The following code is visual basic, .NET, ASP... all of the above? Can anybody
I have the following code to zip all the files and then save it
Im using the following code to output all the nodes from the xml shown.
I have the following code: // Obtain the string names of all the elements
I'm using the following code to get an array with all sub directories from
The following line of code in my class constructor is throwing a StackOverflowException: myList

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.