1

Here is my java codes......

public class Account extends Activity 
{ 

private Database v1;
EditText telefon,sifre1,sifre2,isim,mail,kod;
Button kaydet;
@TargetApi(11)
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_account);
    getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#586361")));
    getActionBar().setTitle("FindMe");
    telefon=(EditText)findViewById(R.id.etTelefon);
    sifre1=(EditText)findViewById(R.id.etPass);
    sifre2=(EditText)findViewById(R.id.etPass2);
    isim=(EditText)findViewById(R.id.etUserName);
    mail=(EditText)findViewById(R.id.etMail);
    kod=(EditText)findViewById(R.id.PhoneCode);
    kaydet=(Button)findViewById(R.id.btnAAccount);

    kaydet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            kaydet2(telefon.getText().toString(), sifre1.getText().toString(), sifre2.getText().toString(), isim.getText().toString(), mail.getText().toString(), kod.getText().toString());
        }
    });


}

public final static boolean isValidEmail(CharSequence target) {
    if (TextUtils.isEmpty(target)) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
}


public void kaydet2(String tel, String sifre1, String sifre2, String isim, String mail, String kod) {

    if(!kod.contains("+")){
        kod="+"+kod;
    }

    if (tel.isEmpty() || sifre1.isEmpty() || sifre2.isEmpty() || isim.isEmpty() || mail.isEmpty() || kod.isEmpty()) {
        Toast.makeText(getApplicationContext(), "All field must be filled...", Toast.LENGTH_SHORT).show();
    } else if (!sifre1.equals(sifre2)) {
        Toast.makeText(getApplicationContext(), "Passwords don't match...", Toast.LENGTH_SHORT).show();
    } else if (!isValidEmail(mail)) {
        Toast.makeText(getApplicationContext(), "E-mail address is invalid...", Toast.LENGTH_SHORT).show();
    }
      else if (!PhoneNumberUtils.isGlobalPhoneNumber(kod+tel)) {
        Toast.makeText(getApplicationContext(), "Phone number is invalid...", Toast.LENGTH_SHORT).show();
    }
    else {
        try {
            Database v1 = new Database(this);
            SQLiteDatabase db = v1.getWritableDatabase();
            ContentValues con = new ContentValues();
            con.put("Email", mail);
            con.put("Name", isim);
            con.put("Phone", kod + tel);
            con.put("Password", sifre1);
            db.insertOrThrow("Account", null, con);
            db.close();
            Toast.makeText(getApplicationContext(), "Congratulation...", Toast.LENGTH_SHORT).show();
        } catch (SQLException e) {
            Toast.makeText(getApplicationContext(), "Error occurred in recording...", Toast.LENGTH_SHORT).show();
        }
    }
}

Here is my layout

<LinearLayout

    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:id="@+id/linearLayout"
    android:paddingBottom="100dp">

    <EditText
        android:id="@+id/etMail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="E-Mail"
        android:textColorHint="#586361"
        android:inputType="text"
        android:ems="15"
        android:paddingTop="80dp" />
    <EditText
        android:id="@+id/etUserNameAccount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="-2dp"
        android:hint="Name"
        android:textColorHint="#586361"
        android:paddingTop="20dp" />
    <EditText
        android:id="@+id/etPassAccount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="-2dp"
        android:hint="Password"
        android:textColorHint="#586361"
        android:password="true"
        android:paddingTop="20dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="-2dp"
        android:id="@+id/etPass2"
        android:hint="Password"
        android:textColorHint="#586361"
        android:password="true"
        android:paddingTop="20dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <EditText
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:paddingTop="20dp"
            android:hint="(+)  Code"
            android:textColorHint="#586361"
            android:id="@+id/PhoneCode"/>

        <EditText
            android:layout_width="227dp"
            android:layout_height="50dp"
            android:id="@+id/etTelefon"
            android:hint="Phone"
            android:textColorHint="#586361"
            android:paddingTop="20dp"/>


    </LinearLayout>
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent" android:orientation="horizontal"
        android:paddingTop="20dp"
        android:paddingLeft="90dp"
        android:id="@+id/linearLayout2">

        <Button
            android:layout_width="147dp"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_margin="4dp"
            android:background="#00A3DA"
            android:text="Create Account"
            android:id="@+id/btnAAccount"
            android:layout_below="@+id/etPass"
            android:layout_centerHorizontal="true"/>
    </LinearLayout>

</LinearLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Account"
    android:id="@+id/MarkaTextView"
    android:layout_alignParentRight="false"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="false"
    android:layout_alignParentLeft="false"
    android:layout_alignParentTop="false"
    android:layout_alignWithParentIfMissing="false"
    android:paddingRight="90dp"
    android:textColor="#586361"
    android:typeface="sans"
    android:textSize="25dp"
    android:textStyle="bold|normal"
    android:fontFamily="@string/app_name"
    android:paddingTop="50dp" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/TxtWarning"
    android:textColor="#586361"
    android:typeface="sans"
    android:textSize="15dp"
    android:textStyle="bold|normal"
    android:fontFamily="@string/app_name"
    android:text="All Field Must Be Filled!"
    android:paddingTop="450dp"
    android:paddingLeft="92dp"
    android:textIsSelectable="false"
    android:width="150dp" />

and this is my logcat......

08-06 03:55:23.149    4718-4718/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.example.emre.findme.Account$1.onClick(Account.java:42)
        at android.view.View.performClick(View.java:4240)
        at android.view.View$PerformClick.run(View.java:17721)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

I get NullPointerException error this codes >>>

kaydet2(telefon.getText().toString(), sifre1.getText().toString(), sifre2.getText().toString(), isim.getText().toString(), mail.getText().toString(), kod.getText().toString()); 

Please help me

4 Answers 4

1

Neither of these items are defined in your layout

sifre1=(EditText)findViewById(R.id.etPass);
isim=(EditText)findViewById(R.id.etUserName);

So any code which uses variables sifire1 and isim will throw a null pointer exception

Sign up to request clarification or add additional context in comments.

Comments

0

I think this is sifre1=(EditText)findViewById(R.id.etPass);. There is no view in your layout that has @+id/etPass. That's why it throws NullPointerException when your try to do this code sifre1.getText().toString(). Kindly check it. Hope this helps you :)

Comments

0

In some cases you may have null view due to your view is in another layout. In this case use inflate method. Some sort of:

public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_account);

       View secondLayout = Inflater.inflate(R.layout.secondLayout, null, false);
       View view = secondLayout.findViewById(R.id.yourView);

       // yourView is in secondLayout xml 

Comments

0

One of the views (telefon, sifre1, sifre2 ...) is null. Use the debugger or log messages to check whiche one.

1 Comment

I'm new in android.How to use debugger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.