Attempt to invoke virtual method 'void com.basicprofileapp.dataholder.setUsername(java.lang.String)'on a null object refrence

huangapple go评论74阅读模式
英文:

Attempt to invoke virtual method 'void com.basicprofileapp.dataholder.setUsername(java.lang.String)'on a null object refrence

问题

我正在创建一个基本应用程序,该应用程序接受输入并将其存储在Firebase数据库中,并自动递增,但每次都崩溃并显示空指针异常。

我认为我的dataholder.java文件中有一些问题,但我无法解决它。

我已经尝试过更改dataholder文件中的参数。我已经检查过Android清单文件中的Firebase服务。

以下是我的dataholder.java文件:

public class dataholder {

    String username,password1;

    public dataholder(String username, String password1) {
        this.username = username;
        this.password1 = password1;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword1() {
        return password1;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }
}

以及MainActivity:

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {

    EditText name;
    EditText password;
    Button login;

    DatabaseReference reff;
    dataholder obj;

    long maxid = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (EditText) findViewById(R.id.etusername);
        password = (EditText) findViewById(R.id.etpassword);
        login = (Button) findViewById(R.id.Login);

        reff = FirebaseDatabase.getInstance().getReference().child("dataholder");
        reff.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists())
                    maxid = (dataSnapshot.getChildrenCount());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                obj.setUsername(name.getText().toString().trim());
                obj.setPassword1(password.getText().toString().trim());

                reff.child(String.valueOf(maxid + 1)).setValue(obj);
                Toast.makeText(getApplicationContext(), "Data inserted", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
英文:

I was creating a basic application that takes input and stores it in a firebase database with auto incrementing but it crashes everytime giving a nullpointer exception.

I think there is some problem in my dataholder java file but I can't fix it.

I've tried changing arguments in dataholder file.i have checked my android manifest file for firebase services.

Here's my dataholder java file

public class dataholder {
String username,password1;
public dataholder(String username, String password1) {
this.username = username;
this.password1 = password1;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
}

and MainActivity

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class MainActivity extends AppCompatActivity {
EditText name;
EditText password;
Button login;
DatabaseReference reff;
dataholder obj;
long maxid = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.etusername);
password = (EditText) findViewById(R.id.etpassword);
login = (Button) findViewById(R.id.Login);
reff = FirebaseDatabase.getInstance().getReference().child("dataholder");
reff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
maxid = (dataSnapshot.getChildrenCount());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
obj.setUsername(name.getText().toString().trim());
obj.setPassword1(password.getText().toString().trim());
reff.child(String.valueOf(maxid + 1)).setValue("obj");
Toast.makeText(getApplicationContext(), "Data inserted ", Toast.LENGTH_SHORT).show();
}
});
}
}

答案1

得分: 1

你的字段 dataholder obj 从未被设置,因此当你在名为“login”的按钮的 OnClickListener 中访问它时,它将为 null。

在你可以访问 getter/setter 之前,你需要将 obj 赋值为一个实际的值,比如 obj = new dataholder(...)

英文:

Your field dataholder obj is never set, so it be null when you to access it in the OnClickListener for the Button called "login".

You'll need to assign obj to an actual value before you can access the getters/setters, such as obj = new dataholder(...).

huangapple
  • 本文由 发表于 2020年7月26日 15:29:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63097336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定