Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys . this is my code

class ChosseAddress : Fragment() {


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        val vl = inflater.inflate(R.layout.choose_address_layout, container, false)


        val animsec: Animation =
            AnimationUtils.loadAnimation(vl.context, R.anim.anim_for_btn_zoom_out)

        vl.button_back_choose_address.setOnClickListener {

            it.startAnimation(animsec)


            childFragmentManager.beginTransaction()
                .replace(R.id.choose_address_container, HamburgerFragment())
                .commit()

        }

        val database = DataBaseRoom(requireContext())
        val repository = RepositoryCart(database)
        val factoryRoom = FactoryRoom(repository)


        val viewmodel: ViewModelRoom = ViewModelProvider(ViewModelStoreOwner { viewModelStore },
            factoryRoom).get(ViewModelRoom::class.java)

        vl.edit_address.setOnClickListener {

            val mDialogView =
                LayoutInflater.from(context).inflate(R.layout.alertfialog_costume, null)
            val dialogtext = LayoutInflater.from(context).inflate(R.layout.edit_alert_txt, null)

            val mBuilder = AlertDialog.Builder(vl.context)
                .setView(mDialogView)
                .setCustomTitle(dialogtext)
            val show = mBuilder.show()

            mDialogView.edit_manually.setOnClickListener {
                show.dismiss()
                val mydialogview =
                    LayoutInflater.from(context).inflate(R.layout.alertdialog_manually, null)
                val mytextcustome =
                    LayoutInflater.from(context).inflate(R.layout.custome_title_editmanually, null)
                val alert = AlertDialog.Builder(vl.context)
                alert.setView(mydialogview)
                    .setCustomTitle(mytextcustome)
                val myalert = alert.show()



                mydialogview.btn_submit_address.setOnClickListener {

                    val edittxt: String = mydialogview.edit_txt_change_address.text.toString()

                    if (edittxt.isEmpty()) {


                        Toast.makeText(
                            vl.context,
                            "لطفا آدرس رو وارد کرده و دکمه ثبت رو فشار دهید ",
                            Toast.LENGTH_SHORT
                        ).show()

                    } else {


                    val address : String

                        address = edittxt

                        viewmodel.updatetxt(Editaddress(null , address))

                        myalert.dismiss()


                        viewmodel.getalledit().observe(viewLifecycleOwner , Observer {

                            val edit = it.toString()

                            Toast.makeText(context , "$edit" , Toast.LENGTH_SHORT).show()



                        })


                    }
                }
                mydialogview.cancel_btn.setOnClickListener {

                    myalert.dismiss()


                }

            }

        }


        return vl

    }


}


So, When i want to update my room database It gives me error that :

Room.Editaddress.toString()' on a null object reference

What I have tried:

I tried So things  like debugging : the edit text returns the value but when i want to show it with observer give me error . 
In my Room DAO i don't have declar any insert method and just declared an update and getall data method . there is no problem with no defining of insert method. Cause i only want to update the data not to insert any thing . anyone can help with this ? 
Thank's in Advanced
Posted
Updated 27-Aug-20 2:18am

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.

Unfortunately, this problem needs your data, and your code running to work out what is going on, and we don;t have that. So it's going to be up to you: get out the debugger, and you can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
WorldofCode 27-Aug-20 8:32am    
thank's OriginalGriff ... good explain
OriginalGriff 27-Aug-20 9:03am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900