1

so in the long run I'm trying to write a reminder application in Java for my capstone project. Right now I'm trying to write some code to a json file so I can save the information but I've ran into this exception. I have the jar files imported to my project so I'm not sure why it cant seem to access it. Is there anything I can do to fix this?

Heres my java build path for the project. I'm using eclipse and for some reason it has this module path and class path. I don't know the difference between the two but it errors out if I put the jars in the module path.

package util;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonUtil {
    private static ObjectMapper mapper;
    static {
        mapper = new ObjectMapper();
    }
    public static String convertJavaToJson(Object object)
    {
        String jsonResult = "";
        try {
            jsonResult = mapper.writeValueAsString(object);
        } 
        catch (JsonGenerationException e) {
            System.out.println("1 Exception Occured while converting Java Object into Json -->" + e.getMessage());
        }
        catch (JsonMappingException e) {
            System.out.println("2 Exception Occured while converting Java Object into Json -->" + e.getMessage());
        }
        catch (IOException e) {
            System.out.println("3 Exception Occured while converting Java Object into Json -->" + e.getMessage());
        }
        catch (Exception e) {
            System.out.println("4 Exception Occured while converting Java Object into Json -->" + e.getMessage());
        }
        return jsonResult;
    }
}

Exception is coming from the final catch block

4 Exception Occured while converting Java Object into Json -->Failed to instantiate standard serializer (of type org.codehaus.jackson.map.ser.std.NullSerializer): class org.codehaus.jackson.map.ser.BasicSerializerFactory cannot access a member of class org.codehaus.jackson.map.ser.std.NullSerializer with modifiers "private"

Any help is appreciated!

  • What kind of object do you try to convert? – Ackdari Apr 28 '20 at 10:30
  • 3
    `org.codehaus.jackson`? Isn't that a very old library? I think the one you should use is `com.fasterxml.jackson`. See also [org.codehaus.jackson versus com.fasterxml.jackson.core](https://stackoverflow.com/questions/30782706/org-codehaus-jackson-versus-com-fasterxml-jackson-core) – Lino Apr 28 '20 at 10:41
  • 2
    Take a look on this question [org.codehaus.jackson versus com.fasterxml.jackson.core](https://stackoverflow.com/questions/30782706/org-codehaus-jackson-versus-com-fasterxml-jackson-core). Next, open [Jackson Databind » 2.11.0](https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.11.0) and download it with dependencies: `jackson-annotations`, `jackson-core`. Remove old jars and attach these 3. And finally, but it should be at start, you need to learn and use [Maven](https://maven.apache.org/) – Michał Ziober Apr 28 '20 at 10:46
  • Thank you all for helping! I was unaware the jackson I was using was old but it makes sense since I was going off of old YT videos. I ran into a new exception but I think it's for my object (which is a custom class for reminder events with localdatetime usage) – TheMidnightMage Apr 29 '20 at 03:47

1 Answers1

0

This code works for me but I have this imports :

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

because I use the dependency com.fasterxml.jackson.core:jackson-databind (version 2.9.9.3).

As said by Lino, the org.codehaus.jackson is kind of old.

Also, you could change:

private static ObjectMapper mapper;
    static {
        mapper = new ObjectMapper();
    }

by:

private static ObjectMapper mapper = new ObjectMapper();
Nicolas Dupouy
  • 450
  • 1
  • 7
  • 11