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 8930513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:54:32+00:00 2026-06-15T08:54:32+00:00

Okay, I have a class createUser which is supposed to create an XML file

  • 0

Okay, I have a class createUser which is supposed to create an XML file to store data on a user. Problem is when I run it I’m getting this error

>        ERROR:  ''
>     javax.xml.transform.TransformerException: java.lang.NullPointerException
>         at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown
> Source)
>         at CreateUser.makeUser(CreateUser.java:156)
>         at Welcomeclass.welcome(Welcomeclass.java:48)
>         at Welcomeclass.main(Welcomeclass.java:32)
>     Caused by: java.lang.NullPointerException
>         at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown
> Source)
>         ... 5 more
>     ---------
>     java.lang.NullPointerException
>         at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown
> Source)
>         at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown
> Source)
>         at CreateUser.makeUser(CreateUser.java:156)
>         at Welcomeclass.welcome(Welcomeclass.java:48)
>         at Welcomeclass.main(Welcomeclass.java:32)

which means it is incapable of transforming my doc into an xml file.

Here is the code it is from.

/*imports*/
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/*A class to create a user object and store it in a XML file for later retrieval
public class CreateUser {   
    static Scanner input = new Scanner(System.in);

    /*objects note: must be strings due to being stored in XML table*/
    static String name;
    static String age;
    static String bday;
    static String gender;
    static String location;
    static String orientation;
    static String relationship;
    static String hobbies;
    static String choice;
    static String username;
    static String password;

    static String fileLocation = "C:/Users/Steven/Workspace/twitter/src/users.xml";

    int count = 0;
    int maxId = 0;
    static int nextId  = 0;

    public static void makeUser() throws SAXException, IOException {
        /*gets user input to fill String objects*/
        System.out.println("Hello, to register we will need some information about you.");
        System.out.println("What is your name?");
        name = input.nextLine();
        System.out.println("How old are you(e.g. 45)?");
        age = input.nextLine();
        System.out.println("When is your birthday(MM/DD/YYYY)?");
        bday = input.nextLine();
        System.out.println("What is your gender?");
        gender = input.nextLine();
        System.out.println("Where do you live?");
        location = input.nextLine();
        System.out.println("What is your orientation?");
        orientation = input.nextLine();
        System.out.println("Are you in a relationship? (y/n)");
        choice = input.nextLine();
        if(choice.equals("y"))
            relationship = "In a relationship.";
        if(choice.equals("y"))  
            relationship = "Single.";
        System.out.println("What are your hobbies?");
        hobbies = input.nextLine();
        System.out.println("What will be your username?");
        username = input.nextLine();
        System.out.println("What will be your password?");
        password = input.nextLine();    

        /*create XML file to store the data*/
        try{
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document userslist = docBuilder.newDocument();
            /*create user element*/
            Element users = userslist.createElement("users");
            userslist.appendChild(users);

            Element user = userslist.createElement("user");
            users.appendChild(user);

            /*get the max id to set the next id if the file exists*/
            File xmlFile = new File(fileLocation);
            if(xmlFile.exists())
            {
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document idgetter = dBuilder.parse(xmlFile);
                idgetter.getDocumentElement().normalize();
                NodeList nodes = idgetter.getElementsByTagName("id");
                int maxId = 0;
                for(int i = 0; i < nodes.getLength(); i++){
                    if(Integer.parseInt(nodes.item(i).getTextContent()) > maxId )
                    {
                        maxId = Integer.parseInt(nodes.item(i).getTextContent());
                    }
                }
                nextId = maxId +1;
            }
            /*else create the file*/
            else
            {
                /*create the id attribute*/
                Attr attr = userslist.createAttribute("id");
                attr.setValue(String.valueOf(nextId));
                user.setAttributeNode(attr);

                /*fill in doc with objects*/
                Element dname = userslist.createElement("name");
                dname.appendChild(userslist.createTextNode(name));
                user.appendChild(dname);
                Element dgender = userslist.createElement("gender");
                dgender.appendChild(userslist.createTextNode(gender));
                user.appendChild(dgender);
                Element dlocation = userslist.createElement("location");
                dlocation.appendChild(userslist.createTextNode(location));
                user.appendChild(dlocation);
                Element dorientation = userslist.createElement("orientation");
                dorientation.appendChild(userslist.createTextNode(orientation));
                user.appendChild(dorientation);
                Element drelationship = userslist.createElement("relationship");
                drelationship.appendChild(userslist.createTextNode(relationship));
                user.appendChild(drelationship);
                Element dhobbies = userslist.createElement("hobbies");
                dhobbies.appendChild(userslist.createTextNode(hobbies));
                user.appendChild(dhobbies);
                Element dchoice = userslist.createElement("choice");
                dchoice.appendChild(userslist.createTextNode(choice));
                user.appendChild(dchoice);
                Element dusername = userslist.createElement("username");
                dusername.appendChild(userslist.createTextNode(username));
                user.appendChild(dusername);
                Element dpassword = userslist.createElement("password");
                dpassword.appendChild(userslist.createTextNode(password));
                user.appendChild(dpassword);
                Element dbday = userslist.createElement("birthday");
                dbday.appendChild(userslist.createTextNode(bday));
                user.appendChild(dbday);
                Element dage = userslist.createElement("age");
                dage.appendChild(userslist.createTextNode(age));
                user.appendChild(dage);

                /*transfer document to XML*/
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(users);

                /*create the document in append mode */
                //StreamResult result = new StreamResult(new FileWriter(fileLocation, true));
                StreamResult result = new StreamResult(System.out);

                transformer.transform(source, result);
            }
        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (TransformerException tfe) {
            tfe.printStackTrace();
        }
    }
}

If you don’t want to take the time to trouble shoot it yourself or look it over that’s fine but if you have and idea about how to troubleshoot transformer issues that would be fantastic. Because I’m having a hard time figuring out exactly what is causing this issue.

  • 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-06-15T08:54:34+00:00Added an answer on June 15, 2026 at 8:54 am

    You get the NullPointerException when something other than y is entered for the relationship question.

        if (choice.equals("y"))
            relationship = "In a relationship.";
        if (choice.equals("y"))
            relationship = "Single.";
    

    A quick fix is to set a default value for the field relationship

    if ("y".equals(choice)) {
        relationship = "In a relationship.";
    else {
        relationship = "Single.";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I have a series of objects based on a base class which are
Okay, so let's say I have a class file called Orange, and then two
I have a class Constants in which I store a number of static readonly
Okay so I have a driver class called MonthlyReport that reads in data from
Okay so I have a class that has 'weak typing' I.E. it can store
Okay so let's say I have class A... CLASS A has a method that
Okay so I have this mode: class Posts(db.Model): rand1 = db.FloatProperty() #other models here
Okay so I have started a new language in class. We are learning Scheme
Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
Okay, I have something like this in C++: class MyClass{ private: int someVariable; int

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.