Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我不能在编辑文本中设置文本_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java 我不能在编辑文本中设置文本

Java 我不能在编辑文本中设置文本,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,LOGCAT Erorrّّّ java.lang.NullPointerException:尝试调用虚拟方法 空值上的“java.lang.String a.thebook.m_Model.new_book1.getnamebook()” 对象引用 在java类中 package a.thebook; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompat

LOGCAT Erorrّّّ

java.lang.NullPointerException:尝试调用虚拟方法 空值上的“java.lang.String a.thebook.m_Model.new_book1.getnamebook()” 对象引用

在java类中

package a.thebook;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import a.thebook.m_Model.new_book1;
....


public class my_book  extends AppCompatActivity {




    private new_book1 model;
    private EditText  bookname1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_book_activity);


        EditText bookname1 = (EditText) findViewById(R.id.editTextbookname2);
         bookname1.setText(model.getbookname());
...



}
从firebase数据库获取值。 新书1

package a.thebook.m_Model;


import java.util.Objects;


public class new_book1 {


    public String author;
    public String bookname;

    public new_book1() {
    }
    public new_book1(String bookname ,String author) {
        this.bookname = bookname;
        this.author = author;

    }
    public String getauthor(int author) {

        return this.author;
    }
    public String getbookname() {

        return this.bookname;
    }

}

您尚未初始化
模型
变量。 因此,您将得到一个
null对象
错误

初始化您的
新\u book1
变量,您的代码将正常工作

希望这有帮助:)

使用:

private new_book1 model = new new_book1("myBook", "myself");

这将对您有所帮助。

模型的初始化在哪里?您的模型有空文本,您没有设置bookname并直接使用getbookname您没有初始化类new\u book1,只是创建了引用,不要忘记投票回答:)即使这样也可能会产生NPE,他应该像
private new_book1 model=new new_book1(“书名”、“作者”)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_book_activity);
    model = new new_book1("abc","xyz");

    EditText bookname1 = (EditText) findViewById(R.id.editTextbookname2);
     bookname1.setText(model.getbookname());

}