2

I have a problem with nullPointer in getter method.

Here is the code:

 public String getTitle()
  {
    if(!title.isEmpty())
        return title;
    else 
        return "foo";
  }

When I change this to normal getter it works perfectly.

public String getTitle()
  {
    return title;
  }

Where is the trick? Is there a reflection trick?

UPDATE The problem is not null checking, but excecuting getter without calling it

STACKTRACE

org.codehaus.jackson.map.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0]- >si.mikropis.webkiosk.model.vao.wine.Wine["title"])

Caused by: java.lang.NullPointerException at si.mikropis.webkiosk.model.vao.base.BaseModel.getTitle(BaseModel.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:483) at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:418) at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) ... 32 more

Mitja Rogl
  • 894
  • 9
  • 22
  • 39
  • 1
    I would post your complete stack trace. – Brian Agnew Aug 21 '13 at 09:51
  • Please add your IDE (Netbeans, Android SDK, ...), included libraries (log4j, AndroidAnnotations, ...) and used frameworks as well (e.g. Hibernate, Struts, ...). – Trinimon Aug 21 '13 at 10:04
  • I'm using jersey and jackson. – Mitja Rogl Aug 21 '13 at 10:23
  • @Trinimon I posted the stacktrace, I guess is jackson and jersey problem, because I'm using restful services. – Mitja Rogl Aug 21 '13 at 10:32
  • Note the BeanPropertyWriter which will use the bean conventions as I've outlined below – Brian Agnew Aug 21 '13 at 10:51
  • @BrianAgnew So what do you suggest? – Mitja Rogl Aug 21 '13 at 10:55
  • 1
    @extra90: as the JSON parser has to run through all members of the object, it might be that there is no way out. Only solutions I can think of is either you tell Jackson not to include `null` values (check http://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null) or you try to include a custom mapper (see for instance http://wiki.fasterxml.com/JacksonHowToCustomSerializers); if you care about performance I'd go for gson anyway (see http://stackoverflow.com/questions/338586/a-better-java-json-library) – Trinimon Aug 21 '13 at 11:23
  • 1
    @extra90 - you have multiple solutions below for handling the null. I would however investigate *why* you have the null. Obviously 'title' isn't being set, and that's a *different* matter/question/subject – Brian Agnew Aug 21 '13 at 11:53

9 Answers9

10

Check, if title is null as well:

  public String getTitle()
  {
    if(title != null && !title.isEmpty())
        return title;
    else 
        return "foo";
  }

p.s. additional comment as per updated issue: as the JSON parser has to run through all members of the object, it might be that there is no simple way out. Only solutions I can think of is either to tell Jackson not to include null values or to implement a custom mapper; if performance is the manin concern I'd go for google-gson as their library is quite ast and less memory consuming than Jackson.

Community
  • 1
  • 1
Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • 1
    Yes but the problem is that the getter is excecuting without calling it. – Mitja Rogl Aug 21 '13 at 09:24
  • @extra90 Set a breakpoint in it and check the stack trace when it's executed. – Joachim Isaksson Aug 21 '13 at 09:24
  • I think that this *doesn't* address the OP's issue wrt. the getter not being called *explicitly* – Brian Agnew Aug 21 '13 at 09:29
  • @extra90: what do you mean exactly by "executing without calling it"? Do you use any kind of framework, that might invoke the getter? – Trinimon Aug 21 '13 at 09:29
  • @JoachimIsaksson When the object is created, the getter is executed. Don't know why? – Mitja Rogl Aug 21 '13 at 09:29
  • @Extra90 - see my answer re. JavaBean conventions – Brian Agnew Aug 21 '13 at 09:33
  • @Brian: I assumed that issue is based on a NPE in the getter; there is no reflection trick. – Trinimon Aug 21 '13 at 09:36
  • @Trinimon Null cheks are ok, but calling getter without explicity calling it can slow down performance. – Mitja Rogl Aug 21 '13 at 09:40
  • @extra90: that's true :) - it's very important to know which kind of framework/container you are using. I'm 100% sure this wouldn't happen in a simple Java program. Brian gave you a good example of what could happen. But Spring is only one example and there are many other frameworks that use JavaBeans and Reflection as well (e.g. ORM frameworks: Hibernate, ORMLite; MVC frameworks: Struts, ...). – Trinimon Aug 21 '13 at 09:54
3

I note specifically your comment re. not calling the getter. Lots of frameworks use the JavaBean convention, such that if you specify a property title, the framework will call getTitle().

e.g. in Spring (not implying you're using Spring):

<property name="title" value="abc"/>

would call your setter implicitly.

So you're right to suggest there's some reflection going on. I suspect you're referencing the title property somewhere, and whatever framework in use is invoking your getter implicitly. As noted, you likely have a possible NPE in your getter. A breakpoint will tell you precisely how it's being invoked. Or, in fact, your exception stacktrace.

EDIT: I note your stacktrace, and in particular:

org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:483)

which suggests such reflection is being used.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • The setTitle comes later, I get NPE at the start, when the object is created. I' m not using spring. – Mitja Rogl Aug 21 '13 at 09:35
  • @Brian: even though this explains why the getter is called, it does not explain why the first one should not work. I do not know any framework that depends on the implemntation of a getter ... – Trinimon Aug 21 '13 at 09:42
  • 1
    @extra90 - I'm not suggesting you're using Spring. But I am suggesting that frameworks work like that, and nominating a property will result in the corresponding setter/getter being invoked – Brian Agnew Aug 21 '13 at 09:47
2

Here in the line

  if(!title.isEmpty())

title is null.

Please add null check.But checking in getter is a not a good idea. instead

String title  ="foo"

public String getTitle()
  {
    return title;
  }

public String setTitle(String title)
    {
    this.title = title;
    }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

If you do these null/empty checks a lot, it may be worth to include Commons Lang.

 return StringUtils.defaultIfBlank(title, "foo");

Null-safe and handles Unicode whitespace.

Thilo
  • 257,207
  • 101
  • 511
  • 656
1
 if(!title.isEmpty())

You are directly performing operations on title without checking whether it is null or not. You must do NPE check first

if(title != null) {
if(!title.isEmpty())
    return title;
 } 
 return "foo";
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

your title object is null and hence calling isEmpty on it throws null pointer exception.

The title is null even in the second alternative but as you are not calling any method on it hence it does not throw null pointer exception.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Title might be null.

public String getTitle() {
    if(title !=null && !title.isEmpty())
        return title;
    else 
        return "foo";
}
kiheru
  • 6,588
  • 25
  • 31
r3ap3r
  • 2,775
  • 2
  • 17
  • 16
0

You can alternatively define title as: String title = "";

Selim
  • 1,064
  • 11
  • 23
0

personally I'd use ternary operations here

public String getTitle() {
   return (title !=null && !title.isEmpty()) ? title : "foo";
}

Apart from that I'd say all other answers give you want you want.

RNJ
  • 15,272
  • 18
  • 86
  • 131