-1

LOGCAT Erorrّّّ

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String a.thebook.m_Model.new_book1.getnamebook()' on a null object reference

in java Class

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());
...



}

get values from firebase Database. new_book1

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;
    }

}
Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
Ahmed
  • 23
  • 2
  • 6

3 Answers3

3

You have not initialised your model variable. Hence you are getting a null object error.

Initialise your new_book1 variable and your code will work

Hope this helps :)

Aradhna
  • 933
  • 9
  • 19
1
@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());

}

This will help you.

divyansh ingle
  • 322
  • 1
  • 9
0

Use:

private new_book1 model = new new_book1("myBook", "myself");
W4R10CK
  • 5,300
  • 2
  • 15
  • 28