Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:03:31+00:00 2026-06-18T07:03:31+00:00

I made an activity Messages which is supposed to get JSON data from a

  • 0

I made an activity “Messages” which is supposed to get JSON data from a given URL. I tried making a loop to print the json data, but the problem was somewhere else. I am getting NullPointerException on the JSONArray, “json”.

Messages class:

public class Messages extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView tv = (TextView) findViewById(R.id.tv_messages);
    JSONArray json = JSONfunctions.getJSONfromURL("http://docs.blackberry.com/sampledata.json");

    tv.setText(json.length());
}

JSONfunctions class:

public class JSONfunctions {

    public static JSONArray getJSONfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONArray jArray = null;

        // http post
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag get data string ",
                    "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONArray(result);
        } catch (JSONException e) {
            Log.e("log_tag create object ",
                    "Error parsing data " + e.toString());
        }

        return jArray;
    }
}

Error:

08:11:43.683: Error in http connection
android.os.NetworkOnMainThreadException 08:11:43.693: Error converting
result java.lang.NullPointerException: lock == null 08:11:43.703:
Error parsing data org.json.JSONException: End of input at character 0
of 08:11:43.723: Shutting down VM 08:11:43.733: threadid=1: thread
exiting with uncaught exception (group=0x40a70930) 08:11:43.923: ATAL
EXCEPTION: main 08:11:43.923: java.lang.RuntimeException: Unable to
start activity
ComponentInfo{com.example.sound/com.example.sound.Messages}:
java.lang.NullPointerException 08:11:43.923: at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08:11:43.923: at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08:11:43.923: at
android.app.ActivityThread.access$600(ActivityThread.java:141)
08:11:43.923: at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08:11:43.923: at android.os.Handler.dispatchMessage(Handler.java:99)
08:11:43.923: at android.os.Looper.loop(Looper.java:137) 08:11:43.923:
at android.app.ActivityThread.main(ActivityThread.java:5039)
08:11:43.923: at java.lang.reflect.Method.invokeNative(Native Method)
08:11:43.923: at java.lang.reflect.Method.invoke(Method.java:511)
08:11:43.923: at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08:11:43.923: at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08:11:43.923: at dalvik.system.NativeStart.main(Native Method)
08:11:43.923: Caused by: java.lang.NullPointerException 08:11:43.923:
at com.example.sound.Messages.onCreate(Messages.java:18) 08:11:43.923:
at android.app.Activity.performCreate(Activity.java:5104)
08:11:43.923: at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08:11:43.923: at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

The line 18 is:

tv.setText(json.length());

Prior to the above errors, I also get many:

Unexpected value from nativeGetEnabledTags: 0

The URL I’m using (for testing purposes, http://docs.blackberry.com/sampledata.json) is up and working.
I’m new to Android Developing and JSON.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:03:34+00:00Added an answer on June 18, 2026 at 7:03 am

    This is because either you are performing network operation on main thread which in not allowed android version >= 3.0.

    To solve this either use

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
     StrictMode.setThreadPolicy(policy); 
    

    OR

    Use AsyncTask 
    

    Read from http://developer.android.com/reference/android/os/AsyncTask.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity which I want to use to display data sent from
i have made a few multi activity database applications using sqlite, but i get
Possible Duplicate: How do I pass data from a BroadcastReceiver through to an Activity
I made a splash image to show at the start of my activity.. The
I made an exceedingly simple app consisting of a launch activity (.TestActivity) with nothing
I want to use an activity as dialog and i made the theme of
Made this nice little loop for hiding and showing div's, works as a charm
I made an iphone app to capture image from camera and to set that
I made a Web service in which I have a function to count some
Ive made an app that plays media from an on line source using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.