Register
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
public class Register extends AppCompatActivity {
EditText username;
EditText email;
EditText password;
EditText repass;
EditText role;
Button register;
VideoView videologin2;
TextView change2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
username=(EditText) findViewById(R.id.edtUsername);
email=(EditText)findViewById(R.id.edtGmail);
password=(EditText) findViewById(R.id.edtPassword);
repass=(EditText) findViewById(R.id.edtRepass);
role=(EditText)findViewById(R.id.edtRole) ;
register=(Button) findViewById(R.id.btnRegister);
change2=(TextView)findViewById(R.id.Change2);
videologin2=(VideoView) findViewById(R.id.videologin2);
role.setText("user");
role.setEnabled(false);
String path="android.resource://com.example.myapplication/"+R.raw.video2;
Uri uri=Uri.parse(path);
videologin2.setVideoURI(uri);
videologin2.start();
videologin2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(false);
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(username.getText().toString().equals("") || password.getText().toString().equals("")
&& email.getText().toString().equals("")){
Toast.makeText(Register.this, "Vui lòng nhập thông tin còn thiếu", Toast.LENGTH_LONG).show();
}
if (username.getText().toString().length() < 5) {
Toast.makeText(Register.this, "Tên tài khoản phải dài hơn 5 kí tự", Toast.LENGTH_LONG).show();
}
else if (password.getText().toString().length() < 8) {
Toast.makeText(Register.this, "Mật khẩu phải dài hơn 8 kí tự", Toast.LENGTH_LONG).show();
}
else if (!(email.getText().toString().contains("@") && email.getText().toString().contains("gmail.com"))) {
Toast.makeText(Register.this, "Hãy nhập đúng định dạng gmail", Toast.LENGTH_LONG).show();
}
else if (!(password.getText().toString().equals(repass.getText().toString()))) {
Toast.makeText(Register.this, "Mật khẩu không khớp", Toast.LENGTH_LONG).show();
}
else {
ContentValues values = new ContentValues();
values.put("Username", username.getText().toString());
values.put("Password", password.getText().toString());
values.put("gmail", email.getText().toString());
values.put("role", role.getText().toString());
long kq = Login.database.insert("Users",null, values);
Intent b = new Intent(Register.this, Login.class);
Toast.makeText(Register.this, "Đăng kí thành công", Toast.LENGTH_LONG).show();
startActivity(b);
}
}
});
change2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent b=new Intent(Register.this,Login.class);
startActivity(b);
}
});
}
}
Login
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class Login extends AppCompatActivity {
EditText username;
EditText password;
Button login;
VideoView videologin;
public String DATABASE_NAME="Mydatabase";
public String DB_SUFFIX_PATH="/databases/";
public static SQLiteDatabase database= null;
TextView change;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username=(EditText) findViewById(R.id.edtUsername);
password=(EditText) findViewById(R.id.edtPassword);
login=(Button) findViewById(R.id.login);
change=(TextView)findViewById(R.id.Change);
videologin=(VideoView) findViewById(R.id.videologin);
proccessCopy();
String path="android.resource://com.example.myapplication/"+R.raw.video;
Uri uri=Uri.parse(path);
videologin.setVideoURI(uri);
videologin.start();
videologin.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(false);
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
String etname = username.getText().toString();
database=openOrCreateDatabase(DATABASE_NAME,MODE_PRIVATE,null);
Cursor cursor=database.rawQuery("select * from Users where Username='"+etname+"'",null);
while(cursor.moveToNext()) {
Integer ma=cursor.getInt(0);
String name=cursor.getString(1);
String pass=cursor.getString(2);
String role=cursor.getString(4);
if((username.getText().toString().equals(name))&&
(password.getText().toString().equals(pass))){
if(role.equals("admin")){
Intent c=new Intent(Login.this,Admin.class);
startActivity(c);
}
else{
Intent d=new Intent(Login.this, userPage.class);
startActivity(d);
}
}
else if(username.getText().toString().equals(name)
&&!password.getText().toString().equals(pass)){
Toast.makeText(Login.this,"Sai mật khẩu",Toast.LENGTH_LONG).show();
}
else if(username.getText().toString().equals("")&&password.getText().toString().equals("")){
Toast.makeText(Login.this,"Vui lòng nhập thông tin",Toast.LENGTH_LONG).show();
}
else if(cursor.getCount()==0){
Toast.makeText(Login.this,"Tên tài khoản không tồn tại",Toast.LENGTH_LONG).show();
}
}
}
});
change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent a=new Intent(Login.this,Register.class);
startActivity(a);
}
});
}
public String getDatabasePath(){
return getApplicationInfo().dataDir+DB_SUFFIX_PATH+DATABASE_NAME;
}
private void proccessCopy(){
try {
File file=getDatabasePath(DATABASE_NAME);
if(!file.exists()){
copyDatabaseFromAssets();
Toast.makeText(this, "Copy successful", Toast.LENGTH_SHORT).show();
}
}
catch (Exception ex){
Toast.makeText(this, "Copy Database Fail", Toast.LENGTH_SHORT).show();
}
}
private void copyDatabaseFromAssets() {
try {
InputStream inputFile=getAssets().open(DATABASE_NAME);
String outputFileName=getDatabasePath();
File file= new File(getApplicationInfo().dataDir+DB_SUFFIX_PATH);
if(!file.exists())
file.mkdir();
OutputStream outFile= new FileOutputStream(outputFileName);
byte []buffer= new byte[1024];
int length;
while ((length=inputFile.read(buffer))>0)
outFile.write(buffer,0,length);
outFile.flush();
outFile.close();
inputFile.close();
}catch (Exception e){
}
}
}
User class
package com.example.myapplication;
import java.io.Serializable;
public class User implements Serializable {
private Integer ID_User;
private String Username;
private String Password;
private String gmail;
private String role;
public User() {
}
public User(Integer ID_User, String username, String password, String gmail, String role) {
this.ID_User = ID_User;
Username = username;
Password = password;
this.gmail = gmail;
this.role = role;
}
public Integer getID_User() {
return ID_User;
}
public void setID_User(Integer ID_User) {
this.ID_User = ID_User;
}
public String getUsername() {
return Username;
}
public void setUsername(String username) {
Username = username;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getGmail() {
return gmail;
}
public void setGmail(String gmail) {
this.gmail = gmail;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
i tried to insert the values to my database but it always appear the
FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 10120
java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference
at com.example.myapplication.Register$2.onClick(Register.java:82)
at android.view.View.performClick(View.java:7659)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1211)
at android.view.View.performClickInternal(View.java:7636)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:30156)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
could anyone tell me the problem of my code? i'm very apreciate