0

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:

this

3
  • any idea please? Commented Nov 23, 2017 at 4:01
  • I think this error is caused by Log.d() method. Make sure you are not passing a null parameter into this method. Commented Nov 23, 2017 at 4:19
  • in the catch block try this catch (Exception e) { Log.d("error upload", e.printStackTrace()); } Commented Nov 23, 2017 at 4:55

4 Answers 4

1

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());

Sign up to request clarification or add additional context in comments.

Comments

0

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.

enter image description here

6 Comments

on your logcat image, why that E/Test value have 2 :?
@flix that's because of the string in Log.e. if you remove : from Log.e("Test value : ", value); then it will be like E/Test value: {"AuditScheduleDetailID":12422,"AuditAnswerId":3,"LocalFindingID":9,"LocalMediaID":18,"Files":"adasdaf","ExtFiles":"jpg"}
so the real String of value is including : sign? how can it be like that? actually i dont need that : sign, because i need to post it into rest-api
@flix not the value, but : is included with the identifier for differentiation between tag and msg. In Log.e("Test value", value); "Test value" is the string (identifier/tag) and value is another string (value/msg).
@flix don't worry it is just to debug the app. It won't make any changes to your original value. Please appreciate with upvote if you are satisfied with my answer.
|
0

replace log with this line:

   Log.d("Test value : ", value+" ");

and

  Log.d ("error upload",e.getMessage()+" ");

Comments

0

use Log.e for printing error as per guide see this
Replace those lines

 Log.d("Test value : ", ""+value);


 catch (Exception e) {
            e.printStackTrace();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.