Error in my Volley. I'm trying upload a image in my webserver
Error in my Volley. I'm trying upload a image in my webserver
E/Volley: [5836] NetworkDispatcher.run: Unhandled exception
java.lang.NullPointerException: Attempt to invoke virtual method 'int
java.lang.String.length()' on a null object reference
This are my codes:
private ImageView imgNBI;
private Bitmap bitmap;
private String image, name, imgtype, retrieveDriverID,driverID;
private String KEY_DRIVERID = "driverid";
private String KEY_IMAGE = "image";
private String KEY_IMAGETYPE = "imgtype";
private String KEY_NAME = "name";
private Context ctx = this;
ProgressDialog pDialog;
Context context = this;
private String UPLOAD_URL ="http://angkas.net23.net/uploadimage1.php";
This is my code for update:
private void update()
pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
getUser();
retrieveDriverID = driverID;
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>()
@Override
public void onResponse(String response)
pDialog.dismiss();
Toast.makeText(Requirements_1.this, "This is response: "+response, Toast.LENGTH_LONG).show();
,
new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
pDialog.dismiss();
Toast.makeText(Requirements_1.this, "This is error: "+error.getMessage(), Toast.LENGTH_LONG).show();
)
@Override
protected Map<String, String> getParams() throws AuthFailureError
image = getStringImage(bitmap);
String ddrid = retrieveDriverID;
String drname = name;
Map<String, String> params = new HashMap<>();
params.put(KEY_DRIVERID, ddrid);
if(ddrid != null)params.put(KEY_DRIVERID,ddrid);
params.put(KEY_IMAGETYPE, "NBIImg");
params.put(KEY_IMAGE, image);
if(image != null)params.put(KEY_IMAGE,image);
params.put(KEY_NAME, drname);
if(drname != null)params.put(KEY_NAME,drname);
return params;
;
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
This is for when I click the imageView:
public void clicknbi(View view)
showFileChooser("1");
private void showFileChooser(String request)
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
This is the Activity Result:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null)
Uri filePath = data.getData();
try
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imgNBI.setImageBitmap(bitmap);
catch (IOException e)
e.printStackTrace();
Converting image:
public String getStringImage(Bitmap bmp)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
And this is my PHP:
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
$RegistrationDate = date('Y-m-d');
$driverid = $_POST['driverid'];
$imgtype = $_POST['imgtype'];
$image = $_POST['image'];
$name = $_POST['name'];
require_once('dbcon.php');
$pathname = "$name$imgtype";
$path = "Images/$pathname.png";
$actualpath = "http://angkas.net23.net/$path";
//$sql = "INSERT INTO driverregistration (DriverID,RegistrationDate,EnglishProficiency,TagalogProficiency,NBIImg) VALUES ('$driverid','$RegistrationDate','','','$actualpath');";
$sql = "UPDATE driverregistration SET NBIImg = '$actualpath' WHERE DriverID = $driverid;";
if(mysqli_query($con,$sql))
file_put_contents($path,base64_decode($image));
echo "Successfully Registered";
else
echo "Could not insert image";
else
echo 'error';
?>
I am encountering two errors. The first one is the null object reference and the second error is:
java.net.SocketException: sendto failed: EPIPE (Broken pipe)
I'm getting this error in my Toast.
Please post full exception details - or, at a minimum, point out which lines are causing the exception. Also, please make sure that this is the minimal amount of code we need to replicate the problem - I suspect that not all of this code is actually relevant to the issue but it's hard to tell without the exception details.
– EJoshuaS
Aug 17 '16 at 3:18
Try with, <uses-permission android:name="android.permission.INTERNET"/>
– Damini Mehra
Aug 17 '16 at 3:58
@hoomi yes, it is returning a string.
– Eugene Santos
Aug 17 '16 at 5:56
@EJoshuaS Sir, the thing is it's not giving me an exception instead i display it in a TOAST and the "com.android.volley.NoConnectionError: java.net.SocketException: sendto failed: EPIPE (Broken pipe)"
– Eugene Santos
Aug 17 '16 at 5:56
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Is 'getStringImage' returning a string?
– hoomi
Aug 17 '16 at 2:25