简体   繁体   中英

Keep getting null pointer exception

here is my program: basically i have an xml file and from that file i have to decode a base64 string but i keep getting NullPointerException..please help! code is as follows...

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;
    }
}

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

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

It's hard to guess where you're getting your error, but I'm assuming here:

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

I don't see dms_current_message being initialized ever, yet you're calling a method on it, which would definitely result in the null pointer exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM