Reputation: 3133
I want to call json array from the server
This line works fine for me
JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php");
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
JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php?identifier="
+ Test.this.myprefs.getEmail());
In eclipse it doesn't point any error i mean missing of var deceleration but it doesn't work . Any suggestion ???
Here is what i am seeing in the logcat
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
More more detail here is the Test class
public class Test extends ListActivity {
Prefs myprefs = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
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<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
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<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Test.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
And here is the share preferences class
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();
}
}
Upvotes: 0
Views: 566
Reputation: 76536
Your myPrefs var is null
public class Test extends ListActivity {
Prefs myprefs = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
//myprefs can only be null at this point, you need this line or an equivalent for your Prefs class:
myprefs = PreferenceManager.getDefaultSharedPreferences(this);
JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php?identifier="
+ Test.this.myprefs.getEmail());
API:
or
EDIT
Ah yes your using some custom preferences class, so you have to instantiate this class.
// Your choice of
Prefs myprefs = new Prefs();
Prefs myprefs = new Prefs(this);
Prefs myprefs = new Prefs(getApplicationContext());
or if he method your looking to use is static:
Prefs.getEmail();
Have a read of this: Top Ten Java Errors (Number 10 and Number 1!)
Upvotes: 1
Reputation: 80340
You are getting NullPointerException because (most probably) your myprefs
is null.
Where do you initialize this field?
Anyhow, for saving references, instead of creating your own solution, you should look at Shared Preferences.
Upvotes: 2
Reputation: 13541
You have a null pointer exception in your code:
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)
Fix this so at the very least your application will run.
Upvotes: 0