Java XML文件无法写入

Java XML文件无法写入,java,xml,file,nullpointerexception,transformer,Java,Xml,File,Nullpointerexception,Transformer,好的,我有一个createUser类,它应该创建一个XML文件来存储用户的数据。问题是当我运行它时,我得到了这个错误 这意味着它无法将我的文档转换为xml文件 这是它的代码 /*imports*/ import java.util.Scanner; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import

好的,我有一个createUser类,它应该创建一个XML文件来存储用户的数据。问题是当我运行它时,我得到了这个错误

这意味着它无法将我的文档转换为xml文件

这是它的代码

/*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();
        }
    }
}
/*导入*/
导入java.util.Scanner;
导入java.io.File;
导入java.io.FileWriter;
导入java.io.IOException;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
导入javax.xml.transform.Transformer;
导入javax.xml.transform.TransformerException;
导入javax.xml.transform.TransformerFactory;
导入javax.xml.transform.dom.DOMSource;
导入javax.xml.transform.stream.StreamResult;
导入org.w3c.dom.Attr;
导入org.w3c.dom.Document;
导入org.w3c.dom.Element;
导入org.w3c.dom.NodeList;
导入org.xml.sax.SAXException;
/*用于创建用户对象并将其存储在XML文件中以供以后检索的类
公共类CreateUser{
静态扫描仪输入=新扫描仪(System.in);
/*对象注意:由于存储在XML表中,所以必须是字符串*/
静态字符串名;
静态串龄;
静态字符串bday;
静态字符串性别;
静态字符串位置;
静态串定向;
静态字符串关系;
静态字符串爱好;
静态字符串选择;
静态字符串用户名;
静态字符串密码;
静态字符串fileLocation=“C:/Users/Steven/Workspace/twitter/src/Users.xml”;
整数计数=0;
int maxId=0;
静态int nextId=0;
公共静态void makeUser()引发SAXException,IOException{
/*获取用户输入以填充字符串对象*/
System.out.println(“您好,要注册,我们需要一些关于您的信息。”);
System.out.println(“你叫什么名字?”);
name=input.nextLine();
System.out.println(“您多大年纪(例如45岁)?”;
age=input.nextLine();
System.out.println(“你的生日是什么时候(MM/DD/YYYY)”;
bday=input.nextLine();
System.out.println(“你的性别是什么?”);
性别=input.nextLine();
System.out.println(“你住在哪里?”);
location=input.nextLine();
System.out.println(“你的方向是什么?”);
方向=输入.nextLine();
System.out.println(“你有关系吗?(y/n)”);
choice=input.nextLine();
if(选择等于(“y”))
relationship=“在关系中。”;
if(选择等于(“y”))
关系=“单身。”;
你的爱好是什么;
嗜好=输入。nextLine();
System.out.println(“您的用户名是什么?”);
username=input.nextLine();
System.out.println(“您的密码是什么?”);
password=input.nextLine();
/*创建XML文件来存储数据*/
试一试{
DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docFactory.newDocumentBuilder();
Document userslist=docBuilder.newDocument();
/*创建用户元素*/
Element users=userslist.createElement(“用户”);
userslist.appendChild(用户);
Element user=userslist.createElement(“用户”);
users.appendChild(用户);
/*获取最大id以设置下一个id(如果文件存在)*/
文件xmlFile=新文件(文件位置);
if(xmlFile.exists())
{
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
文档idgetter=dBuilder.parse(xmlFile);
idgetter.getDocumentElement().normalize();
NodeList nodes=idgetter.getElementsByTagName(“id”);
int maxId=0;
对于(int i=0;imaxId)
{
maxId=Integer.parseInt(nodes.item(i.getTextContent());
}
}
nextId=maxId+1;
}
/*否则创建文件*/
其他的
{
/*创建id属性*/
Attr Attr=userslist.createAttribute(“id”);
attr.setValue(String.valueOf(nextId));
user.setAttributeNode(attr);
/*用对象填充文档*/
Element dname=userslist.createElement(“名称”);
dname.appendChild(userslist.createTextNode(名称));
user.appendChild(dname);
Element dgender=userslist.createElement(“性别”);
appendChild(userslist.createTextNode(性别));
user.appendChild(dgender);
Element dlocation=userslist.createElement(“位置”);
appendChild(userslist.createTextNode(位置));
user.appendChild(dlocation);
Element dorientation=userslist.createElement(“方向”);
appendChild(userslist.createTextNode(方向));
用户。附加子对象(dorientation);
Element-drelationship=userslist.createElement(“关系”);
appendChild(userslist.createTextNode(relationship));
user.appendChild(drelationship);
Element dhobbies=userslist.createElement(“爱好”);
appendChild(userslist.createTextNode(嗜好));
user.appendChild(dhobbies);
Element dchoice=userslist.createElement(“选择”);
dchoice.appendChild(userslist.createTextNode(选项));
user.appendChild(dchoice);
元素dusername=usersli
/*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();
        }
    }
}
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters(Unknown
> Source)
int length = readValue.length();
if (length == 0){
  throw new NullPointerException("Node value can not be null");
}
    if (choice.equals("y"))
        relationship = "In a relationship.";
    if (choice.equals("y"))
        relationship = "Single.";
if ("y".equals(choice)) {
    relationship = "In a relationship.";
else {
    relationship = "Single.";
}