android灬小菜鸟 2015-07-18 02:59 采纳率: 0%
浏览 3108

InputStream in = null 报空指针异常。

package com.infisight.hudmobile.obd.command;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;

//import awis.obd.io.ObdConnectThread;

public class ObdCommand extends Thread {

/**
 * @uml.property name="in"
 */
protected InputStream in = null;
/**
 * @uml.property name="out"
 */
protected OutputStream out = null;
/**
 * @uml.property name="buff"
 * @uml.associationEnd multiplicity="(0 -1)" elementType="java.lang.Byte"
 */
protected ArrayList<Byte> buff = null;
/**
 * @uml.property name="cmd"
 */
protected String cmd = null;
/**
 * @uml.property name="desc"
 */
protected String desc = null;
/**
 * @uml.property name="resType"
 */
protected String resType = null;
/**
 * @uml.property name="error"
 */
protected Exception error;
/**
 * @uml.property name="rawValue"
 */
protected Object rawValue = null;
/**
 * @uml.property name="data"
 * @uml.associationEnd 
 *                     qualifier="constant:java.lang.String java.lang.Double"
 */
protected HashMap<String, Object> data = null;
/**
 * @uml.property name="connectThread"
 * @uml.associationEnd inverse="cmds:awis.obd.io.ObdConnectThread"
 */
// protected ObdConnectThread connectThread = null;
/**
 * @uml.property name="impType"
 */
protected String impType = null;

public ObdCommand(String cmd, String desc, String resType, String impType) {
    this.cmd = cmd;
    this.desc = desc;
    this.resType = resType;
    this.buff = new ArrayList<Byte>();
    this.impType = impType;
}

// /**
// * @param thread
// * @uml.property name="connectThread"
// */
// public void setConnectThread(ObdConnectThread thread) {
// this.connectThread = thread;
// }
// public boolean isImperial() {
// if (connectThread != null && connectThread.getImperialUnits()) {
// return true;
// }
// return false;
// }

public ObdCommand(ObdCommand other) {
    this(other.cmd, other.desc, other.resType, other.impType);
}

public void setInputStream(InputStream in) {
    this.in = in;
}

public void setOutputStream(OutputStream out) {
    this.out = out;
}

public void run() {
    sendCmd(cmd);
    readResult();
}

public void setDataMap(HashMap<String, Object> data) {
    this.data = data;
}

protected void sendCmd(String cmd) {
    try {
        cmd += "\r\n";
        out.write(cmd.getBytes());
        out.flush();
    } catch (Exception e) {
        // TODO: handle exception
    }
}

protected void readResult() {
    byte c = 0;
    this.buff.clear();
    try {
        while ((char) (c = (byte) in.read()) != '>') {
            buff.add(c);
        }
    } catch (IOException e) {
    }
}

public String getResult() {
    return new String(getByteArray());
}

public byte[] getByteArray() {
    byte[] data = new byte[this.buff.size()];
    for (int i = 0; i < this.buff.size(); i++) {
        data[i] = this.buff.get(i);
    }
    return data;
}

public String formatResult(String ret) {
    String res = ret;
    String[] ress = res.split("\r");
    res = ress[0].replace(" ", "");
    return res;
}

/**
 * @return
 * @uml.property name="in"
 */
public InputStream getIn() {
    return in;
}

/**
 * @return
 * @uml.property name="out"
 */
public OutputStream getOut() {
    return out;
}

public ArrayList<Byte> getBuff() {
    return buff;
}

/**
 * @return
 * @uml.property name="cmd"
 */
public String getCmd() {
    return cmd;
}

/**
 * @return
 * @uml.property name="desc"
 */
public String getDesc() {
    return desc;
}

/**
 * @return
 * @uml.property name="resType"
 */
public String getResType() {
    return resType;
}

/**
 * @param e
 * @uml.property name="error"
 */
public void setError(Exception e) {
    error = e;
}

/**
 * @return
 * @uml.property name="error"
 */
public Exception getError() {
    return error;
}

/**
 * @return
 * @uml.property name="rawValue"
 */
public Object getRawValue() {
    return rawValue;
}

}
在package com.infisight.hudmobile.obd.command;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;

//import awis.obd.io.ObdConnectThread;

public class ObdCommand extends Thread {

/**
 * @uml.property name="in"
 */
protected InputStream in = null;
/**
 * @uml.property name="out"
 */
protected OutputStream out = null;
/**
 * @uml.property name="buff"
 * @uml.associationEnd multiplicity="(0 -1)" elementType="java.lang.Byte"
 */
protected ArrayList<Byte> buff = null;
/**
 * @uml.property name="cmd"
 */
protected String cmd = null;
/**
 * @uml.property name="desc"
 */
protected String desc = null;
/**
 * @uml.property name="resType"
 */
protected String resType = null;
/**
 * @uml.property name="error"
 */
protected Exception error;
/**
 * @uml.property name="rawValue"
 */
protected Object rawValue = null;
/**
 * @uml.property name="data"
 * @uml.associationEnd 
 *                     qualifier="constant:java.lang.String java.lang.Double"
 */
protected HashMap<String, Object> data = null;
/**
 * @uml.property name="connectThread"
 * @uml.associationEnd inverse="cmds:awis.obd.io.ObdConnectThread"
 */
// protected ObdConnectThread connectThread = null;
/**
 * @uml.property name="impType"
 */
protected String impType = null;

public ObdCommand(String cmd, String desc, String resType, String impType) {
    this.cmd = cmd;
    this.desc = desc;
    this.resType = resType;
    this.buff = new ArrayList<Byte>();
    this.impType = impType;
}

// /**
// * @param thread
// * @uml.property name="connectThread"
// */
// public void setConnectThread(ObdConnectThread thread) {
// this.connectThread = thread;
// }
// public boolean isImperial() {
// if (connectThread != null && connectThread.getImperialUnits()) {
// return true;
// }
// return false;
// }

public ObdCommand(ObdCommand other) {
    this(other.cmd, other.desc, other.resType, other.impType);
}

public void setInputStream(InputStream in) {
    this.in = in;
}

public void setOutputStream(OutputStream out) {
    this.out = out;
}

public void run() {
    sendCmd(cmd);
    readResult();
}

public void setDataMap(HashMap<String, Object> data) {
    this.data = data;
}

protected void sendCmd(String cmd) {
    try {
        cmd += "\r\n";
        out.write(cmd.getBytes());
        out.flush();
    } catch (Exception e) {
        // TODO: handle exception
    }
}

protected void readResult() {
    byte c = 0;
    this.buff.clear();
    try {
        while ((char) (c = (byte) in.read()) != '>') {
            buff.add(c);
        }
    } catch (IOException e) {
    }
}

public String getResult() {
    return new String(getByteArray());
}

public byte[] getByteArray() {
    byte[] data = new byte[this.buff.size()];
    for (int i = 0; i < this.buff.size(); i++) {
        data[i] = this.buff.get(i);
    }
    return data;
}

public String formatResult(String ret) {
    String res = ret;
    String[] ress = res.split("\r");
    res = ress[0].replace(" ", "");
    return res;
}

/**
 * @return
 * @uml.property name="in"
 */
public InputStream getIn() {
    return in;
}

/**
 * @return
 * @uml.property name="out"
 */
public OutputStream getOut() {
    return out;
}

public ArrayList<Byte> getBuff() {
    return buff;
}

/**
 * @return
 * @uml.property name="cmd"
 */
public String getCmd() {
    return cmd;
}

/**
 * @return
 * @uml.property name="desc"
 */
public String getDesc() {
    return desc;
}

/**
 * @return
 * @uml.property name="resType"
 */
public String getResType() {
    return resType;
}

/**
 * @param e
 * @uml.property name="error"
 */
public void setError(Exception e) {
    error = e;
}

/**
 * @return
 * @uml.property name="error"
 */
public Exception getError() {
    return error;
}

/**
 * @return
 * @uml.property name="rawValue"
 */
public Object getRawValue() {
    return rawValue;
}

}
在protected void readResult() {
byte c = 0;
this.buff.clear();
try {
while ((char) (c = (byte) in.read()) != '>') {
buff.add(c);
}
} catch (IOException e) {
}
}
中while ((char) (c = (byte) in.read()) != '>')这句一直报空指针异常,不知道是怎么回事。

  • 写回答

3条回答 默认 最新

  • JonsonJiao 2015-07-18 03:31
    关注

    是不是你的in没有设置正确啊?给你个测试例子,在readResult方法中增加一个输出即可。

    public static void main(String[] args) {
            ObdCommand obdCom = new ObdCommand("cmd", "desc", "resType", "impType");
            obdCom.setInputStream(System.in);
            obdCom.readResult();
        }
    
    protected void readResult() {
            byte c = 0;
            this.buff.clear();
            try {
                while ((char) (c = (byte) inputStream.read()) != '>') {
                    buff.add(c);
                }
                for (int i = 0; i < this.buff.size(); i++) {
                    System.out.print(buff.get(i) + " ");
                }
            } catch (IOException e) {
            }
        } 
    

    启动后在控制台输入we are ready>得到的结果如下;

    we are ready>
    119 101 32 97 114 101 32 114 101 97 100 121  
    
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?