-4

I get an error like this. The application closes.

enter image description here

Here is my code:

BackgroundTask Class,

public class BackgroundTask extends AsyncTask<String,Void,String> {
AlertDialog alertDialog;
Context ctx;
BackgroundTask(Context ctx)
{
    this.ctx = ctx;
}
@Override
protected void onPreExecute(){
    alertDialog = new AlertDialog.Builder(ctx).create();
    alertDialog.setTitle("Login Information....");
}
@Override
protected String doInBackground(String... params) {
    String reg_url = "http://127.0.0.1/Register.php";
    String login_url = "http://127.0.0.1/Login.php";
    String method = params[0];
    if (method.equals("register"))
    {
        String isim = params [1];
        String soyisim = params[2];
        String plaka = params[3];
        String saseno = params[4];
        String email = params[5];
        String sifre = params[6];
        try {
            URL url = new URL(reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            //httpURLConnection.setDoInput(true);
            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
            String data = URLEncoder.encode("isim","UTF-8") +"="+URLEncoder.encode(isim,"UTF-8")+"&"+
            URLEncoder.encode("soyisim","UTF-8") +"="+URLEncoder.encode(soyisim,"UTF-8")+"&"+
            URLEncoder.encode("plaka","UTF-8") +"="+URLEncoder.encode(plaka,"UTF-8")+"&"+
            URLEncoder.encode("saseno","UTF-8") +"="+URLEncoder.encode(saseno,"UTF-8")+"&"+
            URLEncoder.encode("email","UTF-8") +"="+URLEncoder.encode(email,"UTF-8")+"&"+
            URLEncoder.encode("sifre","UTF-8") +"="+URLEncoder.encode(sifre,"UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();
            InputStream IS = httpURLConnection.getInputStream();
            IS.close();
            //httpURLConnection.connect();
            httpURLConnection.disconnect();
            return  "Registration Success...";
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else if (method.equals("login"))
    {
        String loginmail = params[1];
        String loginsifre = params[2];
        try {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)
            url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
        String data = URLEncoder.encode("loginmail","UTF-8")+"="+URLEncoder.encode(loginmail,"UTF-8")+"&"+
                URLEncoder.encode("loginsifre","UTF-8")+"="+URLEncoder.encode(loginsifre,"UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String response = "";
            String line = "";
            while ((line = bufferedReader.readLine())!=null)
            {
                response+= line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return response;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}
@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
    if (result.equals("Registration Success..."))
    {
        Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
    }
    else
    {
        alertDialog.setMessage(result);
        alertDialog.show();
    }
}
}


Login Javaclass,

package com.example.serdar.tipoff;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;

public class Login extends Activity {
EditText ET_EMAİL_LOGİN,ET_SİFRE_LOGİN;
String loginmail,loginsifre;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_login);

    ET_EMAİL_LOGİN = (EditText)findViewById(R.id.etmaillogin);
    ET_SİFRE_LOGİN = (EditText)findViewById(R.id.etsifrelogin);
}
public void Regkayit (View view)
{
    startActivity(new Intent(this, Register.class));
}
public void LogGiris ( View view)
{
    loginmail = ET_EMAİL_LOGİN.getText().toString();
    loginsifre = ET_SİFRE_LOGİN.getText().toString();
    String method = "login";
    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute(method,loginmail,loginsifre);
}
}


Register Javaclass,

package com.example.serdar.tipoff;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;

public class Register extends Activity {
EditText    ET_İSİM_REG,ET_SOYİSİM_REG,ET_PLAKA_REG,ET_SASENOREG,ET_EMAİL_REG,ET_SİFRE_REG;
String isim,soyisim,plaka,saseno,email,sifre;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_register);

    ET_İSİM_REG = (EditText)findViewById(R.id.etisimreg);
    ET_SOYİSİM_REG = (EditText)findViewById(R.id.etsoyisimreg);
    ET_PLAKA_REG = (EditText)findViewById(R.id.etplakareg);
    ET_SASENOREG = (EditText)findViewById(R.id.etsasenoreg);
    ET_EMAİL_REG = (EditText)findViewById(R.id.etmailreg);
    ET_SİFRE_REG = (EditText)findViewById(R.id.etsifrereg);
}
public void Regkaydet (View view)
{
    isim = ET_İSİM_REG.getText().toString();
    soyisim = ET_SOYİSİM_REG.getText().toString();
    plaka = ET_PLAKA_REG.getText().toString();
    saseno = ET_SASENOREG.getText().toString();
    email = ET_EMAİL_REG.getText().toString();
    sifre = ET_SİFRE_REG.getText().toString();
    String method = "register";
    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute(method,isim,soyisim,plaka,saseno,email,sifre);
    finish();
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

I think in your code you are comparing two strings using equals function like String1.equals(String2) in your case String1 is null (not initialized), and it is the root cause of this exception.

For example:

String string1 = getInputText();
boolean status = false;
if(string1 != null) {
    status = string1.equals(string2);
}

So please check and ensure String1 is not null before calling String1.equals(String2).

halfer
  • 19,824
  • 17
  • 99
  • 186
Ranjith KP
  • 858
  • 6
  • 18
0

As pointed out earlier you are comparing two strings with Equal Method. e.g string1.equals(string2)

But in your case string1 is null. See the below example to understand it properly.

What should be done

String string1 = "hello";
String string2 = "hello";
string1.equals(string2) will return true

What you are doing

 String string1;
 String string2 = "hello";

string1.equals(string2) will crash the application as string1 is null. Equals Method is not applicable on null because NUll can't be compared with other.

Tulsi
  • 719
  • 7
  • 15
  • Minor detail: it is quite possible to compare a null with a string. The reason `string1.equals()` cannot be invoked is that a null variable cannot point to a viable instance or method, and so the run time environment does not know how to handle that. – halfer Jan 02 '17 at 11:54
  • I could not understand the answers Send my codes..pleasee – Serdar Recepoğlu Jan 02 '17 at 12:21