JSON url not working
<p>I want to call json array from the server </p>

<p>This line works fine for me </p>

<pre><code>JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php");
</code></pre>

<p>but when i try to fetch using my sharedprefences class it does not work and cme with force to to stop , here is that code </p>

<pre><code> JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php?identifier="
                + Test.this.myprefs.getEmail());
</code></pre>

<p>In eclipse it doesn't point any error i mean missing of var deceleration but it doesn't work . Any suggestion ???</p>

<p>Here is what i am seeing in the logcat</p>

<pre><code>05-02 19:49:53.569: ERROR/AndroidRuntime(20721): Uncaught handler: thread main exiting due to uncaught exception
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.os.Looper.loop(Looper.java:123)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread.main(ActivityThread.java:4595)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at java.lang.reflect.Method.invoke(Method.java:521)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at dalvik.system.NativeStart.main(Native Method)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): Caused by: java.lang.NullPointerException
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:37)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     ... 11 more
05-02 19:49:53.778: ERROR/ActivityManager(97): fail to set top app changed!
05-02 19:49:58.048: ERROR/PackageInstallationReceiver(248): Remove /data/local/tmp/com.droidnova.android.howto.optionmenu.apk Fail!
05-02 19:51:41.418: ERROR/AndroidRuntime(20746): ERROR: thread attach failed
05-02 19:51:42.818: ERROR/AndroidRuntime(20754): ERROR: thread attach failed
05-02 19:51:43.438: ERROR/AndroidRuntime(20762): Uncaught handler: thread main exiting due to uncaught exception
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.os.Looper.loop(Looper.java:123)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread.main(ActivityThread.java:4595)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at java.lang.reflect.Method.invoke(Method.java:521)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at dalvik.system.NativeStart.main(Native Method)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): Caused by: java.lang.NullPointerException
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:37)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     ... 11 more
05-02 19:51:45.178: ERROR/ActivityManager(97): fail to set top app changed!
05-02 19:51:54.428: ERROR/AndroidRuntime(20772): ERROR: thread attach failed
05-02 19:51:55.818: ERROR/AndroidRuntime(20780): ERROR: thread attach failed
05-02 19:51:56.528: ERROR/AndroidRuntime(20788): Uncaught handler: thread main exiting due to uncaught exception
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.os.Looper.loop(Looper.java:123)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread.main(ActivityThread.java:4595)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at java.lang.reflect.Method.invoke(Method.java:521)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at dalvik.system.NativeStart.main(Native Method)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): Caused by: java.lang.NullPointerException
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:37)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     ... 11 more
05-02 19:51:58.658: ERROR/ActivityManager(97): fail to set top app changed!
05-02 19:55:31.438: ERROR/AndroidRuntime(20809): ERROR: thread attach failed
05-02 19:55:38.148: ERROR/AndroidRuntime(20820): ERROR: thread attach failed
05-02 19:55:41.638: ERROR/PackageInstallationReceiver(248): Remove /data/local/tmp/com.droidnova.android.howto.optionmenu.apk Fail!
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:29.648: ERROR/wpa_supplicant(139): prepare_filter_struct: type=5
05-02 20:15:32.728: ERROR/wpa_supplicant(139): prepare_filter_struct: type=5
</code></pre>

<p>More more detail here is the Test class </p>

<pre><code>public class Test extends ListActivity  {
     Prefs myprefs = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);
        ArrayList&lt;HashMap&lt;String, String&gt;&gt; mylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();

        JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php?identifier="
            + Test.this.myprefs.getEmail());

        try{

            JSONArray  earthquakes = json.getJSONArray("earthquakes");

            for(int i=0;i&lt;earthquakes.length();i++){                        
                HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();    
                JSONObject e = earthquakes.getJSONObject(i);

                map.put("id",  String.valueOf(i));
                map.put("name", "Earthquake name:" + e.getString("name"));
                map.put("password", "Magnitude: " +  e.getString("password"));
                mylist.add(map);            
            }       
        }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }

        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.test, 
                        new String[] { "name", "magnitude" }, 
                        new int[] { R.id.item_title, R.id.item_subtitle });

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {              
                @SuppressWarnings("unchecked")
                HashMap&lt;String, String&gt; o = (HashMap&lt;String, String&gt;) lv.getItemAtPosition(position);                   
                Toast.makeText(Test.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

            }
        });
    }
</code></pre>

<p>And here is the share preferences class </p>

<pre><code>package com.droidnova.android.howto.optionmenu;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Prefs {

    private SharedPreferences _prefs = null;
    private Editor _editor = null;
    private String _useremailaddress = "Unknown";
    private String _serverurl = "http://chinar.gofreeserve.com/db.php";

    public Prefs(Context context) {
        this._prefs = context.getSharedPreferences("PREFS_PRIVATE", Context.MODE_PRIVATE);
        this._editor = this._prefs.edit();
    }

    public String getValue(String key, String defaultvalue) {
        if (this._prefs == null) {
            return "Unknown";
        }

        return this._prefs.getString(key, defaultvalue);
    }

    public void setValue(String key, String value) {
        if (this._editor == null) {
            return;
        }

        this._editor.putString(key, value);

    }

    public String getEmail() {
        if (this._prefs == null) {
            return "Unknown";
        }

        this._useremailaddress = this._prefs.getString("emailaddress", "noidea");
        return this._useremailaddress;
    }

    public String getServer() {
        if (this._prefs == null) {
            return "http://chinar.gofreeserve.com";
        }

        this._serverurl = this._prefs.getString("serverurl", "http://chinar.gofreeserve.com/");
        return this._serverurl;
    }

    public void setEmail(String newemail) {
        if (this._editor == null) {
            return;
        }

        this._editor.putString("emailaddress", newemail);
    }

    public void setServer(String serverurl) {
        if (this._editor == null) {
            return;
        }
        this._editor.putString("serverurl", serverurl);
    }

    public void save() {
        if (this._editor == null) {
            return;
        }
        this._editor.commit();
    }
}
</code></pre>
