Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I am building an app from start I need to say I am beginner in this area . So until now my app on button click open a popup dialog where I can add names and delete them. but the problem comes when I add a new item and try to delete one, give me crash I will put link below . It gives me NullPointerException , on a null object reference. I think problem it s at databasehandler when it need to refresh data but idk for sure. Really appreciate any comment.

https://pastebin.com/MgMbUuNP

and here I put error

https://pastebin.com/x0X1Vh1R

and this is database

https://pastebin.com/nwjBfmyn

What I have tried:

I don't really understand where I get wrong I think in databasehandler
Posted
Updated 14-May-21 5:38am
Comments
Richard MacCutchan 14-May-21 12:01pm    
The error message states:
at com.example.drinkingtime.Treburi.DataBaseHandler.deleteContact(DataBaseHandler.java:80)

However your post of DataBaseHandler.java contains only 70 lines.
David Crow 15-May-21 11:43am    
I'm almost sure it is a context issue. In scanning your code, you assign a value to RecyclerViewAdapter.context in the constructor. Later on in the adapter's ViewHolder constructor, you assign it a different value. Then in the Save button's onClick() handler, you are using that context to create a RecyclerViewAdapter object.

I would start by removing the reassigmnment in the ViewHolder class.

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 yesterday's 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.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. 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, VS 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
George Machis 14-May-21 11:41am    
Thanks for reply. I code in Inteliji andorid studio i can put my cod in visualstudio to see if that? Thanks for advice
OriginalGriff 14-May-21 11:54am    
No, just google for "Android studio debugger" and you should find all the instructions you need.
George Machis 14-May-21 12:21pm    
I used debug like you said and my context form recyclerView it s null there it s problem but how i can solve it ? Thanks
OriginalGriff 14-May-21 12:44pm    
So now you go back through your code and find out why: What was supposed to give it a value, and why didn't it work?

Debugger time! Follow the code back until you find the problem - you ar ethe only one who can do that, as you are the only one who can run the code in the context of your whole application.
George Machis 14-May-21 13:27pm    
yes I resolve a part of problem. I used try and catch
But still my context it s null and i tried to use in catch block context.getApplicationContext();
but still give me error can you say me who to instantiate context? Thanks

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