-1

I am new in JSON with java and was trying to explore it using maven with eclipse.
This is my json file.

  "name": "Jason Bourne",
  "profession": "Super agent",
  "bad-guy": false,
  "kills": 1000,
  "phoneNumbers": [
    {
      "type": "home",
      "number": "123-456-789"
    },
    {
      "type": "work",
      "number": "123-555-555"
    }
  ]
},
{
  "name": "Jason Voorhees",
  "profession": "Maniac killer",
  "bad-guy": true,
  "kills": 100,
  "phoneNumbers": [
    {
      "type": "office",
      "number": "666-666-666"
    }
  ]
},
{
  "name": "Jason",
  "profession": "Lead of Argonauts",
  "bad-guy": false,
  "kills": null
}
]  

My pom.xml file looks like this

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.assignment</groupId>
  <artifactId>assgQuestion</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jsonp.version>1.1.2</jsonp.version>
 </properties>
  
  <dependencies>
    <!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>8.0</version>
    <scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.json/javax.json-api -->
<dependency>
    <groupId>javax.json</groupId>
    <artifactId>javax.json-api</artifactId>
    <version>${jsonp.version}</version>
</dependency>

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>${jsonp.version}</version>
</dependency>

    <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

  </dependencies>
  
  <build>


<plugins>


<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.7.0</version>


<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>

</plugin>


<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>exec-maven-plugin</artifactId>

<version>1.6.0</version>


<configuration>

<executable>java</executable>


<arguments>

<argument>-classpath</argument>

<!-- automatically creates the classpath using all project dependencies,also adding the project build directory -->


<classpath/>

<argument>packt.javaee.json.Main</argument>

</arguments>

</configuration>

</plugin>

</plugins>

</build>
</project>  

I am trying to list all the jsonp events. The following is the code snippet for the same.

public class ReadJSONFile {
    
   public static void main(String args[]) {
       new ReadJSONFile().start();
   }
   
   private void start() {
       while(true) {
           printOptions();
           
           Scanner sc = new Scanner(System.in);
           switch(sc.nextLine()){
                case "1" : 
                jsonParserScenario();
                break;
           }    
                
          }
       }
   
   
        private void printOptions() {
            System.out.println("1. JSON Parser");
            
        }
        
        private void jsonParserScenario() {
          //Creating a JSONParser object
            JsonParser parser = Json.createParser(ReadJSONFile.class.getResourceAsStream("/person.json"));
            
            while (parser.hasNext()) {
                
                //Get event from parse
                JsonParser.Event event = parser.next();
                
                //Display event on screen
                System.out.println(event.toString());
            }
       }
}  

AFter running the above file I am getting the following error.

1. JSON Parser
1
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.InputStream.read()" because "this.in" is null
    at org.glassfish.json.UnicodeDetectingInputStream.fillBuf(UnicodeDetectingInputStream.java:89)
    at org.glassfish.json.UnicodeDetectingInputStream.detectEncoding(UnicodeDetectingInputStream.java:128)
    at org.glassfish.json.UnicodeDetectingInputStream.<init>(UnicodeDetectingInputStream.java:75)
    at org.glassfish.json.JsonParserImpl.<init>(JsonParserImpl.java:95)
    at org.glassfish.json.JsonProviderImpl.createParser(JsonProviderImpl.java:88)
    at javax.json.Json.createParser(Json.java:109)
    at assgQuestion.ReadJSONFile.jsonParserScenario(ReadJSONFile.java:52)
    at assgQuestion.ReadJSONFile.start(ReadJSONFile.java:29)
    at assgQuestion.ReadJSONFile.main(ReadJSONFile.java:19)  

Can someone help me figure out whats wrong.

Anonymous
  • 23
  • 3
  • Is the target json file actually located in the root source dir? Or rather: is the Json you want to parse actually located correctly? This looks a bit like a non existent file (maybe you are working on linux and have no permissions?) – TreffnonX Mar 15 '21 at 13:48
  • It is not a missing file or file / directory permission issue. The code is using getResourceAsStream ... so the issue is in locating a resource rather than a file (in the normal sense). (The resource could be represented as a file **now** ... while the OP is developing and running code in the IDE, but if / when the app is deployed, the JSON resource will be an entry in the application's JAR file.) – Stephen C Mar 16 '21 at 01:43

1 Answers1

0

The problem (a NullPointerException) occurs within the createParser call made on this line:

JsonParser parser = Json.createParser(
    ReadJSONFile.class.getResourceAsStream("/person.json"));

It happens because the expresssion

ReadJSONFile.class.getResourceAsStream("/person.json")

is evaluating to null.

If you look at the javadoc for Class.getResourceAsStream, you will see that null is returned when a classes classloader cannot find the named resource in the classloader's namespace.

Since you are using Maven, the file that it is looking for needs to be in your project's "src/main/resources" directory; see Introduction to the Standard Directory Layout for details.

It might be a better idea to put the JSON file in the file system and use FileInputStream to open it. The problem with using a resource here is that your application will only be able to read JSON files embedded in the application; e.g. in its JAR file.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216