0

Please help me/ I have android studio program

I want to select the id of table user but when I write the code in all activity it is not show me result my application destroy it only work in one activity that I don't need its my database code

I create 3 table

String qry = "create table user (idT1 INTEGER PRIMARY KEY AUTOINCREMENT ,fname TEXT , Lname TEXT, pass TEXT, Phone TEXT, email TEXT,DRorP TEXT)";


 //if user is Patient Create table for patient
 String qry3 =
         "create table patientInformation (idTPatient INTEGER PRIMARY KEY AUTOINCREMENT,age INTEGER , city TEXT, adress TEXT, grup TEXT ,MorF TEXT,idT1 INTEGER  ,FOREIGN KEY (idT1) \n"+
              "  REFERENCES user (idT1) \n"+
                 "         ON DELETE CASCADE \n" +
                 "         ON UPDATE NO ACTION)";


 //if user is DOCTOR Create table for DR
 String qry2 =
         "create table DrInformational (idT2 INTEGER PRIMARY KEY AUTOINCREMENT, pspor TEXT , brwanama TEXT, taman INTEGER  , city TEXT,adress TEXT ,MorF TEXT ,idT1 INTEGER , FOREIGN KEY (idT1) \n" +
                 "      REFERENCES user (idT1) \n" +
                 "         ON DELETE CASCADE \n" +
                 "         ON UPDATE NO ACTION)";



 db.execSQL(qry);
 db.execSQL(qry2);
 db.execSQL(qry3);

Then I have a login page in MainActivity its all codes When I run this its no problem and my program run correctly

 package com.example.myapplication;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    static MyDB myDB;
  //  static Db2 Db2;
    EditText name,pass;
public static String name2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        name=(EditText)findViewById(R.id.username);
        pass=(EditText)findViewById(R.id.password);
        myDB = new MyDB(this);
     //  Db2 = new Db2(this);

    }

    public void loginCheck(View v) {

       String  email = name.getText().toString().trim();
        String passwordd = pass.getText().toString().trim();
        Cursor c = MainActivity.myDB.LoginCheck(email);
        c.moveToFirst();

        if (c == null) {
            Toast.makeText(MainActivity.this, "invalid " + email, Toast.LENGTH_LONG).show();
            name.setText("");
            pass.setText("");
        }
        else {
             name2 = c.getString(0);
            String pass2 = c.getString(1);
            String dr=c.getString(2);


            if (passwordd.equals(pass2)) {

                if(dr.equals("دکتۆر")) {
                    Intent i = new Intent(MainActivity.this, DrActivity.class);
                    i.putExtra("name", name2);
                    startActivity(i);
                }

                else {
                    Intent i = new Intent(MainActivity.this, Patientpage.class);
                    i.putExtra("name", email);
                    startActivity(i);
                    finish();



                }

         }

            else {
                Toast.makeText(MainActivity.this, "invalid ", Toast.LENGTH_LONG).show();
            }
            name.setText("");
            pass.setText("");
        }

    }




    public void register(View view){
        Intent i=new Intent(MainActivity.this, RegisterActivity.class);
     startActivity(i);
    }

    public static String getname(){
        return name2;
    }


}

And I insert some rows its run correctly I don't have problem

But when I want to select the idT1 in table user my program destroy

select statement in my DBMS class

public Cursor selectQ(){
        try{

            String qry= "SELECT MAX(idT1) FROM user";
            SQLiteDatabase db=getWritableDatabase();
            Cursor c=db.rawQuery(qry,null);
            return c;

        }

        catch (Exception e){
            Log.e("MYDB", "login Error",e);
            return null;
        }
    }

And I use this code in my MainActivity class when I write this I have an error

 Cursor a = myDB.selectQ();
        while (a.moveToNext()) {
            String b= a.getString(0);
            System.out.println(b);
        }

this is my exception

01-04 19:21:29.293 1333-1333/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: 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.example.myapplication.MainActivity.onCreate(MainActivity.java:22)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
sandra
  • 23
  • 5
  • 1
    welcome to SO. when you have an exception you should send stack trace from logcat – ygngy Jan 04 '20 at 10:13
  • thanks ... sorry what is this? – sandra Jan 04 '20 at 10:18
  • 1
    when your app stops (throws an exception) you can see error (stack trace) in red color in logcat (at bottom of android studio) – ygngy Jan 04 '20 at 10:23
  • oh thank you sir for your answer – sandra Jan 04 '20 at 19:10
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – forpas Jan 04 '20 at 19:37
  • Change: `Cursor a = db.selectQ();` to `Cursor a = myDB.selectQ();` – forpas Jan 04 '20 at 19:44
  • you have a `NullPointerException` in `onCreate` of `MainActivity` at line 22 – ygngy Jan 04 '20 at 20:37
  • dear 'forpas' thanks for your help ... i don't know what my mistake why i have this error ... and also i change db to myDB sorry i type it wrong but it isn't cause of my error – sandra Jan 04 '20 at 20:41
  • thanks 'bahman' for your answer ... so line 22 is ( Cursor a = myDB.selectQ(); ) why its cause for NullPointerException ? / i do that in another activity it run correctly but i don't know why in MainActivity and some other activity it show me errore :( – sandra Jan 04 '20 at 20:45
  • check `Cursor a = myDB.selectQ()` code to be after `myDB = new MyDB(this)` – ygngy Jan 04 '20 at 23:00
  • i check this its after myDB = new MyDB(this) , please help me why its cause exception – sandra Jan 05 '20 at 10:42
  • your stack trace is not complete because i can not see which item is null and what method is called on a null reference it should be in stack trace. post complete stack trace. also your code in `onCreate` is not send completely because i can not see `Cursor a = myDB.selectQ()` inside it – ygngy Jan 05 '20 at 11:11
  • oh thank you sir for your help i forget write { myDB = new MyDB(this) } its my mistake thank u for your help .... – sandra Jan 05 '20 at 12:10

0 Answers0