继续获取空指针异常

时间:2014-06-26 14:20:03

标签: java

这是我的程序:基本上我有一个xml文件,从那个文件我必须解码一个base64字符串,但我一直得到NullPointerException..please帮助!代码如下......

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.commons.codec.binary.Base64;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Arrays;

public class main {
    public static void main(String[] args) throws Exception {
        SAXParserFactory parserFactor = SAXParserFactory.newInstance();
        SAXParser parser = parserFactor.newSAXParser();
        SAXHandler handler = new SAXHandler();
        //parser.parse(ClassLoader.getSystemResourceAsStream("ATMSPopulateDMSData.xml"), 
        // handler);
        parser.parse(new FileInputStream("C:\\Users\\qta6754\\workspace\\Java_Dev\\XML64_Decoded\\ATMSMessageData.xml"), handler);
        for (NeededInfo emp : handler.empList) {
            System.out.println(emp);
        }
    }
}

class SAXHandler extends DefaultHandler {
    List<NeededInfo> empList = new ArrayList<>();
    NeededInfo emp = null;
    String content = null;
    String did = null;

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        switch (qName) {
            case "dMSDeviceStatus":
                emp = new NeededInfo();
                emp.id = attributes.getValue("id");
                emp.Read();
                break;
        }
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        content = String.copyValueOf(ch, start, length).trim();
    }
}

class NeededInfo {
    String id;
    String firstName;
    String lastName;
    String location;
    String organization_id;
    String operator_id;
    String device_id;
    String dms_device_status;
    String dms_current_message;
    String last_comm_time;
    String date;
    String time;

    public String toString() {
        //return firstName + " " + lastName + "(" + id + ")" + location+date+time+device_name;
        return "Organization id: " + organization_id + "\n" + "Operator id: " + operator_id + "\n" + "Device id: " + device_id + "\n"
                + "Dms Device Status: " + dms_device_status + "\n" + "Dms Current Message: " + dms_current_message + "\n" + "Last Comm Time" + "\n"
                + "Time: " + time + "\n" + "Date: " + date + "\n" + "decoded string is: " + "\n" + "-------------------------------------";
    }

    public void Read() {
        byte[] byteArray = Base64.decodeBase64(dms_current_message.getBytes());
        String decodedString = new String(byteArray);
        System.out.print("The decoded message is:  " + decodedString);
        //                   return decodedString;
    }
}

2 个答案:

答案 0 :(得分:1)

您的Read方法会访问您在问题中包含的所有代码中从未初始化的dms_current_message

byte[] byteArray = Base64.decodeBase64(dms_current_message.getBytes());

答案 1 :(得分:1)

很难猜出你的错误在哪里,但我假设在这里:

byte[] byteArray = Base64.decodeBase64(dms_current_message.getBytes());

我没有看到dms_current_message被初始化,但你正在调用它的方法,这肯定会导致空指针异常。