gazzler gazzler - 5 months ago 34
MySQL Question

Android Login not connecting with DB

I'm using this tutorial to make an Android login application. I have followed all his steps and successfully am able to load the application on the emulator or my phone, however when I try and create an account I get an error - I followed the instructions he provided stating how to test the system, referencing to here:



  1. Testing the App



For a beginner it will be always difficult to run this project for the first time. But don’t worry, the following steps will helps you testing this app. (The ip address looks like 192.168.0.100)

Make sure that both devices (the device running the PHP project and the android device) are on the same wifi network.

Give correct username , password and database name of MySQL in Config.php

Replace the URL ip address of URL_LOGIN and URL_REGISTER in AppConfig.java with your machine ip address. You can get the ip address by running ipconfig in cmd


my IPv4 address is 192.168.0.11,
So I added that to the AppConfig file as follows:

package android.loginfirstattempt.com.loginfirstattempt.app;


public class AppConfig {
// Server user login url
public static String URL_LOGIN = "http://192.168.0.11/android_login_api/login.php";


// Server user register url
public static String URL_REGISTER = "http://192.168.0.11/android_login_api/register.php";

}


I am using Android Studio's built in emulator so both devices are on the same network.

I am using Lampp and I'm under the assumption by default this is the username and password:

<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "android_api");
?>


I started the local servers and tested to make sure they were running by loading up a demo page.

I've already also created the MySQL database and table as shown in his example.

My file structure for the server side is shown in this picture

But when I try create a User I get this crash:


beginning of crash


06-26 19:12:13.205 2668-2668/android.loginfirstattempt.com.loginfirstattempt E/AndroidRuntime: FATAL EXCEPTION: main
Process: android.loginfirstattempt.com.loginfirstattempt, PID: 2668
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.loginfirstattempt.com.loginfirstattempt.app.AppController.addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference
at android.loginfirstattempt.com.loginfirstattempt.activity.RegisterActivity.registerUser(RegisterActivity.java:189)
at android.loginfirstattempt.com.loginfirstattempt.activity.RegisterActivity.access$300(RegisterActivity.java:35)
at android.loginfirstattempt.com.loginfirstattempt.activity.RegisterActivity$1.onClick(RegisterActivity.java:84)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


I am unable to find any obvious error and was wondering if someone could help!

Answer

You didn't add the AppController to the application tag in the AndroidManifest.xml as explained in the tutorial. Your AppController now doesn't get called during the initialization of your app and isn't initialized. When you call AppController.getInstance().addToRequestQueue(strReq, tag_string_req); inside your registerUser method AppController.getInstance() returns null. This causes your nullpointerException. Add the AppController to the manifest and your code should be working.

Your application tag should look like this:

 <application
    android:name="com.loginfirstattempt.app.AppController"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >