I have two activities-
1. MainActivity
2. Display_Activity
The main activity has an inner class "check".
I am trying to access the code written in the inner class "check", by creating its object in the Display_Activity.
According to me I am able to run the code inside the inner class but the Sharedpreference is getting null from somewhere
When I swipe to refresh, the application produces a null object exception. I am not able to get where I am supplying the null. I tried to debug but couldn't find it. Please help if you can.
I have provided with both the activities and the logcat.
Main_Activity.java
public class MainActivity extends Activity {
String value;
String orientation="false";
EditText edtUrl;
String tknnumber;
SharedPreferences sharedpreferences;
String URL="http://192.168.1.101:8080/DoctorApp/newjsp2.jsp?token=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtUrl = (EditText) findViewById(R.id.edtURL);
orientation="true";
}
public void CallURL(View view) {
check check1 = new check();
tknnumber = edtUrl.getText().toString();
boolean result = isNetworkAvailable();
if (!result) {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
} else {
check1.Checking_network(tknnumber);
}
}
public class check{
public void Checking_network(String tkn){
URL = URL.concat(tkn);
(new ParseURL()).execute(new String[]{URL});
}
private class ParseURL extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
StringBuffer buffer = new StringBuffer();
try {
//Log.d("JSwa", "Connecting to [" + strings[0] + "]");
Document doc = Jsoup.connect(strings[0]).get();
String token = doc.getElementById("token").ownText();
String p_name = doc.getElementById("pname").ownText();
String gender = doc.getElementById("gender").ownText();
String city = doc.getElementById("city").ownText();
String date = doc.getElementById("date").ownText();
String doctor=doc.getElementById("doctor").ownText();
String time_left=doc.getElementById("time").ownText();
buffer.append(token+"\n"+p_name+"\n"+gender+"\n"+city+"\n"+date+"\n"+doctor+"\n"+time_left);
} catch (Exception t) {
buffer.delete(0,buffer.length());
buffer.append("Error");
t.printStackTrace();
}
return buffer.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s.equalsIgnoreCase("Error")){
Toast.makeText(MainActivity.this, "Sorry! Something went wrong. Make sure you have entered the correct token number", Toast.LENGTH_LONG).show();
}
else{
try{
callIntent(s);
}
catch(Exception e){
refreshCall(s);}
}}}
}
public boolean isNetworkAvailable(){
ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo=cm.getActiveNetworkInfo();
if(networkInfo!=null && networkInfo.isConnected()){
return true;
}
return false;
}
Context context=this;
void callIntent(String s){
Intent intent;
intent = new Intent(context, display_activity.class);
intent.putExtra("info",s);
intent.putExtra("token_number",tknnumber);
startActivity(intent);}
void refreshCall(String s){
SharedPreferences pref=getSharedPreferences("mydata",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("key", s);
editor.commit();
}}
Display_activity
public class display_activity extends Activity {
TextView token_name,patient,gender,location,date,doctor,token_null,time_left;
String token_received;
SwipeRefreshLayout mSwipeRefreshLayout;
MainActivity mainActivity=new MainActivity();
MainActivity.check abc=mainActivity.new check();
String URL="http://192.168.1.101:8080/DoctorApp/newjsp2.jsp?token=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_activity);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_swipe_refresh_layout);
token_name=(TextView) findViewById(R.id.tkn);
patient=(TextView) findViewById(R.id.patient);
gender = (TextView) findViewById(R.id.gender);
location=(TextView) findViewById(R.id.location);
date=(TextView) findViewById(R.id.date1);
doctor=(TextView) findViewById(R.id.doctor1);
token_null=(TextView)findViewById(R.id.token);
time_left=(TextView)findViewById(R.id.time_left);
Intent intent=getIntent();
String infor=intent.getStringExtra("info");
token_received=intent.getStringExtra("token_number");
breaking(infor);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshContent();
}});}
public void breaking(String a){
String delimiter = "\n";
String[] temp;
temp = a.split(delimiter);
try{
token_name.setText(temp[0]);
patient.setText(temp[1]);
gender.setText(temp[2]);
location.setText(temp[3]);
date.setText(temp[4]);
doctor.setText(temp[5]);
time_left.setText(temp[6]+" mins (approx)");
}
catch (ArrayIndexOutOfBoundsException e){
token_name.setText(a);
e.printStackTrace();
}
}
public void refreshContent(){
boolean result=isNetworkAvailable();
if(!result){
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
}
else {
abc.Checking_network(token_received);
SharedPreferences pref = getSharedPreferences("mydata", Context.MODE_PRIVATE);
String information=pref.getString("key","NA");
breaking(information);
mSwipeRefreshLayout.setRefreshing(false);
}}
public void Callprevious(View view){
Intent intent;
intent = new Intent(this , MainActivity.class);
startActivity(intent);
}
}
logcat On swipe refresh
06-10 20:00:24.399 12012-12012/com.example.nmn.ajsouptry E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.nmn.ajsouptry, PID: 12012
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:169)
at com.example.nmn.ajsouptry.MainActivity.refreshCall(MainActivity.java:144)
at com.example.nmn.ajsouptry.MainActivity$check$ParseURL.onPostExecute(MainActivity.java:115)
at com.example.nmn.ajsouptry.MainActivity$check$ParseURL.onPostExecute(MainActivity.java:56)
I am a new to this website, please give me time to improve. Kindly see my error and if it seems weird , you can simply ignore it. Thank you.