• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointerException Cannot invoke "java.lang.Integer.intValue()"

 
Ranch Hand
Posts: 416
  • Number of slices to send:
    Optional 'thank-you' note:
I've a controller defined like this and getting a NullPointerException Cannot invoke "java.lang.Integer.intValue()" because "this.id" is null (some stack trace shown in the end - had to shorten it as it was exceeding 10k character limit)
while processing the form using Hibernate. By processing, I mean I'm using POST. Since this is first time I'm trying to insert a record, the table is empty so not sure why it's complaining about this.id is null.





Here's my Company class inside com.pending.model:



My Company.hbm.xml file is also inside com.pending.model

CompanyManager interface inside com.pending.service is as follows:



CompanyDAO inside com.pending.dao




CompanyDaoHibernate in com.pending.dao.hibernate




CompanyManagerImpl in com.pending.service.impl





The XML file is defined inside applicationContext-hibernate.xml as follows along with other xml files (not shown below)




And applicationContex.xml contains the following bean :




Here's Company.hbm.xml

 

SQLServer database table design is as follows:

Column Name    Data type       Allow Nulls
id                          int        
name                    varchar(10)
comment              varchar(50)    checkmarked

Where, id is set to primary key

Here is the stacktrace:
 
Bartender
Posts: 2421
13
  • Number of slices to send:
    Optional 'thank-you' note:
Is your Company class annotated with @Entity in your code?
Reference https://www.baeldung.com/jpa-entities
 
Jack Tauson
Ranch Hand
Posts: 416
  • Number of slices to send:
    Optional 'thank-you' note:

Himai Minh wrote:Is your Company class annotated with @Entity in your code?
Reference https://www.baeldung.com/jpa-entities



Nope. Annotations are not used here. I believe the code I'm working on belongs to the time when annotations weren't there. I've included my Company class above as well reference.
 
Jack Tauson
Ranch Hand
Posts: 416
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
It started working. My table was not setup for autoincrement and hence I was getting errors.
 
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:
There you go. Don't you hate it when you provide copious documentation and then the problem is somewhere else? Anyway, good to hear the problem is located and solved.
 
Jack Tauson
Ranch Hand
Posts: 416
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:There you go. Don't you hate it when you provide copious documentation and then the problem is somewhere else? Anyway, good to hear the problem is located and solved.



Yeah, not sure what happened now but it only seems to be working with save() method and not with saveOrUpdate(). With saveOrUpdate it is giving me same error as I got at the start.



Completely confused as why it is happening and same thing worked in the morning.
 
Himai Minh
Bartender
Posts: 2421
13
  • Number of slices to send:
    Optional 'thank-you' note:
Hi, Jack,
Save is fine because the id can be auto generated. But update is not fine. That gives you a clue that the id is not found.
Do you provide the right id to look up in the DB first?
 
Jack Tauson
Ranch Hand
Posts: 416
  • Number of slices to send:
    Optional 'thank-you' note:

Himai Minh wrote:Hi, Jack,
Save is fine because the id can be auto generated. But update is not fine. That gives you a clue that the id is not found.
Do you provide the right id to look up in the DB first?



Hi Himai,
Thanks for your response. So I am only doing an insert so could you tel me why update is coming into picture here? Also, what id it is looking to find - I am not understanding this part. Since I'm just inserting the records, I am not providing any id since it's autogenerated in my SQL server table as I've set it as an Identity.

Pease let me know if I can answer any questions.
 
Himai Minh
Bartender
Posts: 2421
13
  • Number of slices to send:
    Optional 'thank-you' note:
Hi, Jack,
To my understanding, your issue comes from saveOrUpdate(). I believe  you are trying to update a record while that record has not even existed yet.
 
Jack Tauson
Ranch Hand
Posts: 416
  • Number of slices to send:
    Optional 'thank-you' note:

Himai Minh wrote:Hi, Jack,
To my understanding, your issue comes from saveOrUpdate(). I believe  you are trying to update a record while that record has not even existed yet.



Hmm, that part is still confusing me as I'm just sending name and comment from the form, and in this case saveOrUpdate() should act as save(). So not sure why it is looking at an update.
 
Himai Minh
Bartender
Posts: 2421
13
  • Number of slices to send:
    Optional 'thank-you' note:
If the name and comment are new data, then try to use save instead of saveOrUpdate.
 
Jack Tauson
Ranch Hand
Posts: 416
  • Number of slices to send:
    Optional 'thank-you' note:

Himai Minh wrote:If the name and comment are new data, then try to use save instead of saveOrUpdate.



Yeah, that's what I am doing right now until I figure this out. But I was hoping saveOrUpdate should hand handed that such that I won't have to use saveOrUpdate once I go for updating something.
 
Himai Minh
Bartender
Posts: 2421
13
  • Number of slices to send:
    Optional 'thank-you' note:
But when you need to update a comment, you should first retrieve the comment ID and then update the comment.
 
Jack Tauson
Ranch Hand
Posts: 416
  • Number of slices to send:
    Optional 'thank-you' note:
Yes, Thanks Minh.

Good news is that I created a separate table and then new classes and mapping files and it works fine now with saveOrHibernate. Not sure why above code was creating issues but I didn't bother to spend more time on it as my new attempt worked.
 
Himai Minh
Bartender
Posts: 2421
13
  • Number of slices to send:
    Optional 'thank-you' note:
Hi Jack,
So, what is your new approach?
Do you separate the save and update operations ?
For example, the save operation is for creating a new comment.
The update is for updating an existing comment.
 
Jack Tauson
Ranch Hand
Posts: 416
  • Number of slices to send:
    Optional 'thank-you' note:

Himai Minh wrote:Hi Jack,
So, what is your new approach?
Do you separate the save and update operations ?
For example, the save operation is for creating a new comment.
The update is for updating an existing comment.



I'm still using saveOrHibernate(). The only things that I changed are the new table with new column names. Based on that I modified the code.
 
Himai Minh
Bartender
Posts: 2421
13
  • Number of slices to send:
    Optional 'thank-you' note:
Hi Jack,
I read this article https://www.digitalocean.com/community/tutorials/hibernate-session-merge-vs-update-save-saveorupdate-persist-example
I don't see saveOrHibernate() method.
 
Jack Tauson
Ranch Hand
Posts: 416
  • Number of slices to send:
    Optional 'thank-you' note:

Himai Minh wrote:Hi Jack,
I read this article https://www.digitalocean.com/community/tutorials/hibernate-session-merge-vs-update-save-saveorupdate-persist-example
I don't see saveOrHibernate() method.


Sorry, I meant saveOrUpdate() - just like I have shown in my code
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |