Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Nullpointerexception For Getastext In Propertyeditor

spring hibernate property-editor spring-mvc null

  • Please log in to reply
9 replies to this topic

#1 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 09 May 2012 - 02:21 PM

My hibernate mapping requires me to make a custom PropertyEditor for converting a String into an Object type (Document). The setAsText method is working fine and I'm able to save a document number to the database but when I try to display the document number it displays what i believe is an object address (testapp.domain.Document@eeccff..or something similar). When I add a getAsText method to the PropertyEditor it gives me the following error:


java.lang.NullPointerException
testapp.controller.DocumentRevisionController$DocumentPropertyEditor.getAsText(DocumentRevisionController.java:59)
Mapping:



    @OneToMany(mappedBy="document_number", cascade = CascadeType.ALL)
private Set<DocumentRevision> documentRevisions;

public void setDocumentRevisions(Set<DocumentRevision> documentRevisions){
    this.documentRevisions = documentRevisions;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="DOCUMENT_NUMBER")
private Document document_number; 

public void setDocument_number(Document document_number){
    this.document_number = document_number;
}

public Document getDocument_number(){
    return document_number;
}

part of the Controller:


 class DocumentPropertyEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        Document value =  documentService.retrieveDocumentNumber(text);
        setValue(value);
    }

    @Override
    public String getAsText() {
        Document d = (Document) getValue();
        return d != null ? String.valueOf(d.getDocumentNumber()) : "";
    }

}


@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Document.class, new DocumentPropertyEditor());
}


@RequestMapping(value="/add", method=RequestMethod.GET)
public String getDocumentRevision(Model model) {
DocumentRevision documentRevision = new DocumentRevision();
model.addAttribute("documentRevisionAttribute", documentRevision);
model.addAttribute("documentNumberList", documentService.retrieveAllDocumentNumbers());

return "new-documnent-revision";
}


@RequestMapping(value="/add", method=RequestMethod.POST)
public String postDocumentRevision(@ModelAttribute("documentRevisionAttribute") @Valid DocumentRevision documentRevision, BindingResult result) {

if(result.hasErrors()){
    return "new-document-revision";
}

documentRevisionService.createDocumentRevision(documentRevision);
return "redirect:/testapp/document-revision/list";  
}

Method that retrieves DocumentNumbers:

public void saveDocumentRevision(DocumentRevision documentRevision) {
    this.sessionFactory.getCurrentSession().save(documentRevision); 
}

Can anyone see where I've made a mistake and why the getAsText method gives the exception? Thank you for your help!

/D
  • 0

#2 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 09 May 2012 - 04:09 PM

what variable on line 59 has a null value? Find that variable and then find out why it does not have a valid non-null value.
  • 0

#3 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 10 May 2012 - 01:25 AM

what variable on line 59 has a null value? Find that variable and then find out why it does not have a valid non-null value.


It's this line:
return d != null ? String.valueOf(d.getDocumentNumber()) : "";
I'm guessing it's the "d" variable that has null value although I don't understand why. Does anyone know?
  • 0

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 10 May 2012 - 02:42 AM

d most likely is not null, but d.getDocumentNumber() returns null
  • 0

#5 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 10 May 2012 - 03:29 AM

After adding this to the getAsText method it resolves the nullpointerexception:

	  if(getValue() == null){
    return null;
   }

But my problem is now how to be able to display the documentNumber as a String when I display the documentRevision table. Now it gets displayed as: testapp.domain.Document@1f93466.
What can i do to resolve this issue?
  • 0

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 10 May 2012 - 03:30 AM

toString method of Document?
  • 0

#7 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 10 May 2012 - 03:48 AM

testapp.domain.Document@1f93466.

That's the value returned by the default toString() method. Add a toString() method to the Document class that returns the String you want to see printed.
  • 0

#8 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 10 May 2012 - 04:13 AM

That's the value returned by the default toString() method. Add a toString() method to the Document class that returns the String you want to see printed.


If I add toString to the getDocumentNumber method in the Document.java as such:

public String getDocumentNumber(){
		return documentNumber.toString();
	}

then i get the following error:
org.springframework.beans.InvalidPropertyException: Invalid property 'documentNumber' of bean class [testapp.domain.Document]: Getter for property 'documentNumber' threw exception; nested exception is java.lang.reflect.InvocationTargetException

And i cant add it to the getter in the DocumentRevision.java since it needs to return a Document type.

How should i do it?
  • 0

#9 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 10 May 2012 - 04:18 AM

He meant you need to add a whole new method:
@Override
public String toString() {
   return "whatever you want it to show";
}
That's pretty basic Java. How did you get this far in hibernate and Spring without knowing the basics.
  • 0

#10 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 10 May 2012 - 05:02 AM

Thanks that works!

That's pretty basic Java. How did you get this far in hibernate and Spring without knowing the basics.


Google.
  • 0





Also tagged with one or more of these keywords: spring, hibernate, property-editor, spring-mvc, null

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download