0

I have this code :

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_log_reg);

    pw=(ProgressBar) findViewById(R.id.wait);
    pw.setVisibility(View.GONE);


    SharedPreferences settings = getSharedPreferences(info.PREFS_NAME, 0); 
    boolean current_status = settings.getBoolean("status",false); 

    String saved_phone;
    String saved_password;
    if(current_status){
        saved_phone = settings.getString("phone","");
        saved_password = settings.getString("password","");
        SyncTask syncTask = new SyncTask(this,"L");
        syncTask.execute("LOGIN", "", "", "", "", saved_phone,saved_password);
    }

the app always crushs, when I remove

 SharedPreferences settings = getSharedPreferences(info.PREFS_NAME, 0); 
        boolean current_status = settings.getBoolean("status",false); 

        String saved_phone;
        String saved_password;
        if(current_status){
            saved_phone = settings.getString("phone","");
            saved_password = settings.getString("password","");
            SyncTask syncTask = new SyncTask(this,"L");
            syncTask.execute("LOGIN", "", "", "", "", saved_phone,saved_password);

I can understand why it crushs, because the SyncTask Class works, when I call it from other classes. I tried to solve problem with debuger , but it can't help me. It is my log:

06-24 10:28:13.226 8479-8479/com.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.LogReg}: java.lang.NullPointerException
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                                                             at android.app.ActivityThread.access$600(ActivityThread.java:130)
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                                                             at android.os.Handler.dispatchMessage(Handler.java:99)
                                                             at android.os.Looper.loop(Looper.java:137)
                                                             at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                             at java.lang.reflect.Method.invoke(Method.java:511)
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                             at dalvik.system.NativeStart.main(Native Method)
                                                          Caused by: java.lang.NullPointerException
                                                             at com.myapp.SyncTask.onPreExecute(SyncTask.java:38)
                                                             at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
                                                             at android.os.AsyncTask.execute(AsyncTask.java:534)
                                                             at com.myapp.LogReg.onCreate(LogReg.java:41)
                                                             at android.app.Activity.performCreate(Activity.java:5008)
                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                                                             at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                                                             at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                             at android.os.Looper.loop(Looper.java:137) 
                                                             at android.app.ActivityThread.main(ActivityThread.java:4745) 
                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                             at java.lang.reflect.Method.invoke(Method.java:511) 
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                                             at dalvik.system.NativeStart.main(Native Method) 

This is my SyncTask:

public class SyncTask extends AsyncTask<String,Void,String> {
    Context ctx;
    String type;
    AlertDialog alertDialog;
    static String info="",name="",surname="";
    private static final String TAG = "myLogs";
    SyncTask(Context ctx, String type) {
        this.ctx = ctx;
        this.type = type;
    }

    @Override
    protected void onPreExecute() {
        if (type.equals("L")) {
            Login.pw.setVisibility(View.VISIBLE);
            Login.ent.setClickable(false);

        }

    }


    @Override
    protected String doInBackground(String... params) {

        String URL_REG = "http://androidsqltest.netau.net/reg.php";
        String URL_LOGIN = "http://androidsqltest.netau.net/login.php";
        if (params[0].equals("REGISTRATION")) {
            try {

                URL url = new URL(URL_REG);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(params[1], "UTF-8") + "&" +
                        URLEncoder.encode("surname", "UTF-8") + "=" + URLEncoder.encode(params[2], "UTF-8") + "&" +
                        URLEncoder.encode("phone", "UTF-8") + "=" + URLEncoder.encode(params[3], "UTF-8") + "&" +
                        URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(params[4], "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                return "Գրանցումը հաջողությամբ ավարտվել է";

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (params[0].equals("LOGIN")) {
            try {

                URL url = new URL(URL_LOGIN);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("cphone", "UTF-8") + "=" + URLEncoder.encode(params[5], "UTF-8") + "&" +
                        URLEncoder.encode("cpassword", "UTF-8") + "=" + URLEncoder.encode(params[6], "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "UTF-8"));
                String response = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    response += line;

                }
                bufferedReader.close();
                IS.close();
                info = response;
                return info;


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
        return "Ստուգեք ինտերնետ կապի առկայությունը";
    }


    @Override
    protected void onProgressUpdate(Void... values) {

        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {


        if (result.equals("Գրանցումը հաջողությամբ ավարտվել է")) {
            Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
            LogReg.pw.setVisibility(View.GONE);
        } else {

            Login.ent.setClickable(true);
            if (info.equals("Համակարգում այդպիսի տվյալներով գրանցում չկա, փորձեք կրկին  ")) {

                    alertDialog = new AlertDialog.Builder(ctx).create();
                    alertDialog.setTitle("Մուտք համակարգ");
                    Login.pw.setVisibility(View.GONE);
                    alertDialog.setMessage(info);
                    alertDialog.show();

            } else {
                    String temp = info;
                int s;
                        for(int i =0;;i++){
                            if (temp.charAt(i)==' '){

                                s=i;
                                break;

                            }
                            name+=temp.charAt(i);

                        }
                for(int i = s+1;i<(temp.length()-1);i++)
                {
                surname+=temp.charAt(i);

                }

                Log.d(TAG, info);
                ctx.startActivity(new Intent(ctx, info.class));
            }
        }
    }
}
arm_1234
  • 1
  • 1

1 Answers1

0

you SyncTask instance is a local variable which gets removed after app goes out of if (current_status){} block, so its reference is null. Define it as Activity's member variable

Alex Shutov
  • 3,128
  • 2
  • 11
  • 11