Reputation: 1974
I've a JSON like this:
{"AuditScheduleDetailID":12422,"AuditAnswerId":3,"LocalFindingID":9,"LocalMediaID":18,"Files":"adasdaf","ExtFiles":"jpg"}
then, when I'm Copy Paste into android project, that json is changing like :
"{\"AuditScheduleDetailID\":12422,\"AuditAnswerId\":3,\"LocalFindingID\":9," +
"\"LocalMediaID\":18,\"Files\":\"adasdaf\",\"ExtFiles\":\"jpg\"}"
My Code to send data is:
public class MainActivity extends AppCompatActivity {
String send = "{\"AuditScheduleDetailID\":12422,\"AuditAnswerId\":3,\"LocalFindingID\":9," +
"\"LocalMediaID\":18,\"Files\":\"adasdaf\",\"ExtFiles\":\"jpg\"}";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uploadMedia(send);
}
}
public void uploadMedia(String value){
Log.d("uploading ", "test");
Log.d("Test value : ", value);
String urlString = "myURL"; // URL to call
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.connect();
OutputStream out = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter(out, "UTF-8"));
writer.write(URLEncoder.encode(value, "UTF-8"));
writer.flush();
writer.close();
out.close();
urlConnection.connect();
} catch (Exception e) {
Log.d("error upload", e.getMessage());
}
}
but I get an error in logcat like below:
FATAL EXCEPTION: main
Process: com.example.nawadata.sqllite, PID: 8750
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nawadata.sqllite/com.example.nawadata.sqllite.MainActivity}: java.lang.NullPointerException: println needs a message
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:139)
at com.example.nawadata.sqllite.MainActivity.uploadMedia(MainActivity.java:125)
at com.example.nawadata.sqllite.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
can someone help me why I keep getting error java.lang.NullPointerException: println needs a message
? and how to fix it?
when im using postman to send data into api, the return message is success like this:
Upvotes: 0
Views: 173
Reputation: 700
Try this because it seems like value is null.
Log.d("uploading ", "test");
if( !value.equals("") || !value.eqauls(" ") )
{
Log.d("Test value : ", value);
}
if this solution is not working then try Log.e instead of Log.d because Log.e is for use error purposes.
Also change this line in catch.
Log.e("error upload", e.getMessage());
Upvotes: 1
Reputation: 1579
Note: Whenever you paste a JSON response in a string variable, Android Studio automatically do the formatting which is android readable.
Change your activity code like below:
String send = "{\"AuditScheduleDetailID\":12422,\"AuditAnswerId\":3,\"LocalFindingID\":9," +
"\"LocalMediaID\":18,\"Files\":\"adasdaf\",\"ExtFiles\":\"jpg\"}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uploadMedia(send);
}
public void uploadMedia(String value) {
Log.e("uploading ", "test");
Log.e("Test value : ", value);
String urlString = "myURL"; // URL to call
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.connect();
OutputStream out = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(URLEncoder.encode(value, "UTF-8"));
writer.flush();
writer.close();
out.close();
urlConnection.connect();
} catch (Exception e) {
Log.e("error upload", e.getMessage());
}
}
Now run and check, if it works then please let me know.
Your logcat will be like below image.
Upvotes: 0
Reputation: 278
use Log.e for printing error as per guide see this
Replace those lines
Log.d("Test value : ", ""+value);
catch (Exception e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 2781
replace log with this line:
Log.d("Test value : ", value+" ");
and
Log.d ("error upload",e.getMessage()+" ");
Upvotes: 0