我正在为一家与MySQL数据库通信的公司构建应用程序,试图将新条目发送到服务器,但是无法使用outputstream
write
方法工作,我知道有一个错误,但是我似乎找不到解决方法
这是我的代码:
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new OutputStream() {
@Override
public void write(int b) throws IOException {
Log.e("Car DetailStream Writer","outputStreamWriter write method called");
try {
int isLargeAsInt = car.isLarge() ? 1 : 0;
int isDieselAsInt = car.isDiesel() ? 1 : 0;
Log.e("Car Details Write Data",car.getCarModel());
writeData =
URLEncoder.encode("carModel", "UTF-8") + "=" + URLEncoder.encode(car.getCarModel(), "UTF-8") + "&" +
URLEncoder.encode("carYear", "UTF-8") + "=" + URLEncoder.encode(car.getYear(), "UTF-8") + "&" +
URLEncoder.encode("carPlate", "UTF-8") + "=" + URLEncoder.encode(car.getPlateNumber(), "UTF-8") + "&" +
URLEncoder.encode("isLarge", "UTF-8") + "=" + isLargeAsInt + "&" +
URLEncoder.encode("isDiesel", "UTF-8") + "=" + isDieselAsInt + "&" +
URLEncoder.encode("currentMileage", "UTF-8") + "=" + car.getCurrentMileage();
Log.e("Written Data", writeData);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
Log.d("Buffered Writer","Buffered Writer Created");
bufferedWriter.write(writeData);
bufferedWriter.flush();
这是错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at java.io.Writer.write(Writer.java:157)
at com.example.trknil.turknilmanagement.utils.SendUtils.makeHttpRequest(SendUtils.java:124)
这是第124行:
bufferedWriter.write(writeData);
任何人都可以修复该代码,并告诉我我哪里出错了。