2 qq 22923235 qq_22923235 于 2015.07.18 10:59 提问

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个回答

tongyi55555
tongyi55555   2015.07.18 11: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  

tongyi55555
tongyi55555 回复android灬小菜鸟: 你这个问题是现在不知道你怎么赋值的,要不你贴一下你赋值的代码看看。再就是你自己测试下赋值的语句,看看赋值时是否传入的是null.
2 年多之前 回复
qq_22923235
qq_22923235 那么请问要怎么样才能正确给InputStream赋值呢?
2 年多之前 回复
qq_22923235
qq_22923235 还是有报空指针错误,Exception in thread "Thread-0" java.lang.NullPointerException at ObdCommand.readResult(ObdCommand.java:110) at ObdCommand.run(ObdCommand.java:92)
2 年多之前 回复
91program
91program   Ds   Rxr 2015.07.18 11:59

将对象这样赋值: InputStream in = null; 时,其实 in 并没有被正确的初始化,也就是分配空间。
后续使用时,如果直接给 in 赋值就会报错的。

91program
91program 回复android灬小菜鸟: 赋一个有效的值,如空格等,那怕是 “”
2 年多之前 回复
qq_22923235
qq_22923235 那么请问要怎么样才能真确赋值呢。
2 年多之前 回复
caozhy
caozhy   Ds   Rxr 2015.07.19 07:17

在你的函数中判断下
if (in == null)
说明没有赋值

对于调用者,你需要传一个有效的输入流进来。

Csdn user default icon
上传中...
上传图片
插入图片