-1

Further adventures into Android Studio!

I did have this working. I have a log in activity when then passes to another one. Quite simple at the moment. Worked perfectly on the emulator. Tried it with my phone attached and it threw a NetworkOnMainException... So, researched that and put the internet log in check into a separate Async thread.

When I execute my app now, when it starts the new activity, it crashes. The odd thing is, if I debug this and step through the code, it works and the second activity is displayed. If I let the app run at it's own speed, it crashes.

My log in button method is:

public void signInButtonClick(View v) throws InterruptedException {

       // Button button=(Button) v;

        String loginSql;

        Globals.userName =   loginID.getText().toString();

        Globals.passWord = loginPW.getText().toString();
        //www.myarraydata.com/app/androidlogin.php?username=X&password=Y
        loginSql = "http://www.mywebsite.com/app/loginscript.php?username="+Globals.userName+"&password="+Globals.passWord ;
        ((Button) v).setText("Signing in...");
        System.out.println(loginSql);
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        try {
            url = new URL(loginSql);

            new loginTask().execute();

            System.out.println(readStream);

        if(readStream.equals("FAIL"))
            {
                loggedIn = false;
            }
            else
            {
                loggedIn = true;
                //Store the list of arrays for this customer in the arrayList global variable
                Globals.arrayList = Arrays.asList(readStream.split("\\s*#\\s*"));

            }

        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }



    if (loggedIn==true)
    {

        startActivity(new Intent(MainActivity.this,LiveStatus.class));
        Thread.sleep(1000);
        finish();
    }
        else
    {
        new AlertDialog.Builder(this)
                .setTitle("Oops...")
                .setMessage("Sign in failed. Please try again.")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // continue with delete
                    }
                })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        System.exit(0);
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

Logcat shows it caused by InvocationTargetExcecption at signInButtonClick and a NullPointerException.

10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: FATAL EXCEPTION: main
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.view.View$1.onClick(View.java:2072)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.view.View.performClick(View.java:2408)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:8816)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:587)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:123)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:521)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:  Caused by: java.lang.reflect.InvocationTargetException
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at app.ictis.com.myarraydaya.MainActivity.signInButtonClick(MainActivity.java:133)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:521)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.view.View$1.onClick(View.java:2067)
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.view.View.performClick(View.java:2408) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:8816) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:587) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:123) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4627) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:521) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:  Caused by: java.lang.NullPointerException
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at app.ictis.com.myarraydaya.MainActivity.signInButtonClick(MainActivity.java:133) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:521) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.view.View$1.onClick(View.java:2067) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.view.View.performClick(View.java:2408) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:8816) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:587) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:123) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4627) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:521) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-11 08:09:07.224 6589-6589/app.ictis.com.myarraydaya E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

Any ideas please?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Tym
  • 89
  • 2
  • 14
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Phantômaxx Oct 11 '15 at 12:16
  • But how does that relate to the fact then when I debug it line by line it works... – Tym Oct 11 '15 at 12:25
  • ... until you arrive to `at app.ictis.com.myarraydaya.MainActivity.signInButtonClick(MainActivity.java:133)` – Phantômaxx Oct 11 '15 at 12:27

1 Answers1

1

Discovered that the loggedIn variable needs assigning when I declare it. That's the null pointer. My

new loginTask().execute();

doesn't seem to be executed properly when in normal mode, only when setpping though... which is odd.

Thatnks to Frank for the pointer (no pun inteneded!)

Tym
  • 89
  • 2
  • 14